lib/interlock/lock.rb in interlock-1.3 vs lib/interlock/lock.rb in interlock-1.4
- old
+ new
@@ -15,28 +15,29 @@
# We have to be compatible with both client APIs. Eventually we can use Memcached#cas
# for this.
begin
response = CACHE.add("lock:#{key}", "Locked by #{Process.pid}", lock_expiry)
- # Nil is a successful response for Memcached, so we'll simulate the MemCache
- # API.
- response ||= "STORED\r\n"
- rescue Object => e
- # Catch exceptions from Memcached without setting response.
+ # Nil is a successful response for Memcached 0.11, and "STORED\r\n" for MemCache.
+ response = [true, nil, "STORED\r\n"].include?(response)
+ rescue Memcached::NotStored
+ # Do nothing
end
- if response == "STORED\r\n"
+ if response
begin
value = yield(CACHE.get(key))
CACHE.set(key, value)
return value
ensure
CACHE.delete("lock:#{key}")
end
else
+ # Wait
sleep((2**count) / 2.0)
end
end
+
raise ::Interlock::LockAcquisitionError, "Couldn't acquire lock for #{key}"
end
end
end