Class: Manceps::Backoff

Inherits:
Object
  • Object
show all
Defined in:
lib/manceps/backoff.rb

Overview

Exponential backoff calculator for retry logic.

Instance Method Summary collapse

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_delayObject



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

#resetObject



21
22
23
# File 'lib/manceps/backoff.rb', line 21

def reset
  @attempts = 0
end