Class: Mistri::Locks::Hold
- Inherits:
-
Object
- Object
- Mistri::Locks::Hold
- Defined in:
- lib/mistri/locks.rb
Overview
A held lease: one thread renews it on the heartbeat and watches the stop flag between renewals. Renewal runs on a monotonic deadline (the last sleep before it is the remaining fraction, never a full tick, so the cadence is exact and a lease can never lapse waiting for a tick to round up). release stops the thread (and joins it, so a mid-renewal tick can never re-stamp a lease that was just released) and deletes the key.
Instance Method Summary collapse
-
#initialize(adapter, key, ttl, heartbeat, stop_key: nil, signal: nil) ⇒ Hold
constructor
A new instance of Hold.
- #release ⇒ Object
Constructor Details
#initialize(adapter, key, ttl, heartbeat, stop_key: nil, signal: nil) ⇒ Hold
Returns a new instance of Hold.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mistri/locks.rb', line 54 def initialize(adapter, key, ttl, heartbeat, stop_key: nil, signal: nil) @adapter = adapter @key = key @stopping = false @thread = Thread.new do deadline = now + heartbeat until @stopping sleep (deadline - now).clamp(0.01, 1.0) break if @stopping signal.abort!("stopped by user") if stop_key && signal && adapter.flag?(stop_key) if now >= deadline adapter.renew(key, ttl: ttl) deadline = now + heartbeat end end end end |
Instance Method Details
#release ⇒ Object
73 74 75 76 77 78 |
# File 'lib/mistri/locks.rb', line 73 def release @stopping = true @thread.kill @thread.join(2) @adapter.release(@key) end |