Module: Ace::Review::Atoms::RetryWithBackoff

Defined in:
lib/ace/review/atoms/retry_with_backoff.rb

Overview

Pure function for retrying operations with exponential backoff

This atom provides a reusable retry mechanism with exponential backoff for operations that may experience transient failures (network issues, timeouts, temporary unavailability).

Class Method Summary collapse

Class Method Details

.default_retryable_check(result) ⇒ Boolean

Default check for retryable errors (network/timeout errors)

Parameters:

  • result (Hash)

    Result from operation (should have :stderr or :error key)

Returns:

  • (Boolean)

    true if error is retryable



63
64
65
66
67
68
69
70
71
# File 'lib/ace/review/atoms/retry_with_backoff.rb', line 63

def self.default_retryable_check(result)
  error_msg = (result[:stderr] || result[:error]).to_s.downcase

  # Network-related errors are retryable
  error_msg.include?("timeout") ||
    error_msg.include?("connection") ||
    error_msg.include?("network") ||
    error_msg.include?("temporary failure")
end

.execute(options = {}) { ... } ⇒ Object

Retry a block with exponential backoff

Parameters:

  • options (Hash) (defaults to: {})

    Retry options

Options Hash (options):

  • :max_retries (Integer)

    Maximum retry attempts (default: 3)

  • :initial_backoff (Integer)

    Initial backoff in seconds (default: 1)

  • :max_backoff (Integer)

    Maximum backoff in seconds (default: 32)

  • :retryable_check (Proc)

    Custom proc to check if error is retryable. Receives result hash, should return boolean. Defaults to network error check.

  • :error_class (Class)

    Exception class to raise on retry exhaustion (default: Ace::Review::Errors::GhNetworkError)

Yields:

  • Block to retry - should return a hash with :success and :stderr keys

Returns:

  • Result from successful execution

Raises:

  • (error_class)

    if all retries exhausted



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ace/review/atoms/retry_with_backoff.rb', line 25

def self.execute(options = {})
  max_retries = options[:max_retries] || 3
  backoff = options[:initial_backoff] || 1
  max_backoff = options[:max_backoff] || 32
  retryable_check = options[:retryable_check] || method(:default_retryable_check)
  error_class = options[:error_class] || Ace::Review::Errors::GhNetworkError
  attempt = 0

  loop do
    result = yield

    # Success - return result
    return result if result[:success]

    # Check if error is retryable using provided check or default
    unless retryable_check.call(result)
      return result
    end

    # Increment attempt
    attempt += 1

    # Exhausted retries
    if attempt >= max_retries
      error_msg = result[:stderr] || result[:error] || "Unknown error"
      raise error_class, "Operation failed after #{max_retries} retries: #{error_msg}"
    end

    # Wait before retry with exponential backoff, capped at max_backoff
    sleep(backoff)
    backoff = [backoff * 2, max_backoff].min
  end
end