Module: Gruf::Service

Extended by:
ActiveSupport::Concern
Defined in:
lib/gruf/service.rb

Overview

Module for gRPC endpoints. Include this in any gRPC service you wish Gruf to serve. It will automatically mount it to the given gruf server and can be run via the command:

bundle exec gruf

Instance Method Summary collapse

Instance Method Details

#after_call(success, response, call_signature, req, call) ⇒ Object

Happens after a call

Parameters:

  • success (Boolean)

    Whether or not the result was successful

  • response (Object)

    The response object returned from the gRPC call

  • call_signature (Symbol)

    The method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object

Returns:

  • (Object)

    If extending this method or using the after_call_hook, you must return the response object



198
199
200
201
202
# File 'lib/gruf/service.rb', line 198

def after_call(success, response, call_signature, req, call)
  Gruf::Hooks::Registry.each do |_name, h|
    h.new(self, Gruf.hook_options).after(success, response, call_signature, req, call) if h.instance_methods.include?(:after)
  end
end

#around_call(call_signature, req, call, &block) ⇒ Object

Happens around a call.

Parameters:

  • call_signature (Symbol)

    The gRPC method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gruf/service.rb', line 113

def around_call(call_signature, req, call, &block)
  around_hooks = []
  Gruf::Hooks::Registry.each do |_name, h|
    around_hooks << h.new(self, Gruf.hook_options) if h.instance_methods.include?(:around)
  end
  if around_hooks.any?
    run_around_hook(around_hooks, call_signature, req, call, &block)
  else
    yield
  end
end

#authenticate(_method, req, call) ⇒ Object

Authenticate the endpoint caller.

Parameters:

  • _method (Symbol)

    The method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



211
212
213
# File 'lib/gruf/service.rb', line 211

def authenticate(_method, req, call)
  fail!(req, call, :unauthenticated) unless Authentication.verify(call)
end

#before_call(call_signature, req, call) ⇒ Object

Happens before a call.

Parameters:

  • call_signature (Symbol)

    The method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



99
100
101
102
103
104
# File 'lib/gruf/service.rb', line 99

def before_call(call_signature, req, call)
  authenticate(call_signature, req, call)
  Gruf::Hooks::Registry.each do |_name, h|
    h.new(self, Gruf.hook_options).before(call_signature, req, call) if h.instance_methods.include?(:before)
  end
end

#fail!(_req, call, error_code, app_code = nil, message = '', metadata = {}) ⇒ Object

Will issue a GRPC BadStatus exception, with a code based on the code passed.

Parameters:

  • _req (Object)

    The request object being sent

  • call (GRPC::ActiveCall)

    The gRPC active call

  • error_code (Symbol)

    The network error code that maps to gRPC status codes

  • app_code (Symbol) (defaults to: nil)

    The application-specific code for the error

  • message (String) (defaults to: '')

    (Optional) A detail message about the error

  • metadata (Hash) (defaults to: {})

    (Optional) Any metadata to inject into the trailing metadata for the response



225
226
227
228
229
230
231
# File 'lib/gruf/service.rb', line 225

def fail!(_req, call, error_code, app_code = nil, message = '',  = {})
  error.code = error_code.to_sym
  error.app_code = app_code ? app_code.to_sym : error.code
  error.message = message.to_s
  error. = 
  error.fail!(call)
end

#outer_around_call(call_signature, req, call, &block) ⇒ Object

Happens around the entire call chain - before, around, the call itself, and after hooks.

Parameters:

  • call_signature (Symbol)

    The gRPC method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gruf/service.rb', line 151

def outer_around_call(call_signature, req, call, &block)
  outer_around_hooks = []

  # run instrumentation hooks as outer_around calls
  Gruf::Instrumentation::Registry.each do |_name, h|
    outer_around_hooks << h.new(self, Gruf.instrumentation_options)
  end

  Gruf::Hooks::Registry.each do |_name, h|
    outer_around_hooks << h.new(self, Gruf.hook_options) if h.instance_methods.include?(:outer_around)
  end
  if outer_around_hooks.any?
    run_outer_around_hook(outer_around_hooks, call_signature, req, call, &block)
  else
    yield
  end
end

#run_around_hook(hooks, call_signature, req, call, &_) ⇒ Object

Run all around hooks recursively, starting with the last loaded

Parameters:

  • hooks (Array<Gruf::Hooks::Base>)

    The current stack of hooks

  • call_signature (Symbol)

    The gRPC method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



133
134
135
136
137
138
139
140
141
142
# File 'lib/gruf/service.rb', line 133

def run_around_hook(hooks, call_signature, req, call, &_)
  h = hooks.pop
  h.around(call_signature, req, call) do
    if hooks.any?
      run_around_hook(hooks, call_signature, req, call) { yield }
    else
      yield
    end
  end
end

#run_outer_around_hook(hooks, call_signature, req, call, &_) ⇒ Object

Run all outer around hooks recursively, starting with the last loaded

Parameters:

  • hooks (Array<Gruf::Hooks::Base>)

    The current stack of hooks

  • call_signature (Symbol)

    The gRPC method being called

  • req (Object)

    The request object

  • call (GRPC::ActiveCall)

    The gRPC active call object



177
178
179
180
181
182
183
184
185
186
# File 'lib/gruf/service.rb', line 177

def run_outer_around_hook(hooks, call_signature, req, call, &_)
  h = hooks.pop
  h.outer_around(call_signature, req, call) do
    if hooks.any?
      run_outer_around_hook(hooks, call_signature, req, call) { yield }
    else
      yield
    end
  end
end