Class: Tinymon::Transport

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

Overview

HTTP transport. stdlib-only. Sends each event immediately on a background worker thread; events are only queued when the network fails, and that retry queue is drained with exponential backoff. This matches how production error SDKs behave — the error lands in the dashboard now, not on a 5-second timer. at_exit drains anything still pending on shutdown.

Constant Summary collapse

MAX_QUEUE =

cap on retry buffer (dead-network memory guard)

30
REQUEST_TIMEOUT =

seconds

5
BACKOFF_START =

seconds

1.0
BACKOFF_MAX =

seconds

30.0

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, dsn) ⇒ Transport

Returns a new instance of Transport.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tinymon/transport.rb', line 19

def initialize(endpoint, dsn)
  @endpoint = endpoint
  @dsn = dsn
  @pending = []         # fresh events — sent immediately
  @retry = []           # failed events — retried with backoff
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @backoff = BACKOFF_START
  @stop = false
  @thread = Thread.new { run_loop }
  @thread.name = "tinymon-transport" if @thread.respond_to?(:name=)
  at_exit { shutdown }
end

Instance Method Details

#flush(timeout = 2.0) ⇒ Object

Block until pending + retry events are sent, or timeout elapses.



42
43
44
45
46
47
48
49
50
51
# File 'lib/tinymon/transport.rb', line 42

def flush(timeout = 2.0)
  deadline = monotonic + timeout
  loop do
    empty = @mutex.synchronize { @pending.empty? && @retry.empty? }
    return if empty
    break if monotonic >= deadline
    @mutex.synchronize { @cond.signal }
    sleep(0.02)
  end
end

#send_event(event) ⇒ Object

Queue for immediate send on the worker thread (non-blocking).



34
35
36
37
38
39
# File 'lib/tinymon/transport.rb', line 34

def send_event(event)
  @mutex.synchronize do
    @pending.push(event)
    @cond.signal
  end
end