Class: OllamaAgent::Resilience::RetryMiddleware
- Inherits:
-
Object
- Object
- OllamaAgent::Resilience::RetryMiddleware
- Defined in:
- lib/ollama_agent/resilience/retry_middleware.rb
Overview
Wraps an Ollama::Client with exponential backoff retry for transient errors.
Constant Summary collapse
- DEFAULT_MAX_ATTEMPTS =
RetryPolicy::DEFAULT_MAX_ATTEMPTS
- DEFAULT_BASE_DELAY =
RetryPolicy::DEFAULT_BASE_DELAY
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#policy ⇒ Object
readonly
Returns the value of attribute policy.
Instance Method Summary collapse
-
#chat(**args) ⇒ Object
rubocop:disable Metrics/MethodLength – single rescue/retry loop.
-
#initialize(client:, max_attempts: DEFAULT_MAX_ATTEMPTS, hooks: nil, base_delay: DEFAULT_BASE_DELAY) ⇒ RetryMiddleware
constructor
A new instance of RetryMiddleware.
-
#list_model_names ⇒ Object
Delegates to the inner Ollama client (
/api/tags).
Constructor Details
#initialize(client:, max_attempts: DEFAULT_MAX_ATTEMPTS, hooks: nil, base_delay: DEFAULT_BASE_DELAY) ⇒ RetryMiddleware
Returns a new instance of RetryMiddleware.
15 16 17 18 19 |
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 15 def initialize(client:, max_attempts: DEFAULT_MAX_ATTEMPTS, hooks: nil, base_delay: DEFAULT_BASE_DELAY) @client = client @hooks = hooks @policy = RetryPolicy.new(max_attempts: max_attempts, base_delay: base_delay) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 13 def client @client end |
#policy ⇒ Object (readonly)
Returns the value of attribute policy.
13 14 15 |
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 13 def policy @policy end |
Instance Method Details
#chat(**args) ⇒ Object
rubocop:disable Metrics/MethodLength – single rescue/retry loop
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 22 def chat(**args) attempt = 0 begin @client.chat(**args) rescue *RetryPolicy::RETRYABLE => e raise if http_error_non_retryable?(e) attempt += 1 raise if attempt >= @policy.max_attempts delay = @policy.backoff(attempt) @hooks&.emit(:on_retry, { error: e, attempt: attempt, delay_ms: (delay * 1000).round }) sleep delay retry end end |
#list_model_names ⇒ Object
Delegates to the inner Ollama client (/api/tags).
41 42 43 |
# File 'lib/ollama_agent/resilience/retry_middleware.rb', line 41 def list_model_names @client.list_model_names end |