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
-
.default_retryable_check(result) ⇒ Boolean
Default check for retryable errors (network/timeout errors).
-
.execute(options = {}) { ... } ⇒ Object
Retry a block with exponential backoff.
Class Method Details
.default_retryable_check(result) ⇒ Boolean
Default check for retryable errors (network/timeout errors)
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
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( = {}) max_retries = [:max_retries] || 3 backoff = [:initial_backoff] || 1 max_backoff = [:max_backoff] || 32 retryable_check = [:retryable_check] || method(:default_retryable_check) error_class = [: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 |