Permalink
Browse files

Be a little more deterministic and conservative about method lookup. …

…Now works better with Sinatra.
  • Loading branch information...
1 parent 7c8fd74 commit 6099ef0259273f61f026d4d577a9dd2da55ad022 @kenn committed Nov 11, 2011
Showing with 12 additions and 12 deletions.
  1. +3 −3 Gemfile.lock
  2. +9 −9 lib/redis/mutex.rb
View
@@ -9,9 +9,9 @@ GEM
rake
rake (0.9.2.2)
redis (2.2.2)
- redis-classy (1.0.0)
- redis-namespace (~> 1.0.0)
- redis-namespace (1.0.3)
+ redis-classy (1.0.1)
+ redis-namespace (~> 1.0)
+ redis-namespace (1.1.0)
redis (< 3.0.0)
rspec (2.7.0)
rspec-core (~> 2.7.0)
View
@@ -11,7 +11,7 @@ class Redis
#
class Mutex < Redis::Classy
autoload :Macro, 'redis/mutex/macro'
- attr_reader :block, :sleep, :expire, :locking
+ attr_reader :locking
DEFAULT_EXPIRE = 10
def initialize(object, options={})
@@ -53,26 +53,26 @@ def lock
def try_lock
now = Time.now.to_f
- @expires_at = now + @expire # Extend in each blocking loop
- return true if setnx(@expires_at) # Success, the lock has been acquired
- return false if get.to_f > now # Check if the lock is still effective
+ @expires_at = now + @expire # Extend in each blocking loop
+ return true if self.setnx(@expires_at) # Success, the lock has been acquired
+ return false if self.get.to_f > now # Check if the lock is still effective
# The lock has expired but wasn't released... BAD!
- return true if getset(@expires_at).to_f <= now # Success, we acquired the previously expired lock
+ return true if self.getset(@expires_at).to_f <= now # Success, we acquired the previously expired lock
return false # Dammit, it seems that someone else was even faster than us to remove the expired lock!
end
def unlock(force=false)
@locking = false
- del if get.to_f == @expires_at or force # Release the lock if it seems to be yours
+ self.del if self.get.to_f == @expires_at or force # Release the lock if it seems to be yours
end
class << self
def sweep
- return 0 if (all_keys = keys).empty?
+ return 0 if (all_keys = self.keys).empty?
now = Time.now.to_f
- values = mget(*all_keys)
+ values = self.mget(*all_keys)
expired_keys = [].tap do |array|
all_keys.each_with_index do |key, i|
@@ -81,7 +81,7 @@ def sweep
end
expired_keys.each do |key|
- del(key) if getset(key, now + DEFAULT_EXPIRE).to_f <= now # Make extra sure that anyone haven't extended the lock
+ self.del(key) if self.getset(key, now + DEFAULT_EXPIRE).to_f <= now # Make extra sure that anyone haven't extended the lock
end
expired_keys.size

0 comments on commit 6099ef0

Please sign in to comment.