Class: ElephantInTheRoom::TheOneApiSdk::RetryStrategy::ExponentialBackoff
- Inherits:
-
Object
- Object
- ElephantInTheRoom::TheOneApiSdk::RetryStrategy::ExponentialBackoff
- Includes:
- Constants
- Defined in:
- lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/exponential_backoff.rb
Overview
Retry strategy for exponential backoffs
This will rerun an HTTP call up to max_tries times. Each retry will be delayed. The delay will have an exponential backoff that makes each delay about twice as long as the previous delay. The delay will also have a random jitter added to avoid overloading the server from many clients retrying simultaneously.
Constant Summary
Constants included from Constants
Constants::EXPONENTIAL_BACKOFF_TIME_DIVISOR, Constants::MOVIE_PATH, Constants::QUOTE_PATH
Instance Method Summary collapse
-
#initialize(max_tries) ⇒ ExponentialBackoff
constructor
A new instance of ExponentialBackoff.
- #run(do_request) ⇒ Object
Constructor Details
#initialize(max_tries) ⇒ ExponentialBackoff
Returns a new instance of ExponentialBackoff.
16 17 18 |
# File 'lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/exponential_backoff.rb', line 16 def initialize(max_tries) @max_tries = max_tries end |
Instance Method Details
#run(do_request) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/exponential_backoff.rb', line 20 def run(do_request) tries = 0 begin tries += 1 do_request.call rescue ElephantInTheRoom::TheOneApiSdk::Errors::ServerError raise if tries == @max_tries jittered_sleep(tries) retry end end |