Class: Redis::Lockers::Lock
- Inherits:
-
Object
- Object
- Redis::Lockers::Lock
- Defined in:
- lib/redis/lockers/lock.rb
Overview
Single lock instance.
Instance Method Summary collapse
-
#acquire(redis) ⇒ Boolean
Attempts to acquire lease.
-
#initialize(key, ttl:) ⇒ Lock
constructor
Create a new Lock instance.
-
#release(redis) ⇒ void
Release acquired lease.
Constructor Details
#initialize(key, ttl:) ⇒ Lock
Create a new Lock instance.
21 22 23 24 25 26 |
# File 'lib/redis/lockers/lock.rb', line 21 def initialize(key, ttl:) @key = key.to_s @ttl = ttl.to_i @drift = @ttl * 0.01 + 2 @nonce = SecureRandom.uuid end |
Instance Method Details
#acquire(redis) ⇒ Boolean
Attempts to acquire lease.
32 33 34 35 36 37 38 39 40 |
# File 'lib/redis/lockers/lock.rb', line 32 def acquire(redis) deadline = + @ttl + @drift success = LOCK_SCRIPT.eval(redis, { :keys => [@key], :argv => [@nonce, @ttl] }) success && < deadline || false end |
#release(redis) ⇒ void
This method returns an undefined value.
Release acquired lease.
46 47 48 |
# File 'lib/redis/lockers/lock.rb', line 46 def release(redis) UNLOCK_SCRIPT.eval(redis, :keys => [@key], :argv => [@nonce]) end |