Class: Ollama::ApiKeyPool
- Inherits:
-
Object
- Object
- Ollama::ApiKeyPool
- 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
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
True when no keys are configured.
-
#initialize(keys, concurrency_enabled: false) ⇒ ApiKeyPool
constructor
A new instance of ApiKeyPool.
-
#key_for(context, offset: 0) ⇒ String?
Resolve a key from a request-local context and rotation offset.
-
#request_context ⇒ Hash
Build request-local rotation context.
-
#size ⇒ Integer
Number of configured keys.
Constructor Details
#initialize(keys, concurrency_enabled: false) ⇒ ApiKeyPool
Returns a new instance of ApiKeyPool.
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
#keys ⇒ Object (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.
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.
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_context ⇒ Hash
Build request-local rotation context.
34 35 36 |
# File 'lib/ollama/api_key_pool.rb', line 34 def request_context { start_index: initial_index } end |
#size ⇒ Integer
Returns number of configured keys.
22 23 24 |
# File 'lib/ollama/api_key_pool.rb', line 22 def size @keys.size end |