Class: AiClient::RetryMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_client/retry_middleware.rb

Overview

AiClient.use(

AiClient::RetryMiddleware.new(
  max_retries:  5,
  base_delay:   2,
  max_delay:    30
)

)

Instance Method Summary collapse

Constructor Details

#initialize(max_retries: 3, base_delay: 2, max_delay: 16) ⇒ RetryMiddleware

Initializes a new instance of RetryMiddleware.

Parameters:

  • max_retries (Integer) (defaults to: 3)

    The maximum number of retries to attempt (default: 3).

  • base_delay (Integer) (defaults to: 2)

    The base delay in seconds before retrying (default: 2).

  • max_delay (Integer) (defaults to: 16)

    The maximum delay in seconds between retries (default: 16).



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.

Parameters:

  • client (AiClient)

    The client instance that invokes the middleware.

  • next_middleware (Proc)

    The next middleware in the chain to call.

  • args (Array)

    Any additional arguments to pass to the next middleware.

Raises:

  • (StandardError)

    Reraise the error if max retries are exceeded.



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.message}")
      sleep(delay)
      retry
    else
      raise
    end
  end
end