Class: Griffin::Server

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

Constant Summary collapse

DEFAULT_WORKER_SIZE =
10
DEFAULT_BACKLOG_SIZE =
1024
GRACEFUL_SHUTDOWN =
'0'
FORCIBLE_SHUTDOWN =
'1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_size: DEFAULT_WORKER_SIZE, **opts) ⇒ Server

Returns a new instance of Server.



32
33
34
35
36
37
38
# File 'lib/griffin/server.rb', line 32

def initialize(worker_size: DEFAULT_WORKER_SIZE, **opts)
  @worker_size = worker_size
  @server = GrpcKit::Server.new
  @opts = opts
  @status = :run
  @worker_id = 0
end

Class Method Details

.config_builderObject



27
28
29
# File 'lib/griffin/server.rb', line 27

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

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

Yields:



23
24
25
# File 'lib/griffin/server.rb', line 23

def configure
  yield(config_builder)
end

.runObject



18
19
20
21
# File 'lib/griffin/server.rb', line 18

def run
  c = config_builder.build
  Griffin::Engine.start(c, cluster: Integer(c[:workers]) > 1)
end

Instance Method Details

#before_run(worker_id = 0) ⇒ Object



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

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



40
41
42
43
44
45
# File 'lib/griffin/server.rb', line 40

def handle(handler)
  @server.handle(handler)
  handler.class.rpc_descs.each do |path, _|
    Griffin.logger.debug("Handle #{path}")
  end
end

#run(sock, blocking: true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/griffin/server.rb', line 56

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

  @thread_pool = Griffin::ThreadPool.new(@worker_size) do |conn|
    @server.run(conn)
  end

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

#shutdown(reason = GRACEFUL_SHUTDOWN) ⇒ Object



70
71
72
# File 'lib/griffin/server.rb', line 70

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