Class: ActiveHarness::Http::RetryPolicy
- Inherits:
-
Object
- Object
- ActiveHarness::Http::RetryPolicy
- Defined in:
- lib/active_harness/http/retry_policy.rb
Overview
Wraps a block with automatic retry on transient errors. Uses exponential backoff: delay doubles after each failed attempt.
Example:
RetryPolicy.new(max_attempts: 3, base_delay: 0.5).run do
http_client.post(...)
end
Custom errors:
RetryPolicy.new(errors: [MyTransientError]).run { ... }
Constant Summary collapse
- DEFAULT_ERRORS =
[ Errors::TimeoutError, Errors::RateLimitError, Errors::ProviderUnavailableError, Errors::ServerError ].freeze
Instance Method Summary collapse
-
#initialize(max_attempts: 3, base_delay: 1.0, errors: DEFAULT_ERRORS) ⇒ RetryPolicy
constructor
A new instance of RetryPolicy.
- #run ⇒ Object
Constructor Details
#initialize(max_attempts: 3, base_delay: 1.0, errors: DEFAULT_ERRORS) ⇒ RetryPolicy
Returns a new instance of RetryPolicy.
25 26 27 28 29 |
# File 'lib/active_harness/http/retry_policy.rb', line 25 def initialize(max_attempts: 3, base_delay: 1.0, errors: DEFAULT_ERRORS) @max_attempts = max_attempts @base_delay = base_delay @errors = errors end |
Instance Method Details
#run ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_harness/http/retry_policy.rb', line 33 def run attempt = 0 begin attempt += 1 yield rescue *@errors raise if attempt >= @max_attempts sleep(@base_delay * (2**(attempt - 1))) retry end end |