Class: Manceps::Backoff
- Inherits:
-
Object
- Object
- Manceps::Backoff
- Defined in:
- lib/manceps/backoff.rb
Overview
Exponential backoff calculator for retry logic.
Instance Method Summary collapse
-
#initialize(base: 1, max: 30, multiplier: 2, jitter: true) ⇒ Backoff
constructor
A new instance of Backoff.
- #next_delay ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(base: 1, max: 30, multiplier: 2, jitter: true) ⇒ Backoff
Returns a new instance of Backoff.
6 7 8 9 10 11 12 |
# File 'lib/manceps/backoff.rb', line 6 def initialize(base: 1, max: 30, multiplier: 2, jitter: true) @base = base @max = max @multiplier = multiplier @jitter = jitter @attempts = 0 end |
Instance Method Details
#next_delay ⇒ Object
14 15 16 17 18 19 |
# File 'lib/manceps/backoff.rb', line 14 def next_delay delay = [@base * (@multiplier**@attempts), @max].min delay *= rand(0.5..1.0) if @jitter @attempts += 1 delay end |
#reset ⇒ Object
21 22 23 |
# File 'lib/manceps/backoff.rb', line 21 def reset @attempts = 0 end |