Class: SkillBench::Clients::RetryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/clients/retry_handler.rb

Overview

Service object for retrying HTTP requests with exponential backoff. Retries on transient errors (429, 503). Raises permanent errors immediately. Returns the block result on success.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts:, base_delay:, block:) ⇒ RetryHandler

Returns a new instance of RetryHandler.

Parameters:

  • max_attempts (Integer)

    Maximum number of attempts.

  • base_delay (Numeric)

    Base delay before first retry.

  • block (Proc)

    The request block to execute.



31
32
33
34
35
# File 'lib/skill_bench/clients/retry_handler.rb', line 31

def initialize(max_attempts:, base_delay:, block:)
  @max_attempts = max_attempts
  @base_delay = base_delay
  @block = block
end

Class Method Details

.call(max_attempts: Constants::HttpClient::DEFAULT_MAX_RETRIES, base_delay: Constants::HttpClient::DEFAULT_RETRY_DELAY) { ... } ⇒ Object

Executes the given block with retry logic.

Parameters:

  • max_attempts (Integer) (defaults to: Constants::HttpClient::DEFAULT_MAX_RETRIES)

    Maximum number of attempts (default: 3).

  • base_delay (Numeric) (defaults to: Constants::HttpClient::DEFAULT_RETRY_DELAY)

    Base delay in seconds before first retry (doubles each attempt).

Yields:

  • The request block to execute.

Returns:

  • (Object)

    The block’s return value on success.

Raises:

  • (Faraday::Error)

    On non-retryable errors or after exhausting retries.

  • (ArgumentError)

    if no block is given or max_attempts < 1.



21
22
23
24
25
26
# File 'lib/skill_bench/clients/retry_handler.rb', line 21

def self.call(max_attempts: Constants::HttpClient::DEFAULT_MAX_RETRIES, base_delay: Constants::HttpClient::DEFAULT_RETRY_DELAY, &block)
  raise ArgumentError, 'RetryHandler requires a block' unless block
  raise ArgumentError, 'max_attempts must be >= 1' if max_attempts < 1

  new(max_attempts:, base_delay:, block:).call
end

Instance Method Details

#callObject

Executes the block with retry logic.

Returns:

  • (Object)

    The block’s return value on success.

Raises:

  • (Faraday::Error)

    On non-retryable errors or after exhausting retries.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/skill_bench/clients/retry_handler.rb', line 41

def call
  attempt = 0

  loop do
    attempt += 1
    return @block.call
  rescue Faraday::Error => e
    status = extract_status(e)
    raise e unless retryable?(status, attempt)

    delay = compute_delay(attempt)
    wait(delay)
  end
end