Class: Griffin::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/griffin/server.rb

Constant Summary collapse

DEFAULT_BACKLOG_SIZE =
1024
GRACEFUL_SHUTDOWN =
'0'
FORCIBLE_SHUTDOWN =
'1'
GRACEFUL_RESTART =
'2'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], settings: [], **opts) ⇒ Server

Returns a new instance of Server.

Parameters:

  • min_pool_size (Integer)

    Worker thread mininum size

  • max_pool_size (Integer)

    Worker thread maximun size

  • min_connection_size (Integer)

    Maximun connection of TCP

  • max_connection_size (Integer)

    Minimum connection of TCP

  • interceptors (Array<GrpcKit::GRPC::ServerInterceptor>) (defaults to: [])

    list of interceptors

  • settings (Array<DS9::Settings,Integer>) (defaults to: [])

    list of HTTP2-Settings headers



45
46
47
48
49
50
51
52
# File 'lib/griffin/server.rb', line 45

def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], settings: [], **opts)
  @min_connection_size = min_connection_size
  @max_connection_size = max_connection_size
  @server = GrpcKit::Server.new(interceptors: interceptors, min_pool_size: min_pool_size, max_pool_size: max_pool_size, settings: settings)
  @opts = opts
  @status = :run
  @worker_id = 0
end

Class Method Details

.config_builderObject



34
35
36
# File 'lib/griffin/server.rb', line 34

def config_builder
  @config_builder ||= Griffin::ServerConfigBuilder.new
end

.configure {|config_builder| ... } ⇒ Object

Yields:



30
31
32
# File 'lib/griffin/server.rb', line 30

def configure
  yield(config_builder)
end

.run(bind: nil, port: nil) ⇒ Object

Parameters:

  • bind (String) (defaults to: nil)
  • port (Integer, String) (defaults to: nil)


20
21
22
23
24
25
26
27
28
# File 'lib/griffin/server.rb', line 20

def run(bind: nil, port: nil)
  c = config_builder.build
  if c[:services].empty?
    raise 'Required at least one service to handle reqeust'
  end

  opts = { bind: bind, port: port }.compact
  Griffin::Engine.start(c.merge(opts), cluster: Integer(c[:workers]) > 1)
end

Instance Method Details

#before_run(worker_id = 0) ⇒ Object



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

def before_run(worker_id = 0)
  @worker_id = worker_id

  # To separete fd with other forked process
  @socks = []
  @command, @signal = IO.pipe
  @socks << @command
end

#handle(handler) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/griffin/server.rb', line 54

def handle(handler)
  @server.handle(handler)
  klass = handler.is_a?(Class) ? handler : handler.class
  klass.rpc_descs.each_key do |path|
    Griffin.logger.info("Handle #{path}")
  end
end

#run(sock, blocking: true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/griffin/server.rb', line 71

def run(sock, blocking: true)
  @socks << sock

  @thread_pool = Griffin::ThreadPool.new(min: @min_connection_size, max: @max_connection_size) do |conn|
    @server.run(conn)
  end

  if blocking
    handle_server
  else
    Thread.new { handle_server }
  end
end

#shutdown(reason = GRACEFUL_SHUTDOWN) ⇒ Object



85
86
87
# File 'lib/griffin/server.rb', line 85

def shutdown(reason = GRACEFUL_SHUTDOWN)
  @signal.write(reason)
end