Class: Tinymon::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/tinymon/transport.rb

Overview

Batched HTTP transport. stdlib-only. A background thread flushes every 5 seconds; at_exit drains the queue on shutdown.

Constant Summary collapse

FLUSH_INTERVAL =

seconds

5.0
MAX_BATCH =
10
MAX_QUEUE =
30
REQUEST_TIMEOUT =

seconds

5

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, dsn) ⇒ Transport

Returns a new instance of Transport.



16
17
18
19
20
21
22
23
24
25
# File 'lib/tinymon/transport.rb', line 16

def initialize(endpoint, dsn)
  @endpoint = endpoint
  @dsn = dsn
  @queue = []
  @mutex = Mutex.new
  @stop = false
  @thread = Thread.new { run_loop }
  @thread.name = "tinymon-transport" if @thread.respond_to?(:name=)
  at_exit { shutdown }
end

Instance Method Details

#enqueue(event) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/tinymon/transport.rb', line 27

def enqueue(event)
  should_flush = false
  @mutex.synchronize do
    @queue.shift while @queue.length >= MAX_QUEUE
    @queue.push(event)
    should_flush = @queue.length >= MAX_BATCH
  end
  flush if should_flush
end

#flushObject



37
38
39
40
41
42
43
44
# File 'lib/tinymon/transport.rb', line 37

def flush
  batch = nil
  @mutex.synchronize do
    batch = @queue.shift(MAX_BATCH)
  end
  return if batch.nil? || batch.empty?
  batch.each { |event| send_one(event) }
end