Class: GrpcServer::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_server/engine.rb

Constant Summary collapse

DEFAULT_HOST =
"0.0.0.0"
DEFAULT_PORT =
50051
DEFAULT_THREADS =
30
DEFAULT_ENV =
"unknown"
DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS =
[
  "TERM",
  "INT",
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, threads: DEFAULT_THREADS, env: DEFAULT_ENV, interceptors: [], signals: DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS, logger: nil) ⇒ Engine

Returns a new instance of Engine.

Parameters:

  • host (String) (defaults to: DEFAULT_HOST)
  • port (Integer) (defaults to: DEFAULT_PORT)
  • threads (Integer) (defaults to: DEFAULT_THREADS)

    the size of the thread pool the server uses to run its threads. No more concurrent requests can be made than the size of the thread pool

  • env (String) (defaults to: DEFAULT_ENV)
  • interceptors (Array<GRPC::ServerInterceptor>) (defaults to: [])

    An array of GRPC::ServerInterceptor objects that will be used for intercepting server handlers to provide extra functionality.

  • signals (Array<String>) (defaults to: DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS)

    List of String representing signals that the user would like to send to the server for graceful shutdown

  • logger (Logger) (defaults to: nil)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grpc_server/engine.rb', line 28

def initialize(host:         DEFAULT_HOST,
               port:         DEFAULT_PORT,
               threads:      DEFAULT_THREADS,
               env:          DEFAULT_ENV,
               interceptors: [],
               signals:      DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS,
               logger:       nil)
  @host    = host
  @port    = port
  @threads = threads
  @env     = env
  @logger  = logger || Logger.new($stdout)
  @signals = signals

  @server = GRPC::RpcServer.new(
    pool_size:    @threads,
    interceptors: interceptors,
  )
  @server.add_http2_port("#{@host}:#{@port}", :this_port_is_insecure)

  # Setup Health Cheker.
  # cf. https://github.com/grpc/grpc/blob/master/doc/health-checking.md
  @server.handle(health_checker)
  register_service_to_health_checker!(service: "")
end

Instance Method Details

#runObject



60
61
62
63
64
65
66
67
68
# File 'lib/grpc_server/engine.rb', line 60

def run
  @logger.info("gRPC server starting...")
  @logger.info("* Max threads: #{@threads}")
  @logger.info("* Environment: #{@env}")
  @logger.info("* Listening on tcp://#{@host}:#{@port}")
  @logger.info("Use Ctrl-C to stop")
  @server.run_till_terminated_or_interrupted(@signals)
  @logger.info("Exiting")
end

#set_handler(service) ⇒ Object

Parameters:

  • service (GRPC::GenericService)


55
56
57
58
# File 'lib/grpc_server/engine.rb', line 55

def set_handler(service)
  @server.handle(service)
  register_service_to_health_checker!(service: service.service_name)
end

#stopObject



70
71
72
# File 'lib/grpc_server/engine.rb', line 70

def stop
  @server.stop
end