Class: Zizq::Backoff

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

Overview

Encapsulates exponential backoff state for retry loops.

Each call to ‘wait` sleeps for the current duration and then advances to the next interval. Call `reset` to return to the initial wait time after a successful operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_wait:, max_wait:, multiplier:) ⇒ Backoff

Returns a new instance of Backoff.



22
23
24
25
26
27
# File 'lib/zizq/backoff.rb', line 22

def initialize(min_wait:, max_wait:, multiplier:)
  @min_wait = min_wait.to_f
  @max_wait = max_wait.to_f
  @multiplier = multiplier.to_f
  @current = @min_wait #: Float
end

Instance Attribute Details

#max_waitObject (readonly)

: Float



15
16
17
# File 'lib/zizq/backoff.rb', line 15

def max_wait
  @max_wait
end

#min_waitObject (readonly)

: Float



14
15
16
# File 'lib/zizq/backoff.rb', line 14

def min_wait
  @min_wait
end

#multiplierObject (readonly)

: Float



16
17
18
# File 'lib/zizq/backoff.rb', line 16

def multiplier
  @multiplier
end

Instance Method Details

#durationObject

Returns the current backoff duration without advancing.



30
31
32
# File 'lib/zizq/backoff.rb', line 30

def duration #: () -> Float
  @current
end

#freshObject

Returns a new Backoff with the same configuration but reset state.



46
47
48
# File 'lib/zizq/backoff.rb', line 46

def fresh #: () -> Backoff
  self.class.new(min_wait: @min_wait, max_wait: @max_wait, multiplier: @multiplier)
end

#resetObject

Resets the backoff to the initial min_wait.



41
42
43
# File 'lib/zizq/backoff.rb', line 41

def reset #: () -> void
  @current = @min_wait
end

#waitObject

Sleeps for the current backoff duration, then advances to the next.



35
36
37
38
# File 'lib/zizq/backoff.rb', line 35

def wait #: () -> void
  sleep @current
  @current = [@current * @multiplier, @max_wait].min
end