robust-redis-lock 0.2.2 → 0.3.0
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 +32 -4
- data/lib/robust-redis-lock/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d01644e1b5f69294708e5de33618bc62c2eff7b7
|
4
|
+
data.tar.gz: 41772d6656bff57ce9a552ea834181dfbc6d0289
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a643e22cb0d7f53b8eda94bbbe799bfb44109926655675ce8acc93237442be9d6fbd4017ca3d0de406b95d3c9e9ced32658e58d95ec7f6aa81121ffe716b18cc
|
7
|
+
data.tar.gz: 92caa5262446e635736f6326fe389975ff0ee71193c303f56b78881d867d354fc93db2babb8ac5b304bb22426a89508a4b5a24bf7469f2d80e204e8b6d62776e
|
data/lib/redis-lock.rb
CHANGED
@@ -11,16 +11,34 @@ class Redis::Lock
|
|
11
11
|
attr_accessor :sleep
|
12
12
|
attr_accessor :expire
|
13
13
|
attr_accessor :namespace
|
14
|
+
attr_accessor :key_group
|
15
|
+
|
16
|
+
def expired(options={})
|
17
|
+
self.redis.zrangebyscore(key_group_key(options), 0, Time.now.to_i).map { |key| self.new(key, options) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def key_group_key(options)
|
21
|
+
[namespace_prefix(options), (options[:key_group] || self.key_group), 'group'].join(':')
|
22
|
+
end
|
23
|
+
|
24
|
+
def namespace_prefix(options)
|
25
|
+
(options[:namespace] || self.namespace)
|
26
|
+
end
|
14
27
|
end
|
15
28
|
|
16
29
|
self.timeout = 60
|
17
30
|
self.expire = 60
|
18
31
|
self.sleep = 0.1
|
19
32
|
self.namespace = 'redis:lock'
|
33
|
+
self.key_group = 'default'
|
20
34
|
|
21
35
|
def initialize(key, options={})
|
22
36
|
raise "key cannot be nil" if key.nil?
|
23
|
-
@
|
37
|
+
@options = options
|
38
|
+
|
39
|
+
namespace_prefix = self.class.namespace_prefix(options) unless key.start_with?(self.class.namespace_prefix(options))
|
40
|
+
@key = [namespace_prefix, key].compact.join(':')
|
41
|
+
@key_group_key = self.class.key_group_key(@options)
|
24
42
|
|
25
43
|
@redis = options[:redis] || self.class.redis
|
26
44
|
raise "redis cannot be nil" if @redis.nil?
|
@@ -50,6 +68,7 @@ class Redis::Lock
|
|
50
68
|
# that's okay, because the race is harmless.
|
51
69
|
@@lock_script ||= Script.new <<-LUA
|
52
70
|
local key = KEYS[1]
|
71
|
+
local key_group = KEYS[2]
|
53
72
|
local now = tonumber(ARGV[1])
|
54
73
|
local expires_at = tonumber(ARGV[2])
|
55
74
|
local token_key = 'redis:lock:token'
|
@@ -63,6 +82,7 @@ class Redis::Lock
|
|
63
82
|
|
64
83
|
redis.call('hset', key, 'expires_at', expires_at)
|
65
84
|
redis.call('hset', key, 'token', next_token)
|
85
|
+
redis.call('zadd', key_group, expires_at, key)
|
66
86
|
|
67
87
|
if prev_expires_at then
|
68
88
|
return {'recovered', next_token}
|
@@ -70,7 +90,7 @@ class Redis::Lock
|
|
70
90
|
return {'acquired', next_token}
|
71
91
|
end
|
72
92
|
LUA
|
73
|
-
result, token = @@lock_script.eval(@redis, :keys => [@key], :argv => [now.to_i, now.to_i + @expire])
|
93
|
+
result, token = @@lock_script.eval(@redis, :keys => [@key, @key_group_key], :argv => [now.to_i, now.to_i + @expire])
|
74
94
|
|
75
95
|
@token = token if token
|
76
96
|
|
@@ -87,36 +107,44 @@ class Redis::Lock
|
|
87
107
|
# remains the same, and do not release when the lock timestamp was overwritten.
|
88
108
|
@@unlock_script ||= Script.new <<-LUA
|
89
109
|
local key = KEYS[1]
|
110
|
+
local key_group = KEYS[2]
|
90
111
|
local token = ARGV[1]
|
91
112
|
|
92
113
|
if redis.call('hget', key, 'token') == token then
|
93
114
|
redis.call('del', key)
|
115
|
+
redis.call('zrem', key_group, key)
|
94
116
|
return true
|
95
117
|
else
|
96
118
|
return false
|
97
119
|
end
|
98
120
|
LUA
|
99
|
-
result = @@unlock_script.eval(@redis, :keys => [@key], :argv => [@token])
|
121
|
+
result = @@unlock_script.eval(@redis, :keys => [@key, @key_group_key], :argv => [@token])
|
100
122
|
!!result
|
101
123
|
end
|
102
124
|
|
103
125
|
def extend
|
104
126
|
@@extend_script ||= Script.new <<-LUA
|
105
127
|
local key = KEYS[1]
|
128
|
+
local key_group = KEYS[2]
|
106
129
|
local expires_at = tonumber(ARGV[1])
|
107
130
|
local token = ARGV[2]
|
108
131
|
|
109
132
|
if redis.call('hget', key, 'token') == token then
|
110
133
|
redis.call('hset', key, 'expires_at', expires_at)
|
134
|
+
redis.call('zadd', key_group, expires_at, key)
|
111
135
|
return true
|
112
136
|
else
|
113
137
|
return false
|
114
138
|
end
|
115
139
|
LUA
|
116
|
-
!!@@extend_script.eval(@redis, :keys => [@key], :argv => [now.to_i + @expire, @token])
|
140
|
+
!!@@extend_script.eval(@redis, :keys => [@key, @key_group_key], :argv => [now.to_i + @expire, @token])
|
117
141
|
end
|
118
142
|
|
119
143
|
def now
|
120
144
|
Time.now
|
121
145
|
end
|
146
|
+
|
147
|
+
def ==(other)
|
148
|
+
self.key == other.key
|
149
|
+
end
|
122
150
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robust-redis-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kareem Kouddous
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|