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: [], max_receive_message_size: nil, max_send_message_size: nil, **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

  • max_receive_message_size (Integer, nil) (defaults to: nil)

    Specify the default maximum size of inbound message in bytes. Default to 4MB.

  • max_send_message_size (Integer, nil) (defaults to: nil)

    Specify the default maximum size of outbound message in bytes. Default to 4MB.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/griffin/server.rb', line 47

def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], settings: [], max_receive_message_size: nil, max_send_message_size: nil, **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,
    max_receive_message_size: max_receive_message_size,
    max_send_message_size: max_send_message_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



71
72
73
74
75
76
77
78
# File 'lib/griffin/server.rb', line 71

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



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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/griffin/server.rb', line 80

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



94
95
96
# File 'lib/griffin/server.rb', line 94

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