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.



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

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)


49
50
51
52
53
# File 'lib/gruf/instrumentable_grpc_server.rb', line 49

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)


58
59
60
61
62
# File 'lib/gruf/instrumentable_grpc_server.rb', line 58

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

#notify(event) ⇒ Object

Notify the event listener of something interesting



40
41
42
43
44
# File 'lib/gruf/instrumentable_grpc_server.rb', line 40

def notify(event)
  return if @event_listener_proc.nil? || !@event_listener_proc.respond_to?(:call)

  @event_listener_proc.call(event)
end