Class: Flare::BackoffPolicy
- Inherits:
-
Object
- Object
- Flare::BackoffPolicy
- Defined in:
- lib/flare/backoff_policy.rb
Overview
Exponential backoff with jitter for retry logic. Based on Flipper’s implementation.
Constant Summary collapse
- MIN_TIMEOUT_MS =
Default minimum timeout between intervals in milliseconds
1_000- MAX_TIMEOUT_MS =
Default maximum timeout between intervals in milliseconds
30_000- MULTIPLIER =
Value to multiply the current interval with for each retry attempt
1.5- RANDOMIZATION_FACTOR =
Randomization factor to create a range around the retry interval
0.5
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#max_timeout_ms ⇒ Object
readonly
Returns the value of attribute max_timeout_ms.
-
#min_timeout_ms ⇒ Object
readonly
Returns the value of attribute min_timeout_ms.
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
-
#randomization_factor ⇒ Object
readonly
Returns the value of attribute randomization_factor.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ BackoffPolicy
constructor
A new instance of BackoffPolicy.
-
#next_interval ⇒ Object
Returns the next backoff interval in milliseconds.
- #reset ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BackoffPolicy
Returns a new instance of BackoffPolicy.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/flare/backoff_policy.rb', line 22 def initialize( = {}) @min_timeout_ms = .fetch(:min_timeout_ms) { ENV.fetch("FLARE_BACKOFF_MIN_TIMEOUT_MS", MIN_TIMEOUT_MS).to_i } @max_timeout_ms = .fetch(:max_timeout_ms) { ENV.fetch("FLARE_BACKOFF_MAX_TIMEOUT_MS", MAX_TIMEOUT_MS).to_i } @multiplier = .fetch(:multiplier) { ENV.fetch("FLARE_BACKOFF_MULTIPLIER", MULTIPLIER).to_f } @randomization_factor = .fetch(:randomization_factor) { ENV.fetch("FLARE_BACKOFF_RANDOMIZATION_FACTOR", RANDOMIZATION_FACTOR).to_f } validate! @attempts = 0 end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
20 21 22 |
# File 'lib/flare/backoff_policy.rb', line 20 def attempts @attempts end |
#max_timeout_ms ⇒ Object (readonly)
Returns the value of attribute max_timeout_ms.
19 20 21 |
# File 'lib/flare/backoff_policy.rb', line 19 def max_timeout_ms @max_timeout_ms end |
#min_timeout_ms ⇒ Object (readonly)
Returns the value of attribute min_timeout_ms.
19 20 21 |
# File 'lib/flare/backoff_policy.rb', line 19 def min_timeout_ms @min_timeout_ms end |
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
19 20 21 |
# File 'lib/flare/backoff_policy.rb', line 19 def multiplier @multiplier end |
#randomization_factor ⇒ Object (readonly)
Returns the value of attribute randomization_factor.
19 20 21 |
# File 'lib/flare/backoff_policy.rb', line 19 def randomization_factor @randomization_factor end |
Instance Method Details
#next_interval ⇒ Object
Returns the next backoff interval in milliseconds.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/flare/backoff_policy.rb', line 41 def next_interval interval = @min_timeout_ms * (@multiplier**@attempts) interval = add_jitter(interval, @randomization_factor) @attempts += 1 # Cap the interval to the max timeout result = [interval, @max_timeout_ms].min # Add small jitter even when maxed out result == @max_timeout_ms ? add_jitter(result, 0.05) : result end |
#reset ⇒ Object
53 54 55 |
# File 'lib/flare/backoff_policy.rb', line 53 def reset @attempts = 0 end |