Class: Flipper::Cloud::Telemetry::BackoffPolicy
- Inherits:
-
Object
- Object
- Flipper::Cloud::Telemetry::BackoffPolicy
- Defined in:
- lib/flipper/cloud/telemetry/backoff_policy.rb
Constant Summary collapse
- MIN_TIMEOUT_MS =
Private: The default minimum timeout between intervals in milliseconds.
1_000
- MAX_TIMEOUT_MS =
Private: The default maximum timeout between intervals in milliseconds.
30_000
- MULTIPLIER =
Private: The value to multiply the current interval with for each retry attempt.
1.5
- RANDOMIZATION_FACTOR =
Private: The randomization factor to use to create a range around the retry interval.
0.5
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Private.
-
#max_timeout_ms ⇒ Object
readonly
Private.
-
#min_timeout_ms ⇒ Object
readonly
Private.
-
#multiplier ⇒ Object
readonly
Private.
-
#randomization_factor ⇒ Object
readonly
Private.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ BackoffPolicy
constructor
Public: Create new instance of backoff policy.
-
#next_interval ⇒ Object
Public: Returns the next backoff interval in milliseconds.
- #reset ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BackoffPolicy
Public: Create new instance of backoff policy.
options - The Hash of options.
:min_timeout_ms - The minimum backoff timeout.
:max_timeout_ms - The maximum backoff timeout.
:multiplier - The value to multiply the current interval with for each
retry attempt.
:randomization_factor - The randomization factor to use to create a range
around the retry interval.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 34 def initialize( = {}) @min_timeout_ms = .fetch(:min_timeout_ms) { ENV.fetch("FLIPPER_BACKOFF_MIN_TIMEOUT_MS", MIN_TIMEOUT_MS).to_i } @max_timeout_ms = .fetch(:max_timeout_ms) { ENV.fetch("FLIPPER_BACKOFF_MAX_TIMEOUT_MS", MAX_TIMEOUT_MS).to_i } @multiplier = .fetch(:multiplier) { ENV.fetch("FLIPPER_BACKOFF_MULTIPLIER", MULTIPLIER).to_f } @randomization_factor = .fetch(:randomization_factor) { ENV.fetch("FLIPPER_BACKOFF_RANDOMIZATION_FACTOR", RANDOMIZATION_FACTOR).to_f } unless @min_timeout_ms >= 0 raise ArgumentError, ":min_timeout_ms must be >= 0 but was #{@min_timeout_ms.inspect}" end unless @max_timeout_ms >= 0 raise ArgumentError, ":max_timeout_ms must be >= 0 but was #{@max_timeout_ms.inspect}" end unless @min_timeout_ms <= max_timeout_ms raise ArgumentError, ":min_timeout_ms (#{@min_timeout_ms.inspect}) must be <= :max_timeout_ms (#{@max_timeout_ms.inspect})" end @attempts = 0 end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Private
23 24 25 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 23 def attempts @attempts end |
#max_timeout_ms ⇒ Object (readonly)
Private
20 21 22 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 20 def max_timeout_ms @max_timeout_ms end |
#min_timeout_ms ⇒ Object (readonly)
Private
20 21 22 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 20 def min_timeout_ms @min_timeout_ms end |
#multiplier ⇒ Object (readonly)
Private
20 21 22 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 20 def multiplier @multiplier end |
#randomization_factor ⇒ Object (readonly)
Private
20 21 22 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 20 def randomization_factor @randomization_factor end |
Instance Method Details
#next_interval ⇒ Object
Public: Returns the next backoff interval in milliseconds.
64 65 66 67 68 69 70 71 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 64 def next_interval interval = @min_timeout_ms * (@multiplier**@attempts) interval = add_jitter(interval, @randomization_factor) @attempts += 1 [interval, @max_timeout_ms].min end |
#reset ⇒ Object
73 74 75 |
# File 'lib/flipper/cloud/telemetry/backoff_policy.rb', line 73 def reset @attempts = 0 end |