robust-redis-lock 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/redis-lock.rb +8 -7
- data/lib/robust-redis-lock/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7d9c9882c711325cf6560eaef7a0ea124a70575
|
4
|
+
data.tar.gz: d7f4c26f869c92382b7e2e0924749404e13c4c9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f6cb3c1b59df99f4c1531db778957d4279a186ce0c2d13c8515f70066528d7fdcb06060d08f63da3cce8dd1bb7093200f92dc9c69b0ac70367a3ebedf2a0d71
|
7
|
+
data.tar.gz: 22d94c6e9901cf5f6190306b2839e489d6015a1a33423d0030bca914ecbe567d78ec084d4f95acf6b94953a9e21ad950553e22fd648f9ae390e3534494eefc36
|
data/lib/redis-lock.rb
CHANGED
@@ -32,8 +32,8 @@ class Redis::Lock
|
|
32
32
|
|
33
33
|
def lock
|
34
34
|
result = false
|
35
|
-
start_at =
|
36
|
-
while
|
35
|
+
start_at = now
|
36
|
+
while now - start_at < @timeout
|
37
37
|
break if result = try_lock
|
38
38
|
sleep @sleep.to_f
|
39
39
|
end
|
@@ -46,8 +46,6 @@ class Redis::Lock
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def try_lock
|
49
|
-
now = Time.now.to_i
|
50
|
-
|
51
49
|
# This script loading is not thread safe (touching a class variable), but
|
52
50
|
# that's okay, because the race is harmless.
|
53
51
|
@@lock_script ||= Script.new <<-LUA
|
@@ -72,7 +70,7 @@ class Redis::Lock
|
|
72
70
|
return {'acquired', next_token}
|
73
71
|
end
|
74
72
|
LUA
|
75
|
-
result, token = @@lock_script.eval(@redis, :keys => [@key], :argv => [now, now + @expire])
|
73
|
+
result, token = @@lock_script.eval(@redis, :keys => [@key], :argv => [now.to_i, now.to_i + @expire])
|
76
74
|
|
77
75
|
@token = token if token
|
78
76
|
|
@@ -103,7 +101,6 @@ class Redis::Lock
|
|
103
101
|
end
|
104
102
|
|
105
103
|
def extend
|
106
|
-
now = Time.now.to_i
|
107
104
|
@@extend_script ||= Script.new <<-LUA
|
108
105
|
local key = KEYS[1]
|
109
106
|
local expires_at = tonumber(ARGV[1])
|
@@ -116,6 +113,10 @@ class Redis::Lock
|
|
116
113
|
return false
|
117
114
|
end
|
118
115
|
LUA
|
119
|
-
!!@@extend_script.eval(@redis, :keys => [@key], :argv => [now + @expire, @token])
|
116
|
+
!!@@extend_script.eval(@redis, :keys => [@key], :argv => [now.to_i + @expire, @token])
|
117
|
+
end
|
118
|
+
|
119
|
+
def now
|
120
|
+
Time.now
|
120
121
|
end
|
121
122
|
end
|