Class: Userlist::Retryable
- Inherits:
-
Object
- Object
- Userlist::Retryable
- Includes:
- Logging
- Defined in:
- lib/userlist/retryable.rb
Constant Summary collapse
- RETRIES =
10- DELAY =
100- MULTIPLIER =
2- MAX_DELAY =
10_000- DEFAULT_RETRY_CHECK =
lambda do |error| case error when Userlist::RequestError status = error.status status >= 500 || status == 429 when Userlist::TimeoutError true else false end end
Instance Method Summary collapse
- #attempt ⇒ Object
-
#initialize(retries: RETRIES, delay: DELAY, max_delay: MAX_DELAY, multiplier: MULTIPLIER, &retry_check) ⇒ Retryable
constructor
A new instance of Retryable.
- #retry?(value) ⇒ Boolean
Methods included from Logging
Constructor Details
#initialize(retries: RETRIES, delay: DELAY, max_delay: MAX_DELAY, multiplier: MULTIPLIER, &retry_check) ⇒ Retryable
Returns a new instance of Retryable.
22 23 24 25 26 27 28 |
# File 'lib/userlist/retryable.rb', line 22 def initialize(retries: RETRIES, delay: DELAY, max_delay: MAX_DELAY, multiplier: MULTIPLIER, &retry_check) @retries = retries @delay = delay @max_delay = max_delay @multiplier = multiplier @retry_check = retry_check || DEFAULT_RETRY_CHECK end |
Instance Method Details
#attempt ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/userlist/retryable.rb', line 34 def attempt (0..@retries).each do |attempt| if attempt.positive? milliseconds = delay(attempt) logger.debug "Retrying in #{milliseconds}ms, #{@retries - attempt} retries left" sleep(milliseconds / 1000.0) end return yield rescue Userlist::Error => e raise e unless retry?(e) end logger.debug 'Retries exhausted, giving up' nil end |
#retry?(value) ⇒ Boolean
30 31 32 |
# File 'lib/userlist/retryable.rb', line 30 def retry?(value) @retry_check.call(value) end |