Class: JetstreamBridge::BackoffStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/consumer/message_processor.rb

Overview

Simple exponential backoff strategy for transient failures.

Constant Summary collapse

TRANSIENT_ERRORS =
[Timeout::Error, IOError].freeze
MAX_EXPONENT =
6
MAX_DELAY =
60
MIN_DELAY =
1

Instance Method Summary collapse

Instance Method Details

#delay(deliveries, error) ⇒ Object

Returns a bounded delay in seconds



36
37
38
39
40
41
# File 'lib/jetstream_bridge/consumer/message_processor.rb', line 36

def delay(deliveries, error)
  base = transient?(error) ? 0.5 : 2.0
  power = [deliveries - 1, MAX_EXPONENT].min
  raw = (base * (2**power)).to_i
  raw.clamp(MIN_DELAY, MAX_DELAY)
end