Class: HydraulicBrake::AsyncSender

Inherits:
Object
  • Object
show all
Defined in:
lib/hydraulic_brake/async_sender.rb

Overview

In-memory, thread-safe storage for notices that haven’t yet been sent to Airbrake

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AsyncSender

Returns a new instance of AsyncSender.



8
9
10
11
12
13
14
# File 'lib/hydraulic_brake/async_sender.rb', line 8

def initialize(opts={})
  @sync_sender = opts[:sync_sender]
  @q = Queue.new
  @capacity = opts[:capacity] || 100
  @logger = opts
  @thread = nil
end

Instance Attribute Details

#sync_senderObject (readonly)

Returns the value of attribute sync_sender.



5
6
7
# File 'lib/hydraulic_brake/async_sender.rb', line 5

def sync_sender
  @sync_sender
end

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/hydraulic_brake/async_sender.rb', line 6

def thread
  @thread
end

Instance Method Details

#send_to_airbrake(notice) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hydraulic_brake/async_sender.rb', line 16

def send_to_airbrake(notice)
  return will_not_deliver(notice) if @q.length >= @capacity

  @q.push(notice)

  return if @thread && @thread.alive?

  @thread = Thread.new do
    while n = @q.pop
      @sync_sender.send_to_airbrake(n)
    end
  end
end