Class: Lens::Rails::BackoffLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/lens/rails/backoff_loop.rb

Overview

Supervised sleep/call loop for flush threads.

  • Rescues StandardError (not Exception — we must not swallow Interrupt / SystemExit, which would defeat the bounded at_exit flush).

  • On failure, doubles the sleep interval up to ‘max`. Resets to `base` on the next successful call.

  • Sets a thread name and enables report_on_exception so a truly fatal error is visible instead of silently killing telemetry.

Instance Method Summary collapse

Constructor Details

#initialize(base:, max: 60, name: "lens-loop") ⇒ BackoffLoop

Returns a new instance of BackoffLoop.



14
15
16
17
18
# File 'lib/lens/rails/backoff_loop.rb', line 14

def initialize(base:, max: 60, name: "lens-loop")
  @base = base
  @max = max
  @name = name
end

Instance Method Details

#start(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lens/rails/backoff_loop.rb', line 20

def start(&block)
  Thread.new do
    Thread.current.name = @name
    Thread.current.report_on_exception = true
    current = @base
    loop do
      sleep(current)
      begin
        block.call
        current = @base
      rescue => e
        warn "#{@name}: #{e.class}: #{e.message}"
        current = [current * 2, @max].min
      end
    end
  end
end