Class: Raif::Utils::TransientRetry
- Inherits:
-
Object
- Object
- Raif::Utils::TransientRetry
- Defined in:
- lib/raif/utils/transient_retry.rb
Overview
Retries a block on transient errors using exponential backoff.
Single source of truth for "retry the HTTP call on a network blip" across Raif's synchronous (Raif::Llm#perform_model_completion!) and batch (Raif::Concerns::Llms::*::BatchInference) paths.
Defaults to Raif.config.llm_request_max_retries and Raif.config.llm_request_retriable_exceptions so retry behavior moves together when hosts tune those.
Constant Summary collapse
- DEFAULT_BASE_DELAY =
3- DEFAULT_MAX_DELAY =
30
Class Method Summary collapse
-
.call(label:, max_retries: nil, retriable_exceptions: nil, base_delay: DEFAULT_BASE_DELAY, max_delay: DEFAULT_MAX_DELAY, on_retry: nil) { ... } ⇒ Object
Whatever the block returns on its successful attempt.
Class Method Details
.call(label:, max_retries: nil, retriable_exceptions: nil, base_delay: DEFAULT_BASE_DELAY, max_delay: DEFAULT_MAX_DELAY, on_retry: nil) { ... } ⇒ Object
Returns whatever the block returns on its successful attempt.
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 58 59 60 |
# File 'lib/raif/utils/transient_retry.rb', line 33 def self.call(label:, max_retries: nil, retriable_exceptions: nil, base_delay: DEFAULT_BASE_DELAY, max_delay: DEFAULT_MAX_DELAY, on_retry: nil) max_retries ||= Raif.config.llm_request_max_retries retriable_exceptions ||= Raif.config.llm_request_retriable_exceptions retriable_exceptions = Array(retriable_exceptions) attempt = 0 begin yield rescue *retriable_exceptions => e attempt += 1 if attempt <= max_retries delay = [base_delay * (2**(attempt - 1)), max_delay].min on_retry&.call(e, attempt, max_retries, delay) Raif.logger.warn( "Raif::Utils::TransientRetry[#{label}]: retry #{attempt}/#{max_retries} " \ "after #{e.class}: #{e.}. Sleeping #{delay}s." ) sleep_for(delay) retry end Raif.logger.error( "Raif::Utils::TransientRetry[#{label}]: exhausted #{max_retries} retries. " \ "Last error: #{e.class}: #{e.}" ) raise end end |