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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7d9c9882c711325cf6560eaef7a0ea124a70575
4
- data.tar.gz: d7f4c26f869c92382b7e2e0924749404e13c4c9a
3
+ metadata.gz: d01644e1b5f69294708e5de33618bc62c2eff7b7
4
+ data.tar.gz: 41772d6656bff57ce9a552ea834181dfbc6d0289
5
5
  SHA512:
6
- metadata.gz: 3f6cb3c1b59df99f4c1531db778957d4279a186ce0c2d13c8515f70066528d7fdcb06060d08f63da3cce8dd1bb7093200f92dc9c69b0ac70367a3ebedf2a0d71
7
- data.tar.gz: 22d94c6e9901cf5f6190306b2839e489d6015a1a33423d0030bca914ecbe567d78ec084d4f95acf6b94953a9e21ad950553e22fd648f9ae390e3534494eefc36
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
- @key = (options[:namespace] || self.class.namespace) + ":" + key
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
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  class Lock
3
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  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.2.2
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-05-21 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis