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
-
#after_call(success, response, call_signature, req, call) ⇒ Object
Happens after a call.
-
#around_call(call_signature, req, call, &block) ⇒ Object
Happens around a call.
-
#authenticate(_method, req, call) ⇒ Object
Authenticate the endpoint caller.
-
#before_call(call_signature, req, call) ⇒ Object
Happens before a call.
-
#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.
-
#outer_around_call(call_signature, req, call, &block) ⇒ Object
Happens around the entire call chain - before, around, the call itself, and after hooks.
-
#run_around_hook(hooks, call_signature, req, call, &_) ⇒ Object
Run all around hooks recursively, starting with the last loaded.
-
#run_outer_around_hook(hooks, call_signature, req, call, &_) ⇒ Object
Run all outer around hooks recursively, starting with the last loaded.
Instance Method Details
#after_call(success, response, call_signature, req, call) ⇒ Object
Happens after a call
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.).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.
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.) 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.
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.
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.).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.
225 226 227 228 229 230 231 |
# File 'lib/gruf/service.rb', line 225 def fail!(_req, call, error_code, app_code = nil, = '', = {}) error.code = error_code.to_sym error.app_code = app_code ? app_code.to_sym : error.code error. = .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.
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.) end Gruf::Hooks::Registry.each do |_name, h| outer_around_hooks << h.new(self, Gruf.) 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
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
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 |