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.

Constant Summary collapse

RETRYABLE_STATUSES =
[429, 503].freeze
MAX_DELAY =

Maximum delay cap in seconds

30

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.



34
35
36
37
38
# File 'lib/skill_bench/clients/retry_handler.rb', line 34

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

Class Method Details

.call(max_attempts: 3, base_delay: 1) { ... } ⇒ Object

Executes the given block with retry logic.

Parameters:

  • max_attempts (Integer) (defaults to: 3)

    Maximum number of attempts (default: 3).

  • base_delay (Numeric) (defaults to: 1)

    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.



24
25
26
27
28
29
# File 'lib/skill_bench/clients/retry_handler.rb', line 24

def self.call(max_attempts: 3, base_delay: 1, &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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/skill_bench/clients/retry_handler.rb', line 44

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