Class: Ollama::ApiKeyPool

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/api_key_pool.rb

Overview

Thread-safe immutable API key pool for rate-limit failover.

The pool never mutates the configured key list after construction. Request state is represented as a short-lived context hash so retry counters and rotation offsets remain isolated to the caller's execution path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys, concurrency_enabled: false) ⇒ ApiKeyPool

Returns a new instance of ApiKeyPool.

Parameters:

  • keys (Array<String>)

    API keys in configured priority order

  • concurrency_enabled (Boolean) (defaults to: false)

    whether initial keys should be distributed round-robin



14
15
16
17
18
19
# File 'lib/ollama/api_key_pool.rb', line 14

def initialize(keys, concurrency_enabled: false)
  @keys = keys.map(&:to_s).map(&:strip).reject(&:empty?).freeze
  @concurrency_enabled = concurrency_enabled
  @mutex = Mutex.new
  @next_index = 0
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



10
11
12
# File 'lib/ollama/api_key_pool.rb', line 10

def keys
  @keys
end

Instance Method Details

#empty?Boolean

Returns true when no keys are configured.

Returns:

  • (Boolean)

    true when no keys are configured



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

def empty?
  @keys.empty?
end

#key_for(context, offset: 0) ⇒ String?

Resolve a key from a request-local context and rotation offset.

Parameters:

  • context (Hash)
  • offset (Integer) (defaults to: 0)

Returns:

  • (String, nil)


43
44
45
46
47
# File 'lib/ollama/api_key_pool.rb', line 43

def key_for(context, offset: 0)
  return nil if empty?

  @keys[(context.fetch(:start_index) + offset) % size]
end

#request_contextHash

Build request-local rotation context.

Returns:

  • (Hash)

    isolated context for one logical request attempt



34
35
36
# File 'lib/ollama/api_key_pool.rb', line 34

def request_context
  { start_index: initial_index }
end

#sizeInteger

Returns number of configured keys.

Returns:

  • (Integer)

    number of configured keys



22
23
24
# File 'lib/ollama/api_key_pool.rb', line 22

def size
  @keys.size
end