redis-mutex 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/redis/mutex.rb +8 -4
- data/redis-mutex.gemspec +1 -1
- data/spec/redis_mutex_spec.rb +42 -0
- metadata +26 -42
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93509c7e322a8d7f8b8aab44e8b679b4943aaf21
|
4
|
+
data.tar.gz: 10570c618421703581ee0ea2cb74d6fca9b33b91
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75ceb7ece788185aa6c8a0a32f6b36ad2a524775bb2e2e44f4751c61400fcc742d0f326e37f72857ebefd709e7e007da384522093f04e65eabe6d2dd3ca1ab5f
|
7
|
+
data.tar.gz: 50541e94bd60a2af29a2b835b6a909ec160b62590f104db0f61454e16e026d0e7dd678cb9b3ee7c8fe3cc5683f6eb05e3394f96030774ee1006ca0cc75ce4751
|
data/lib/redis/mutex.rb
CHANGED
@@ -45,12 +45,16 @@ class Redis
|
|
45
45
|
def try_lock
|
46
46
|
now = Time.now.to_f
|
47
47
|
@expires_at = now + @expire # Extend in each blocking loop
|
48
|
-
|
49
|
-
|
48
|
+
|
49
|
+
loop do
|
50
|
+
return true if setnx(@expires_at) # Success, the lock has been acquired
|
51
|
+
end until old_value = get # Repeat if unlocked before get
|
52
|
+
|
53
|
+
return false if old_value.to_f > now # Check if the lock is still effective
|
50
54
|
|
51
55
|
# The lock has expired but wasn't released... BAD!
|
52
|
-
return true
|
53
|
-
return false
|
56
|
+
return true if getset(@expires_at).to_f <= now # Success, we acquired the previously expired lock
|
57
|
+
return false # Dammit, it seems that someone else was even faster than us to remove the expired lock!
|
54
58
|
end
|
55
59
|
|
56
60
|
# Returns true if resource is locked. Note that nil.to_f returns 0.0
|
data/redis-mutex.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
13
|
gem.name = "redis-mutex"
|
14
14
|
gem.require_paths = ["lib"]
|
15
|
-
gem.version = '2.1.
|
15
|
+
gem.version = '2.1.1' # retrieve this value by: Gem.loaded_specs['redis-mutex'].version.to_s
|
16
16
|
|
17
17
|
gem.add_runtime_dependency "redis-classy", "~> 1.2"
|
18
18
|
gem.add_development_dependency "rspec"
|
data/spec/redis_mutex_spec.rb
CHANGED
@@ -171,4 +171,46 @@ describe Redis::Mutex do
|
|
171
171
|
t2.join
|
172
172
|
end
|
173
173
|
end
|
174
|
+
|
175
|
+
describe 'stress test' do
|
176
|
+
LOOP_NUM = 1000
|
177
|
+
|
178
|
+
def run(id)
|
179
|
+
print "invoked worker #{id}...\n"
|
180
|
+
Redis::Classy.db.client.reconnect
|
181
|
+
mutex = Redis::Mutex.new(:test_lock, :expire => 1, :block => 10, :sleep => 0.01)
|
182
|
+
result = 0
|
183
|
+
LOOP_NUM.times do |i|
|
184
|
+
mutex.with_lock do
|
185
|
+
result += 1
|
186
|
+
sleep rand/100
|
187
|
+
end
|
188
|
+
end
|
189
|
+
print "result for worker #{id}: #{result} successful locks\n"
|
190
|
+
exit!(result == LOOP_NUM)
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'runs without hiccups' do
|
194
|
+
begin
|
195
|
+
STDOUT.sync = true
|
196
|
+
puts "\nrunning stress tests..."
|
197
|
+
if pid1 = fork
|
198
|
+
# Parent
|
199
|
+
if pid2 = fork
|
200
|
+
# Parent
|
201
|
+
Process.waitall
|
202
|
+
else
|
203
|
+
# Child 2
|
204
|
+
run(2)
|
205
|
+
end
|
206
|
+
else
|
207
|
+
# Child 1
|
208
|
+
run(1)
|
209
|
+
end
|
210
|
+
STDOUT.flush
|
211
|
+
rescue NotImplementedError
|
212
|
+
puts $!
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
174
216
|
end
|
metadata
CHANGED
@@ -1,80 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-mutex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 2.1.0
|
4
|
+
version: 2.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kenn Ejima
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-06-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.2'
|
20
|
-
none: false
|
21
|
-
prerelease: false
|
22
14
|
name: redis-classy
|
23
15
|
requirement: !ruby/object:Gem::Requirement
|
24
16
|
requirements:
|
25
17
|
- - ~>
|
26
18
|
- !ruby/object:Gem::Version
|
27
19
|
version: '1.2'
|
28
|
-
none: false
|
29
20
|
type: :runtime
|
30
|
-
|
21
|
+
prerelease: false
|
31
22
|
version_requirements: !ruby/object:Gem::Requirement
|
32
23
|
requirements:
|
33
|
-
- -
|
24
|
+
- - ~>
|
34
25
|
- !ruby/object:Gem::Version
|
35
|
-
version: '
|
36
|
-
|
37
|
-
prerelease: false
|
26
|
+
version: '1.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
38
28
|
name: rspec
|
39
29
|
requirement: !ruby/object:Gem::Requirement
|
40
30
|
requirements:
|
41
|
-
- -
|
31
|
+
- - '>='
|
42
32
|
- !ruby/object:Gem::Version
|
43
33
|
version: '0'
|
44
|
-
none: false
|
45
34
|
type: :development
|
46
|
-
|
35
|
+
prerelease: false
|
47
36
|
version_requirements: !ruby/object:Gem::Requirement
|
48
37
|
requirements:
|
49
|
-
- -
|
38
|
+
- - '>='
|
50
39
|
- !ruby/object:Gem::Version
|
51
40
|
version: '0'
|
52
|
-
|
53
|
-
prerelease: false
|
41
|
+
- !ruby/object:Gem::Dependency
|
54
42
|
name: bundler
|
55
43
|
requirement: !ruby/object:Gem::Requirement
|
56
44
|
requirements:
|
57
|
-
- -
|
45
|
+
- - '>='
|
58
46
|
- !ruby/object:Gem::Version
|
59
47
|
version: '0'
|
60
|
-
none: false
|
61
48
|
type: :development
|
62
|
-
|
49
|
+
prerelease: false
|
63
50
|
version_requirements: !ruby/object:Gem::Requirement
|
64
51
|
requirements:
|
65
|
-
- -
|
52
|
+
- - '>='
|
66
53
|
- !ruby/object:Gem::Version
|
67
54
|
version: '0'
|
68
|
-
|
69
|
-
prerelease: false
|
55
|
+
- !ruby/object:Gem::Dependency
|
70
56
|
name: rake
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
|
-
- -
|
59
|
+
- - '>='
|
74
60
|
- !ruby/object:Gem::Version
|
75
61
|
version: '0'
|
76
|
-
none: false
|
77
62
|
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
78
69
|
description: Distrubuted mutex using Redis
|
79
70
|
email:
|
80
71
|
- kenn.ejima@gmail.com
|
@@ -97,33 +88,26 @@ files:
|
|
97
88
|
- spec/spec_helper.rb
|
98
89
|
homepage: http://github.com/kenn/redis-mutex
|
99
90
|
licenses: []
|
91
|
+
metadata: {}
|
100
92
|
post_install_message:
|
101
93
|
rdoc_options: []
|
102
94
|
require_paths:
|
103
95
|
- lib
|
104
96
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
97
|
requirements:
|
106
|
-
- -
|
98
|
+
- - '>='
|
107
99
|
- !ruby/object:Gem::Version
|
108
100
|
version: '0'
|
109
|
-
segments:
|
110
|
-
- 0
|
111
|
-
hash: -4158529389064232866
|
112
|
-
none: false
|
113
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
102
|
requirements:
|
115
|
-
- -
|
103
|
+
- - '>='
|
116
104
|
- !ruby/object:Gem::Version
|
117
105
|
version: '0'
|
118
|
-
segments:
|
119
|
-
- 0
|
120
|
-
hash: -4158529389064232866
|
121
|
-
none: false
|
122
106
|
requirements: []
|
123
107
|
rubyforge_project:
|
124
|
-
rubygems_version:
|
108
|
+
rubygems_version: 2.0.3
|
125
109
|
signing_key:
|
126
|
-
specification_version:
|
110
|
+
specification_version: 4
|
127
111
|
summary: Distrubuted mutex using Redis
|
128
112
|
test_files:
|
129
113
|
- spec/redis_mutex_spec.rb
|