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

Parameters:

  • options (Hash) (defaults to: {})


36
37
38
39
40
41
42
43
44
45
46
47
48
# 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
  @stop_server = false
  @stop_server_cv = ConditionVariable.new
  @stop_server_mu = Monitor.new
  @server_mu = Monitor.new
  @hostname = options.fetch(:hostname, Gruf.server_binding_url)
  setup
end

Instance Attribute Details

#optionsHash (readonly)

Returns Hash of options passed into the server.

Returns:

  • (Hash)

    Hash of options passed into the server



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

def options
  @options
end

#portInteger (readonly)

Returns The port the server is bound to.

Returns:

  • (Integer)

    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

Parameters:

  • klass (Class)

    The Interceptor to add to the registry

  • opts (Hash) (defaults to: {})

    A hash of options for the interceptor

Raises:



115
116
117
118
# File 'lib/gruf/server.rb', line 115

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

#add_service(klass) ⇒ Object

Add a gRPC service stub to be served by gruf

Parameters:

  • klass (Class)

Raises:



103
104
105
106
# File 'lib/gruf/server.rb', line 103

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

#clear_interceptorsObject

Clear the interceptor registry of interceptors



166
167
168
169
# File 'lib/gruf/server.rb', line 166

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

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

Insert an interceptor after another in the currently registered order of execution

Parameters:

  • after_class (Class)

    The interceptor that you want to add the new interceptor after

  • interceptor_class (Class)

    The Interceptor to add to the registry

  • options (Hash) (defaults to: {})

    A hash of options for the interceptor

Raises:



139
140
141
142
# File 'lib/gruf/server.rb', line 139

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

Insert an interceptor before another in the currently registered order of execution

Parameters:

  • before_class (Class)

    The interceptor that you want to add the new interceptor before

  • interceptor_class (Class)

    The Interceptor to add to the registry

  • options (Hash) (defaults to: {})

    A hash of options for the interceptor

Raises:



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

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

Returns:

  • (Array<Class>)


149
150
151
# File 'lib/gruf/server.rb', line 149

def list_interceptors
  @interceptors.list
end

#remove_interceptor(klass) ⇒ Object

Remove an interceptor from the server

Parameters:

  • klass (Class)

Raises:



158
159
160
161
# File 'lib/gruf/server.rb', line 158

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

#serverGRPC::RpcServer

Returns The GRPC server running.

Returns:

  • (GRPC::RpcServer)

    The GRPC server running



53
54
55
56
57
58
59
60
61
62
# File 'lib/gruf/server.rb', line 53

def server
  @server_mu.synchronize do
    @server ||= begin
      server = GRPC::RpcServer.new(options)
      @port = server.add_http2_port(@hostname, ssl_credentials)
      @services.each { |s| server.handle(s) }
      server
    end
  end
end

#start!Object

Start the gRPC server

:nocov:



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
94
# File 'lib/gruf/server.rb', line 68

def start!
  update_proc_title(:starting)

  server_thread = Thread.new do
    logger.info { "Starting gruf server at #{@hostname}..." }
    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