Class: Cassanity::RetryStrategies::ExponentialBackoff
- Inherits:
-
RetryStrategy
- Object
- RetryStrategy
- Cassanity::RetryStrategies::ExponentialBackoff
- Defined in:
- lib/cassanity/retry_strategies/exponential_backoff.rb
Constant Summary collapse
- ForeverSentinel =
:forever
- SleepTimes =
Private: Taken from github.com/twitter/kestrel-client’s blocking client.
[[0] * 1, [0.01] * 2, [0.1] * 2, [0.5] * 2, [1.0] * 1].flatten
Instance Attribute Summary collapse
-
#retries ⇒ Object
readonly
Private: the maxmimum number of times to retry or -1 to try forever.
Instance Method Summary collapse
- #fail(attempts, error) ⇒ Object
-
#initialize(args = {}) ⇒ ExponentialBackoff
constructor
Public: Initialize the retry strategy.
-
#sleep_for_count(count) ⇒ Object
Private: sleep a randomized amount of time from the SleepTimes mostly-exponential distribution.
Methods inherited from RetryStrategy
Constructor Details
#initialize(args = {}) ⇒ ExponentialBackoff
Public: Initialize the retry strategy.
args - The Hash of options.
:retries - the maximum number of times to retry (default: forever)
19 20 21 22 |
# File 'lib/cassanity/retry_strategies/exponential_backoff.rb', line 19 def initialize(args = {}) # The default behavior is to retry forever. @retries = args[:retries] || ForeverSentinel end |
Instance Attribute Details
#retries ⇒ Object (readonly)
Private: the maxmimum number of times to retry or -1 to try forever.
13 14 15 |
# File 'lib/cassanity/retry_strategies/exponential_backoff.rb', line 13 def retries @retries end |
Instance Method Details
#fail(attempts, error) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/cassanity/retry_strategies/exponential_backoff.rb', line 24 def fail(attempts, error) if @retries != ForeverSentinel && attempts > @retries raise error end sleep_for_count(attempts) end |
#sleep_for_count(count) ⇒ Object
Private: sleep a randomized amount of time from the SleepTimes mostly-exponential distribution.
count - the index into the distribution to pull the base sleep time from
35 36 37 38 39 40 |
# File 'lib/cassanity/retry_strategies/exponential_backoff.rb', line 35 def sleep_for_count(count) base = SleepTimes[count] || SleepTimes.last time = ((rand * base) + base) / 2 sleep time if time > 0 end |