Module: Redislock
- Defined in:
- lib/redislock.rb,
lib/redislock/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.9.0"
Class Method Summary collapse
Class Method Details
.aquire(name, seconds, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/redislock.rb', line 18 def self.aquire(name, seconds, &block) current = Speedytime.current expiry = current + seconds + 1 key = "lock.#{name}" # Try to aquire the lock, if we can do this # we can simply execute the block with the # help of the Lock strucutre if Redislock.redis.setnx(key, expiry) return Lock.new(key, &block).call end # we didn't get the lock, lets see if there is a deadlock # that we can resolve via an expired lock upstream_value = Redislock.redis.get(key) if upstream_value.to_i < current # ok we are expired. Let's see if we can aquire this # # Note, there are likely multiple processes competing # to aquire this lock, therefore we have to use # atomic primitives here to avoid mistakes if Redislock.redis.getset(key, expiry) == upstream_value return Lock.new(key, &block).call end end # in all other cases we fail to aquire the lock return false end |
.redis ⇒ Object
14 15 16 |
# File 'lib/redislock.rb', line 14 def self.redis @redis || raise(Error, "Please set Redislock.redis before using this class") end |
.redis=(redis) ⇒ Object
10 11 12 |
# File 'lib/redislock.rb', line 10 def self.redis=(redis) @redis = redis end |