Class: Ollama::Policies::Retry
- Defined in:
- lib/ollama/policies/retry.rb,
lib/ollama/policies/retry.rb,
lib/ollama/policies/retry/strategies/fixed.rb,
lib/ollama/policies/retry/strategies/jitter.rb,
lib/ollama/policies/retry/strategies/linear.rb,
lib/ollama/policies/retry/strategies/exponential.rb
Overview
(continued from the stub declaration above, now that Strategies exists)
Defined Under Namespace
Modules: Strategies
Instance Attribute Summary collapse
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#max_attempts ⇒ Object
readonly
Returns the value of attribute max_attempts.
-
#retryable_errors ⇒ Object
readonly
Returns the value of attribute retryable_errors.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#around(request, env, &block) ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#initialize(max_attempts: 3, strategy: :exponential, base_delay: 1.0, max_delay: 60.0, jitter: 0.1, retryable_errors: nil, hooks: {}) ⇒ Retry
constructor
rubocop:disable Metrics/ParameterLists.
Constructor Details
#initialize(max_attempts: 3, strategy: :exponential, base_delay: 1.0, max_delay: 60.0, jitter: 0.1, retryable_errors: nil, hooks: {}) ⇒ Retry
rubocop:disable Metrics/ParameterLists
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ollama/policies/retry.rb', line 37 def initialize( max_attempts: 3, strategy: :exponential, base_delay: 1.0, max_delay: 60.0, jitter: 0.1, retryable_errors: nil, hooks: {} ) super() @max_attempts = max_attempts @base_delay = base_delay @max_delay = max_delay @jitter = jitter @strategy = build_strategy(strategy) @retryable_errors = retryable_errors || default_retryable_errors @hooks = hooks end |
Instance Attribute Details
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
27 28 29 |
# File 'lib/ollama/policies/retry.rb', line 27 def hooks @hooks end |
#max_attempts ⇒ Object (readonly)
Returns the value of attribute max_attempts.
27 28 29 |
# File 'lib/ollama/policies/retry.rb', line 27 def max_attempts @max_attempts end |
#retryable_errors ⇒ Object (readonly)
Returns the value of attribute retryable_errors.
27 28 29 |
# File 'lib/ollama/policies/retry.rb', line 27 def retryable_errors @retryable_errors end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
27 28 29 |
# File 'lib/ollama/policies/retry.rb', line 27 def strategy @strategy end |
Instance Method Details
#around(request, env, &block) ⇒ Object
rubocop:enable Metrics/ParameterLists
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ollama/policies/retry.rb', line 57 def around(request, env, &block) attempt = 0 last_error = nil loop do attempt += 1 env[:attempt] = attempt begin return block.call(request, env) rescue StandardError => e last_error = e # Check if we should retry raise unless retryable?(e, env) raise if attempt >= @max_attempts # Execute before_retry hook @hooks[:before_retry]&.call(request, env, e, attempt) # Calculate delay delay = @strategy.delay(attempt, @base_delay, @max_delay, @jitter) # Sleep sleep(delay) if delay.positive? # Execute after_retry hook @hooks[:after_retry]&.call(request, env, e, attempt, delay) end end rescue StandardError => e # If we exhausted retries, raise the last error raise last_error || e end |