Class: Interceptors::RetryInterceptor
- Inherits:
-
Interceptor
- Object
- Interceptor
- Interceptors::RetryInterceptor
- Defined in:
- lib/interceptors/retry_interceptor.rb
Instance Method Summary collapse
- #around(ctx) ⇒ Object
-
#initialize(tries: 3, on: [StandardError], base_delay: 0.05, max_delay: 0.5) ⇒ RetryInterceptor
constructor
A new instance of RetryInterceptor.
Methods inherited from Interceptor
Constructor Details
#initialize(tries: 3, on: [StandardError], base_delay: 0.05, max_delay: 0.5) ⇒ RetryInterceptor
Returns a new instance of RetryInterceptor.
5 6 7 8 9 10 11 12 |
# File 'lib/interceptors/retry_interceptor.rb', line 5 def initialize(tries: 3, on: [StandardError], base_delay: 0.05, max_delay: 0.5) raise ArgumentError, "tries must be >= 1" unless tries.to_i >= 1 @tries = tries.to_i @exceptions = Array(on) @base_delay = base_delay.to_f @max_delay = max_delay.to_f end |
Instance Method Details
#around(ctx) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/interceptors/retry_interceptor.rb', line 14 def around(ctx) attempt = 0 begin attempt += 1 yield ctx rescue => e raise unless @exceptions.any? { |klass| e.is_a?(klass) } return Result.err(e) if attempt >= @tries Kernel.sleep(delay_for_attempt(attempt)) retry end end |