Class: Kaal::Backend::MemoryAdapter
- Includes:
- DispatchLogging
- Defined in:
- lib/kaal/backend/memory_adapter.rb
Overview
In-memory backend adapter using Mutex and Hash.
This adapter stores locks in memory with TTL tracking. Locks are stored with an expiration time and automatically considered released if the TTL has passed.
IMPORTANT: This adapter is suitable only for single-node deployments (development, testing). For multi-node production systems, use Redis or PostgreSQL adapters instead.
Instance Method Summary collapse
-
#acquire(key, ttl) ⇒ Boolean
Attempt to acquire a lock in memory.
-
#definition_registry ⇒ Kaal::Definition::MemoryEngine
Get the definition registry for in-memory definition persistence.
-
#dispatch_registry ⇒ Kaal::Dispatch::MemoryEngine
Get the dispatch registry for in-memory logging.
-
#initialize ⇒ MemoryAdapter
constructor
A new instance of MemoryAdapter.
-
#release(key) ⇒ Boolean
Release a lock from memory.
Methods included from DispatchLogging
#log_dispatch_attempt, #parse_lock_key, parse_lock_key
Methods inherited from Adapter
Constructor Details
#initialize ⇒ MemoryAdapter
Returns a new instance of MemoryAdapter.
31 32 33 34 35 |
# File 'lib/kaal/backend/memory_adapter.rb', line 31 def initialize super @locks = {} @mutex = Mutex.new end |
Instance Method Details
#acquire(key, ttl) ⇒ Boolean
Attempt to acquire a lock in memory.
Opportunistically prunes expired locks to prevent unbounded memory growth. Since the coordinator generates unique keys per dispatch and relies on TTL expiration without calling release, this pruning is essential.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kaal/backend/memory_adapter.rb', line 63 def acquire(key, ttl) acquired = @mutex.synchronize do prune_expired_locks expiration_time = @locks[key] current_time = Time.now.utc next false if expiration_time && expiration_time > current_time @locks[key] = current_time + ttl true end log_dispatch_attempt(key) if acquired acquired end |
#definition_registry ⇒ Kaal::Definition::MemoryEngine
Get the definition registry for in-memory definition persistence.
49 50 51 |
# File 'lib/kaal/backend/memory_adapter.rb', line 49 def definition_registry @definition_registry ||= Kaal::Definition::MemoryEngine.new end |
#dispatch_registry ⇒ Kaal::Dispatch::MemoryEngine
Get the dispatch registry for in-memory logging.
41 42 43 |
# File 'lib/kaal/backend/memory_adapter.rb', line 41 def dispatch_registry @dispatch_registry ||= Kaal::Dispatch::MemoryEngine.new end |
#release(key) ⇒ Boolean
Release a lock from memory.
84 85 86 87 88 89 90 91 |
# File 'lib/kaal/backend/memory_adapter.rb', line 84 def release(key) @mutex.synchronize do return false unless @locks.key?(key) @locks.delete(key) true end end |