Module: Gruf::Service

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

Overview

Module for gRPC endpoints

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



164
165
166
167
168
# File 'lib/gruf/service.rb', line 164

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gruf/service.rb', line 85

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



177
178
179
# File 'lib/gruf/service.rb', line 177

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



71
72
73
74
75
76
# File 'lib/gruf/service.rb', line 71

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 = {}) ⇒ RPC::Error

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

Returns:

  • (RPC::Error)


192
193
194
195
196
197
198
# File 'lib/gruf/service.rb', line 192

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



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gruf/service.rb', line 123

def outer_around_call(call_signature, req, call, &block)
  outer_around_hooks = []
  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



105
106
107
108
109
110
111
112
113
114
# File 'lib/gruf/service.rb', line 105

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/gruf/service.rb', line 143

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