Class: Gruf::InstrumentableGrpcServer

Inherits:
GRPC::RpcServer
  • Object
show all
Defined in:
lib/gruf/instrumentable_grpc_server.rb

Overview

A subclass of GRPC::RpcServer that can be used for enhanced monitoring of thread pool. Note that since we are reaching into the internals of GRPC::RpcServer, we need to watch the evolution of that class.

Instance Method Summary collapse

Constructor Details

#initialize(pool_size: DEFAULT_POOL_SIZE, max_waiting_requests: DEFAULT_MAX_WAITING_REQUESTS, poll_period: DEFAULT_POLL_PERIOD, pool_keep_alive: Pool::DEFAULT_KEEP_ALIVE, connect_md_proc: nil, server_args: {}, interceptors: [], event_listener_proc: nil) ⇒ InstrumentableGrpcServer

Add an event_listener_proc that, if supplied, will be called when interesting events happen in the server.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gruf/instrumentable_grpc_server.rb', line 12

def initialize(pool_size: DEFAULT_POOL_SIZE,
               max_waiting_requests: DEFAULT_MAX_WAITING_REQUESTS,
               poll_period: DEFAULT_POLL_PERIOD,
               pool_keep_alive: Pool::DEFAULT_KEEP_ALIVE,
               connect_md_proc: nil,
               server_args: {},
               interceptors: [],
               event_listener_proc: nil)
  # Call the base class initializer
  super(
    pool_size: pool_size,
    max_waiting_requests: max_waiting_requests,
    poll_period: poll_period,
    pool_keep_alive: pool_keep_alive,
    connect_md_proc: connect_md_proc,
    server_args: server_args,
    interceptors: interceptors
  )

  # Save event listener for later
  @event_listener_proc = event_listener_proc
end

Instance Method Details

#available?(an_rpc) ⇒ Boolean

Hook into the thread pool availability check for monitoring

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/gruf/instrumentable_grpc_server.rb', line 46

def available?(an_rpc)
  super.tap do |obj|
    notify(:thread_pool_exhausted) unless obj
  end
end

#implemented?(an_rpc) ⇒ Boolean

Hook into the method implementation check for monitoring

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/gruf/instrumentable_grpc_server.rb', line 55

def implemented?(an_rpc)
  super.tap do |obj|
    notify(:unimplemented) unless obj
  end
end

#notify(event) ⇒ Object

Notify the event listener of something interesting



38
39
40
41
# File 'lib/gruf/instrumentable_grpc_server.rb', line 38

def notify(event)
  return unless @event_listener_proc && @event_listener_proc.respond_to?(:call)
  @event_listener_proc.call(event)
end