mlanett-redis-lock 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +7 -0
- data/Gemfile-redis2 +9 -0
- data/Gemfile-redis2.lock +35 -0
- data/Gemfile-redis3 +9 -0
- data/Gemfile-redis3.lock +35 -0
- data/README.md +5 -2
- data/lib/redis-lock.rb +3 -3
- data/lib/redis-lock/version.rb +1 -1
- data/spec/redis_lock_spec.rb +12 -6
- metadata +21 -5
data/.travis.yml
ADDED
data/Gemfile-redis2
ADDED
data/Gemfile-redis2.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mlanett-redis-lock (0.1.1)
|
5
|
+
redis
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
multi_json (1.3.6)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
redis (2.2.2)
|
14
|
+
rspec (2.11.0)
|
15
|
+
rspec-core (~> 2.11.0)
|
16
|
+
rspec-expectations (~> 2.11.0)
|
17
|
+
rspec-mocks (~> 2.11.0)
|
18
|
+
rspec-core (2.11.1)
|
19
|
+
rspec-expectations (2.11.2)
|
20
|
+
diff-lcs (~> 1.1.3)
|
21
|
+
rspec-mocks (2.11.2)
|
22
|
+
simplecov (0.6.4)
|
23
|
+
multi_json (~> 1.0)
|
24
|
+
simplecov-html (~> 0.5.3)
|
25
|
+
simplecov-html (0.5.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
mlanett-redis-lock!
|
32
|
+
rake
|
33
|
+
redis (~> 2.0)
|
34
|
+
rspec
|
35
|
+
simplecov
|
data/Gemfile-redis3
ADDED
data/Gemfile-redis3.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mlanett-redis-lock (0.1.1)
|
5
|
+
redis
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
multi_json (1.3.6)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
redis (3.0.1)
|
14
|
+
rspec (2.11.0)
|
15
|
+
rspec-core (~> 2.11.0)
|
16
|
+
rspec-expectations (~> 2.11.0)
|
17
|
+
rspec-mocks (~> 2.11.0)
|
18
|
+
rspec-core (2.11.1)
|
19
|
+
rspec-expectations (2.11.2)
|
20
|
+
diff-lcs (~> 1.1.3)
|
21
|
+
rspec-mocks (2.11.2)
|
22
|
+
simplecov (0.6.4)
|
23
|
+
multi_json (~> 1.0)
|
24
|
+
simplecov-html (~> 0.5.3)
|
25
|
+
simplecov-html (0.5.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
mlanett-redis-lock!
|
32
|
+
rake
|
33
|
+
redis (~> 3.0)
|
34
|
+
rspec
|
35
|
+
simplecov
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Redis::Lock
|
2
2
|
|
3
|
+
[![Build Status](https://secure.travis-ci.org/langalex/redis-lock.png?branch=master)](http://travis-ci.org/langalex/redis-lock)
|
4
|
+
|
5
|
+
|
3
6
|
This gem implements a pessimistic lock using Redis.
|
4
7
|
It correctly handles timeouts and vanishing lock owners (such as machine failures)
|
5
8
|
|
@@ -9,7 +12,7 @@ This uses setnx, but not the setnx algorithm described in the redis cookbook, wh
|
|
9
12
|
|
10
13
|
Add this line to your application's Gemfile:
|
11
14
|
|
12
|
-
gem 'redis-lock'
|
15
|
+
gem 'mlanett-redis-lock', require: 'redis-lock'
|
13
16
|
|
14
17
|
And then execute:
|
15
18
|
|
@@ -17,7 +20,7 @@ And then execute:
|
|
17
20
|
|
18
21
|
Or install it yourself as:
|
19
22
|
|
20
|
-
$ gem install redis-lock
|
23
|
+
$ gem install mlanett-redis-lock
|
21
24
|
|
22
25
|
## Background
|
23
26
|
|
data/lib/redis-lock.rb
CHANGED
@@ -35,12 +35,12 @@ class Redis
|
|
35
35
|
do_lock_with_timeout(timeout) or raise LockNotAcquired.new(key)
|
36
36
|
if block then
|
37
37
|
begin
|
38
|
-
block.call
|
38
|
+
result = block.call
|
39
39
|
ensure
|
40
40
|
release_lock
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
43
|
+
result
|
44
44
|
end
|
45
45
|
|
46
46
|
def extend_life( new_life )
|
@@ -85,7 +85,7 @@ class Redis
|
|
85
85
|
new_xval = Time.now.to_i + life
|
86
86
|
result = redis.mapped_msetnx okey => oval, xkey => new_xval
|
87
87
|
|
88
|
-
if result
|
88
|
+
if [1, true].include?(result) then
|
89
89
|
log :debug, "do_lock() success"
|
90
90
|
@xval = new_xval
|
91
91
|
return true
|
data/lib/redis-lock/version.rb
CHANGED
data/spec/redis_lock_spec.rb
CHANGED
@@ -20,16 +20,22 @@ describe Redis::Lock, redis: true do
|
|
20
20
|
hers.should_not be_locked
|
21
21
|
end
|
22
22
|
|
23
|
+
it 'returns the return value of the block' do
|
24
|
+
hers.lock do
|
25
|
+
1
|
26
|
+
end.should eql(1)
|
27
|
+
end
|
28
|
+
|
23
29
|
it "can prevent other use of a lock" do
|
24
30
|
hers.lock do
|
25
|
-
expect { his.lock.unlock }.to raise_exception
|
31
|
+
expect { his.lock; his.unlock }.to raise_exception
|
26
32
|
end
|
27
|
-
expect { his.lock.unlock }.to_not raise_exception
|
33
|
+
expect { his.lock; his.unlock }.to_not raise_exception
|
28
34
|
end
|
29
35
|
|
30
36
|
it "can lock two different items at the same time" do
|
31
37
|
his.lock do
|
32
|
-
expect { his_other.lock.unlock }.to_not raise_exception
|
38
|
+
expect { his_other.lock; his_other.unlock }.to_not raise_exception
|
33
39
|
his.should be_locked
|
34
40
|
end
|
35
41
|
end
|
@@ -45,7 +51,7 @@ describe Redis::Lock, redis: true do
|
|
45
51
|
end
|
46
52
|
|
47
53
|
it "can release a lock" do
|
48
|
-
hers.lock.release_lock
|
54
|
+
hers.lock; hers.release_lock
|
49
55
|
end
|
50
56
|
|
51
57
|
it "can use a timeout" do
|
@@ -70,14 +76,14 @@ describe Redis::Lock, redis: true do
|
|
70
76
|
hers.life = 1
|
71
77
|
hers.lock
|
72
78
|
# don't unlock it, let hers time out
|
73
|
-
expect { his.lock(10).unlock }.to_not raise_exception
|
79
|
+
expect { his.lock(10); his.unlock }.to_not raise_exception
|
74
80
|
end
|
75
81
|
|
76
82
|
it "can extend the life of a lock" do
|
77
83
|
hers.life = 1
|
78
84
|
hers.lock
|
79
85
|
hers.extend_life(100)
|
80
|
-
expect { his.lock(10).unlock }.to raise_exception
|
86
|
+
expect { his.lock(10); his.unlock }.to raise_exception
|
81
87
|
hers.unlock
|
82
88
|
end
|
83
89
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mlanett-redis-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Pessimistic locking using Redis
|
26
31
|
email:
|
27
32
|
- mark.lanett@gmail.com
|
@@ -31,7 +36,12 @@ extra_rdoc_files: []
|
|
31
36
|
files:
|
32
37
|
- .gitignore
|
33
38
|
- .rspec
|
39
|
+
- .travis.yml
|
34
40
|
- Gemfile
|
41
|
+
- Gemfile-redis2
|
42
|
+
- Gemfile-redis2.lock
|
43
|
+
- Gemfile-redis3
|
44
|
+
- Gemfile-redis3.lock
|
35
45
|
- Guardfile
|
36
46
|
- LICENSE
|
37
47
|
- README.md
|
@@ -56,15 +66,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
66
|
- - ! '>='
|
57
67
|
- !ruby/object:Gem::Version
|
58
68
|
version: '0'
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
hash: -3504077138606239435
|
59
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
73
|
none: false
|
61
74
|
requirements:
|
62
75
|
- - ! '>='
|
63
76
|
- !ruby/object:Gem::Version
|
64
77
|
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: -3504077138606239435
|
65
81
|
requirements: []
|
66
82
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.8.
|
83
|
+
rubygems_version: 1.8.23
|
68
84
|
signing_key:
|
69
85
|
specification_version: 3
|
70
86
|
summary: Pessimistic locking using Redis
|