Class: Zizq::Backoff
- Inherits:
-
Object
- Object
- Zizq::Backoff
- 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
-
#max_wait ⇒ Object
readonly
: Float.
-
#min_wait ⇒ Object
readonly
: Float.
-
#multiplier ⇒ Object
readonly
: Float.
Instance Method Summary collapse
-
#duration ⇒ Object
Returns the current backoff duration without advancing.
-
#fresh ⇒ Object
Returns a new Backoff with the same configuration but reset state.
-
#initialize(min_wait:, max_wait:, multiplier:) ⇒ Backoff
constructor
A new instance of Backoff.
-
#reset ⇒ Object
Resets the backoff to the initial min_wait.
-
#wait ⇒ Object
Sleeps for the current backoff duration, then advances to the next.
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_wait ⇒ Object (readonly)
: Float
15 16 17 |
# File 'lib/zizq/backoff.rb', line 15 def max_wait @max_wait end |
#min_wait ⇒ Object (readonly)
: Float
14 15 16 |
# File 'lib/zizq/backoff.rb', line 14 def min_wait @min_wait end |
#multiplier ⇒ Object (readonly)
: Float
16 17 18 |
# File 'lib/zizq/backoff.rb', line 16 def multiplier @multiplier end |
Instance Method Details
#duration ⇒ Object
Returns the current backoff duration without advancing.
30 31 32 |
# File 'lib/zizq/backoff.rb', line 30 def duration #: () -> Float @current end |
#fresh ⇒ Object
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 |
#reset ⇒ Object
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 |
#wait ⇒ Object
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 |