Class: Gruf::Server

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/server.rb

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

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(options = {}) ⇒ Server

Initialize the server and load and setup the services



36
37
38
39
40
41
42
43
# File 'lib/gruf/server.rb', line 36

def initialize(options = {})
  @options = options || {}
  @interceptors = options.fetch(:interceptor_registry, Gruf.interceptors)
  @interceptors = Gruf::Interceptors::Registry.new unless @interceptors.is_a?(Gruf::Interceptors::Registry)
  @services = []
  @started = false
  setup!
end

Instance Attribute Details

#optionsHash (readonly)



29
30
31
# File 'lib/gruf/server.rb', line 29

def options
  @options
end

#portInteger (readonly)



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

Raises:



90
91
92
93
# File 'lib/gruf/server.rb', line 90

def add_interceptor(klass, opts = {})
  raise ServerAlreadyStartedError if @started
  @interceptors.use(klass, opts)
end

#add_service(klass) ⇒ Object

Raises:



78
79
80
81
# File 'lib/gruf/server.rb', line 78

def add_service(klass)
  raise ServerAlreadyStartedError if @started
  @services << klass unless @services.include?(klass)
end

#clear_interceptorsObject

Clear the interceptor registry of interceptors



135
136
137
138
# File 'lib/gruf/server.rb', line 135

def clear_interceptors
  raise ServerAlreadyStartedError if @started
  @interceptors.clear
end

#insert_interceptor_after(after_class, interceptor_class, options = {}) ⇒ Object



110
111
112
113
# File 'lib/gruf/server.rb', line 110

def insert_interceptor_after(after_class, interceptor_class, options = {})
  raise ServerAlreadyStartedError if @started
  @interceptors.insert_after(after_class, interceptor_class, options)
end

#insert_interceptor_before(before_class, interceptor_class, options = {}) ⇒ Object



100
101
102
103
# File 'lib/gruf/server.rb', line 100

def insert_interceptor_before(before_class, interceptor_class, options = {})
  raise ServerAlreadyStartedError if @started
  @interceptors.insert_before(before_class, interceptor_class, options)
end

#list_interceptorsArray<Class>

Return the current list of added interceptor classes



120
121
122
# File 'lib/gruf/server.rb', line 120

def list_interceptors
  @interceptors.list
end

#remove_interceptor(klass) ⇒ Object

Remove an interceptor from the server



127
128
129
130
# File 'lib/gruf/server.rb', line 127

def remove_interceptor(klass)
  raise ServerAlreadyStartedError if @started
  @interceptors.remove(klass)
end

#serverGRPC::RpcServer



48
49
50
51
52
53
54
55
# File 'lib/gruf/server.rb', line 48

def server
  unless @server
    @server = GRPC::RpcServer.new(options)
    @port = @server.add_http2_port(options.fetch(:hostname, Gruf.server_binding_url), ssl_credentials)
    @services.each { |s| @server.handle(s) }
  end
  @server
end

#start!Object

Start the gRPC server

:nocov: rubocop:disable Lint/ShadowedException



62
63
64
65
66
67
68
69
70
# File 'lib/gruf/server.rb', line 62

def start!
  logger.info { 'Booting gRPC Server...' }
  @started = true
  server.run_till_terminated
rescue Interrupt, SignalException, SystemExit
  logger.info { 'Shutting down gRPC server...' }
  @started = false
  server.stop
end