Class: Ollama::Policies::Retry

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • max_attempts (Integer) (defaults to: 3)

    Maximum number of retry attempts (default: 3)

  • strategy (Symbol, Class) (defaults to: :exponential)

    Backoff strategy: :exponential, :linear, :fixed, :jitter (default: :exponential)

  • base_delay (Float) (defaults to: 1.0)

    Base delay in seconds for backoff (default: 1.0)

  • max_delay (Float) (defaults to: 60.0)

    Maximum delay in seconds (default: 60.0)

  • jitter (Float) (defaults to: 0.1)

    Jitter factor 0.0-1.0 (default: 0.1)

  • retryable_errors (Array<Class>) (defaults to: nil)

    Exception classes to retry (default: standard retryable)

  • hooks (Hash) (defaults to: {})

    Optional hooks: :before_retry, :after_retry



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

#hooksObject (readonly)

Returns the value of attribute hooks.



27
28
29
# File 'lib/ollama/policies/retry.rb', line 27

def hooks
  @hooks
end

#max_attemptsObject (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_errorsObject (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

#strategyObject (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