Class: OllamaAgent::Resilience::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/resilience/retry_policy.rb

Overview

Retryable exception list and HTTP status / backoff rules for RetryMiddleware.

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =
3
DEFAULT_BASE_DELAY =
2.0
RETRYABLE =
begin
  list = [Ollama::TimeoutError, Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET]
  list << SocketError if defined?(SocketError)
  list << Socket::ResolutionError if defined?(Socket::ResolutionError)
  list << Ollama::Error if defined?(Ollama::Error)
  list << Ollama::HTTPError if defined?(Ollama::HTTPError)
  list
rescue NameError
  [Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET]
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: DEFAULT_MAX_ATTEMPTS, base_delay: DEFAULT_BASE_DELAY) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



25
26
27
28
# File 'lib/ollama_agent/resilience/retry_policy.rb', line 25

def initialize(max_attempts: DEFAULT_MAX_ATTEMPTS, base_delay: DEFAULT_BASE_DELAY)
  @max_attempts = max_attempts.to_i
  @base_delay   = base_delay.to_f
end

Instance Attribute Details

#base_delayObject (readonly)

Returns the value of attribute base_delay.



23
24
25
# File 'lib/ollama_agent/resilience/retry_policy.rb', line 23

def base_delay
  @base_delay
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



23
24
25
# File 'lib/ollama_agent/resilience/retry_policy.rb', line 23

def max_attempts
  @max_attempts
end

Instance Method Details

#backoff(attempt) ⇒ Object



45
46
47
48
# File 'lib/ollama_agent/resilience/retry_policy.rb', line 45

def backoff(attempt)
  jitter = @base_delay.positive? ? rand * 0.5 : 0
  [(@base_delay * (2**(attempt - 1))) + jitter, 30.0].min
end

#retryable_http_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ollama_agent/resilience/retry_policy.rb', line 30

def retryable_http_error?(error)
  msg = error.message.to_s
  status = msg[/HTTP (\d+)/, 1].to_i
  return false if status.zero?

  if status == 429
    return false if msg.downcase.include?("weekly usage limit")
    return false if msg.downcase.include?("monthly usage limit")

    return true
  end

  status.between?(500, 599)
end