Class: Gruf::Server
Overview
Represents a gRPC server. Automatically loads and augments gRPC handlers and services based on configuration values.
Defined Under Namespace
Classes: ServerAlreadyStartedError
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Hash of options passed into the server.
-
#port ⇒ Integer
readonly
The port the server is bound to.
Instance Method Summary collapse
-
#add_interceptor(klass, opts = {}) ⇒ Object
Add an interceptor to the server.
- #add_service(klass) ⇒ Object
-
#clear_interceptors ⇒ Object
Clear the interceptor registry of interceptors.
-
#initialize(options = {}) ⇒ Server
constructor
Initialize the server and load and setup the services.
- #insert_interceptor_after(after_class, interceptor_class, options = {}) ⇒ Object
- #insert_interceptor_before(before_class, interceptor_class, options = {}) ⇒ Object
-
#list_interceptors ⇒ Array<Class>
Return the current list of added interceptor classes.
-
#remove_interceptor(klass) ⇒ Object
Remove an interceptor from the server.
-
#server ⇒ GRPC::RpcServer
The GRPC server running.
-
#start! ⇒ Object
Start the gRPC server.
Methods included from Loggable
Constructor Details
#initialize(options = {}) ⇒ Server
Initialize the server and load and setup the services
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gruf/server.rb', line 36 def initialize( = {}) = || {} @interceptors = .fetch(:interceptor_registry, Gruf.interceptors) @interceptors = Gruf::Interceptors::Registry.new unless @interceptors.is_a?(Gruf::Interceptors::Registry) @services = [] @started = false @stop_server = false @stop_server_cv = ConditionVariable.new @stop_server_mu = Monitor.new @server_mu = Monitor.new setup end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Hash of options passed into the server.
29 30 31 |
# File 'lib/gruf/server.rb', line 29 def end |
#port ⇒ Integer (readonly)
Returns The port the server is bound to.
27 28 29 |
# File 'lib/gruf/server.rb', line 27 def port @port end |
Instance Method Details
#add_interceptor(klass, opts = {}) ⇒ Object
Add an interceptor to the server
112 113 114 115 |
# File 'lib/gruf/server.rb', line 112 def add_interceptor(klass, opts = {}) raise ServerAlreadyStartedError if @started @interceptors.use(klass, opts) end |
#add_service(klass) ⇒ Object
100 101 102 103 |
# File 'lib/gruf/server.rb', line 100 def add_service(klass) raise ServerAlreadyStartedError if @started @services << klass unless @services.include?(klass) end |
#clear_interceptors ⇒ Object
Clear the interceptor registry of interceptors
157 158 159 160 |
# File 'lib/gruf/server.rb', line 157 def clear_interceptors raise ServerAlreadyStartedError if @started @interceptors.clear end |
#insert_interceptor_after(after_class, interceptor_class, options = {}) ⇒ Object
132 133 134 135 |
# File 'lib/gruf/server.rb', line 132 def insert_interceptor_after(after_class, interceptor_class, = {}) raise ServerAlreadyStartedError if @started @interceptors.insert_after(after_class, interceptor_class, ) end |
#insert_interceptor_before(before_class, interceptor_class, options = {}) ⇒ Object
122 123 124 125 |
# File 'lib/gruf/server.rb', line 122 def insert_interceptor_before(before_class, interceptor_class, = {}) raise ServerAlreadyStartedError if @started @interceptors.insert_before(before_class, interceptor_class, ) end |
#list_interceptors ⇒ Array<Class>
Return the current list of added interceptor classes
142 143 144 |
# File 'lib/gruf/server.rb', line 142 def list_interceptors @interceptors.list end |
#remove_interceptor(klass) ⇒ Object
Remove an interceptor from the server
149 150 151 152 |
# File 'lib/gruf/server.rb', line 149 def remove_interceptor(klass) raise ServerAlreadyStartedError if @started @interceptors.remove(klass) end |
#server ⇒ GRPC::RpcServer
Returns The GRPC server running.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gruf/server.rb', line 52 def server @server_mu.synchronize do @server ||= begin server = GRPC::RpcServer.new() @port = server.add_http2_port(.fetch(:hostname, Gruf.server_binding_url), ssl_credentials) @services.each { |s| server.handle(s) } server end end end |
#start! ⇒ Object
Start the gRPC server
:nocov:
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gruf/server.rb', line 67 def start! update_proc_title(:starting) server_thread = Thread.new do logger.info { 'Booting gRPC Server...' } server.run end stop_server_thread = Thread.new do loop do break if @stop_server @stop_server_mu.synchronize { @stop_server_cv.wait(@stop_server_mu, 10) } end logger.info { 'Shutting down...' } server.stop end server.wait_till_running @started = true update_proc_title(:serving) stop_server_thread.join server_thread.join @started = false update_proc_title(:stopped) logger.info { 'Goodbye!' } end |