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



173
174
175
176
177
# File 'lib/gruf/service.rb', line 173

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.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gruf/service.rb', line 88

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.



186
187
188
# File 'lib/gruf/service.rb', line 186

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.



74
75
76
77
78
79
# File 'lib/gruf/service.rb', line 74

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.



200
201
202
203
204
205
206
# File 'lib/gruf/service.rb', line 200

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gruf/service.rb', line 126

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/gruf/service.rb', line 108

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



152
153
154
155
156
157
158
159
160
161
# File 'lib/gruf/service.rb', line 152

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