Module: Glancer::Utils::RateLimitRetry

Defined in:
lib/glancer/utils/rate_limit_retry.rb

Constant Summary collapse

RATE_LIMIT_PATTERNS =
[
  /rate.?limit/i,
  /quota.?exceed/i,
  /exceeded.?your.?current.?quota/i,
  /too.?many.?request/i,
  /resource.?exhausted/i,
  /\b429\b/
].freeze
RETRY_AFTER_PATTERN =
/retry.?in\s+([0-9]+(?:\.[0-9]+)?)\s*s/i

Class Method Summary collapse

Class Method Details

.with_retry(context:, max_retries: nil, base_delay: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/glancer/utils/rate_limit_retry.rb', line 17

def self.with_retry(context:, max_retries: nil, base_delay: nil)
  max_retries ||= Glancer.configuration.max_llm_retries
  base_delay  ||= Glancer.configuration.llm_retry_delay
  attempt = 0

  begin
    yield
  rescue StandardError => e
    raise unless rate_limit_error?(e) && attempt < max_retries

    attempt += 1
    delay = parse_retry_after(e.message) || (base_delay * (2**(attempt - 1)))
    Glancer::Utils::Logger.warn(
      context,
      "Rate limit hit (attempt #{attempt}/#{max_retries}). Retrying in #{delay.ceil}s..."
    )
    sleep(delay)
    retry
  end
end