Class: OllamaAgent::Runtime::FencingAllocator
- Inherits:
-
Object
- Object
- OllamaAgent::Runtime::FencingAllocator
- Defined in:
- lib/ollama_agent/runtime/fencing_allocator.rb
Overview
Monotonic fencing integers per scope in runtime.db (UPSERT increment).
Constant Summary collapse
- UPSERT_SQL =
"INSERT INTO fencing_tokens (scope, last_token) VALUES (?, 1) " \ "ON CONFLICT(scope) DO UPDATE SET last_token = last_token + 1"
Instance Method Summary collapse
-
#allocate(scope:) ⇒ Integer
New token for
scope. -
#allocate_joining(scope:) ⇒ Integer
Like #allocate but assumes the caller already holds an open transaction(:immediate) on @db.
-
#initialize(db) ⇒ FencingAllocator
constructor
A new instance of FencingAllocator.
Constructor Details
#initialize(db) ⇒ FencingAllocator
Returns a new instance of FencingAllocator.
13 14 15 |
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 13 def initialize(db) @db = db end |
Instance Method Details
#allocate(scope:) ⇒ Integer
Returns new token for scope.
18 19 20 |
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 18 def allocate(scope:) @db.transaction(:immediate) { allocate_joining(scope: scope) } end |
#allocate_joining(scope:) ⇒ Integer
Like #allocate but assumes the caller already holds an open transaction(:immediate) on @db.
24 25 26 27 |
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 24 def allocate_joining(scope:) @db.execute(UPSERT_SQL, [scope]) @db.get_first_value("SELECT last_token FROM fencing_tokens WHERE scope = ?", [scope]).to_i end |