Class: Desiru::Jobs::RetryStrategies::ExponentialBackoff
- Inherits:
-
Object
- Object
- Desiru::Jobs::RetryStrategies::ExponentialBackoff
- Defined in:
- lib/desiru/jobs/retry_strategies.rb
Overview
Exponential backoff with jitter
Instance Attribute Summary collapse
-
#base_delay ⇒ Object
readonly
Returns the value of attribute base_delay.
-
#jitter ⇒ Object
readonly
Returns the value of attribute jitter.
-
#max_delay ⇒ Object
readonly
Returns the value of attribute max_delay.
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
Instance Method Summary collapse
-
#delay_for(retry_count) ⇒ Object
Calculate delay for the given retry attempt.
-
#initialize(base_delay: 1, max_delay: 300, multiplier: 2, jitter: true) ⇒ ExponentialBackoff
constructor
A new instance of ExponentialBackoff.
Constructor Details
#initialize(base_delay: 1, max_delay: 300, multiplier: 2, jitter: true) ⇒ ExponentialBackoff
Returns a new instance of ExponentialBackoff.
11 12 13 14 15 16 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 11 def initialize(base_delay: 1, max_delay: 300, multiplier: 2, jitter: true) @base_delay = base_delay @max_delay = max_delay @multiplier = multiplier @jitter = jitter end |
Instance Attribute Details
#base_delay ⇒ Object (readonly)
Returns the value of attribute base_delay.
9 10 11 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 9 def base_delay @base_delay end |
#jitter ⇒ Object (readonly)
Returns the value of attribute jitter.
9 10 11 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 9 def jitter @jitter end |
#max_delay ⇒ Object (readonly)
Returns the value of attribute max_delay.
9 10 11 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 9 def max_delay @max_delay end |
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
9 10 11 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 9 def multiplier @multiplier end |
Instance Method Details
#delay_for(retry_count) ⇒ Object
Calculate delay for the given retry attempt
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/desiru/jobs/retry_strategies.rb', line 19 def delay_for(retry_count) delay = [base_delay * (multiplier**retry_count), max_delay].min if jitter # Add random jitter (±25%) to prevent thundering herd jitter_amount = delay * 0.25 jittered_delay = delay + ((rand * 2 * jitter_amount) - jitter_amount) # Ensure we don't exceed max_delay even with jitter [jittered_delay, max_delay].min else delay end end |