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'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], **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


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

def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], **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)
  @opts = opts
  @status = :run
  @worker_id = 0
end

Class Method Details

.config_builderObject


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

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

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

Yields:


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

def configure
  yield(config_builder)
end

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

Parameters:

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

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

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


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

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


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

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


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

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


83
84
85
# File 'lib/griffin/server.rb', line 83

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