Class: Hackle::EventDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hackle/events/event_dispatcher.rb

Constant Summary collapse

DEFAULT_DISPATCH_WORKER_SIZE =
2
DEFAULT_DISPATCH_QUEUE_CAPACITY =
50

Instance Method Summary collapse

Constructor Details

#initialize(config:, sdk_info:) ⇒ EventDispatcher

Returns a new instance of EventDispatcher.



9
10
11
12
13
14
15
16
17
18
# File 'lib/hackle/events/event_dispatcher.rb', line 9

def initialize(config:, sdk_info:)
  @logger = config.logger
  @client = HTTP.client(base_uri: config.event_uri)
  @headers = HTTP.sdk_headers(sdk_info: sdk_info)
  @dispatcher_executor = Concurrent::ThreadPoolExecutor.new(
    min_threads: DEFAULT_DISPATCH_WORKER_SIZE,
    max_threads: DEFAULT_DISPATCH_WORKER_SIZE,
    max_queue: DEFAULT_DISPATCH_QUEUE_CAPACITY
  )
end

Instance Method Details

#dispatch(events:) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/hackle/events/event_dispatcher.rb', line 20

def dispatch(events:)
  payload = create_payload(events: events)
  begin
    @dispatcher_executor.post { dispatch_payload(payload: payload) }
  rescue Concurrent::RejectedExecutionError
    @logger.warn { 'Dispatcher executor queue is full. Event dispatch rejected' }
  end
end

#shutdownObject



29
30
31
32
33
34
# File 'lib/hackle/events/event_dispatcher.rb', line 29

def shutdown
  @dispatcher_executor.shutdown
  unless @dispatcher_executor.wait_for_termination(10)
    @logger.warn { 'Failed to dispatch previously submitted events' }
  end
end