Class: AiClient::RetryMiddleware
- Inherits:
-
Object
- Object
- AiClient::RetryMiddleware
- Defined in:
- lib/ai_client/retry_middleware.rb
Overview
Instance Method Summary collapse
-
#call(client, next_middleware, *args) ⇒ Object
Calls the next middleware, retrying on specific errors.
-
#initialize(max_retries: 3, base_delay: 2, max_delay: 16) ⇒ RetryMiddleware
constructor
Initializes a new instance of RetryMiddleware.
Constructor Details
#initialize(max_retries: 3, base_delay: 2, max_delay: 16) ⇒ RetryMiddleware
Initializes a new instance of RetryMiddleware.
21 22 23 24 25 |
# File 'lib/ai_client/retry_middleware.rb', line 21 def initialize(max_retries: 3, base_delay: 2, max_delay: 16) @max_retries = max_retries @base_delay = base_delay @max_delay = max_delay end |
Instance Method Details
#call(client, next_middleware, *args) ⇒ Object
Calls the next middleware, retrying on specific errors.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ai_client/retry_middleware.rb', line 35 def call(client, next_middleware, *args) retries = 0 begin next_middleware.call rescue OmniAI::RateLimitError, OmniAI::NetworkError => e if retries < @max_retries retries += 1 delay = [@base_delay * (2 ** (retries - 1)), @max_delay].min client.logger.warn("Retrying in #{delay} seconds due to error: #{e.}") sleep(delay) retry else raise end end end |