redis_mutex 0.0.1.pre.alpha.pre.11 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/lib/redis_mutex.rb +28 -41
- data/redis_mutex.gemspec +0 -1
- data/test/test_redis_mutex.rb +2 -11
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
ZTBlYmMxZjA5YzcxMjQ0N2Q3NDBjYzUwMjRlOWZiMjM4OTVkOGEyNA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ed7285292327df555cdb24c880c95488bd49ef48
|
4
|
+
data.tar.gz: f664b54f0fcc52e1e1d90e771c52666f18174063
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ZGU4OGNjYjc0MWU3YTIwYTRhMGQxMmQzOThmZWRjM2Y2MzFkYTI1ZmM5NDM4
|
11
|
-
NDUyODA3MmJjNTczNjZjOWNiZTA5M2Q3MTQ3NTc3ZDE5YmFkNTE=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MzcyZmQ0MTE0MzlmOWYxZTI3NjU1NzkxZjZjNjA4Zjk3NjgxNjQ2NzgyZThk
|
14
|
-
YzBlMWI4NDM1YWQ0MjMxZWQ1ODBiMTA4ZGVhOGFmODkwYmUxZWUyZjAxZmMz
|
15
|
-
ZGU3ZDAxZTJiNTNlYzUwZDM1OTc4OWVhZDYyY2NkY2M1OTk3NGY=
|
6
|
+
metadata.gz: 4a0177cb03328bcca09855fbc2abeab8eeb9ef26785eee204bd7ce2cc2325a90c878015efabcd17709c4644dbb8582b12873289ae069da9d48a421da0f350fbb
|
7
|
+
data.tar.gz: 4c3eaff96de53af183942973176a2055b2efbae2aeda1393120ea6d4d97787d20b70ba87988bc562ee50ca0c4d2c515b8a834a2e419dccf8229f2434ed31d13c
|
data/lib/redis_mutex.rb
CHANGED
@@ -2,16 +2,9 @@ require "redis_mutex/version"
|
|
2
2
|
require 'securerandom'
|
3
3
|
|
4
4
|
class RedisMutex
|
5
|
-
|
5
|
+
SHAs = {}
|
6
6
|
MutexNotLockedError = Class.new(StandardError)
|
7
7
|
|
8
|
-
def self.def_redis_script(name, source)
|
9
|
-
SCRIPTS[name] = {
|
10
|
-
source: source,
|
11
|
-
sha: Digest::SHA1.hexdigest(source),
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
8
|
def initialize(redis, name, token=nil)
|
16
9
|
@redis = redis
|
17
10
|
@name = name
|
@@ -29,37 +22,40 @@ class RedisMutex
|
|
29
22
|
@redis.set(key, token, nx: true, ex: expire)
|
30
23
|
end
|
31
24
|
|
32
|
-
def_redis_script :compare_and_delete, <<-LUA
|
33
|
-
if ARGV[1] ~= redis.call('get', KEYS[1]) then
|
34
|
-
return false
|
35
|
-
end
|
36
|
-
|
37
|
-
return redis.call('del', KEYS[1])
|
38
|
-
LUA
|
39
25
|
def release
|
40
|
-
|
41
|
-
|
26
|
+
sha = SHAs[:delete_if_locked] ||= @redis.script(:load, <<-LUA)
|
27
|
+
if ARGV[1] ~= redis.call('get', KEYS[1]) then
|
28
|
+
return false
|
29
|
+
end
|
42
30
|
|
43
|
-
|
44
|
-
|
45
|
-
return false
|
46
|
-
end
|
31
|
+
return redis.call('del', KEYS[1])
|
32
|
+
LUA
|
47
33
|
|
48
|
-
|
49
|
-
LUA
|
50
|
-
def renew(expire)
|
51
|
-
run_script(:compare_and_expire, [key], [token, expire]) or raise MutexNotLockedError
|
34
|
+
@redis.evalsha(sha, [key], [token]) or raise MutexNotLockedError
|
52
35
|
end
|
53
36
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
37
|
+
def renew(expire)
|
38
|
+
sha = SHAs[:set_expiration_if_locked] ||= @redis.script(:load, <<-LUA)
|
39
|
+
if ARGV[1] ~= redis.call('get', KEYS[1]) then
|
40
|
+
return false
|
41
|
+
end
|
42
|
+
|
43
|
+
return redis.call('expire', KEYS[1], ARGV[2])
|
44
|
+
LUA
|
45
|
+
|
46
|
+
@redis.evalsha(sha, [key], [token, expire]) or raise MutexNotLockedError
|
47
|
+
end
|
58
48
|
|
59
|
-
return redis.call('set', KEYS[1], ARGV[2])
|
60
|
-
LUA
|
61
49
|
def set_token(new_token)
|
62
|
-
|
50
|
+
sha = SHAs[:compare_and_swap] ||= @redis.script(:load, <<-LUA)
|
51
|
+
if ARGV[1] ~= redis.call('get', KEYS[1]) then
|
52
|
+
return false
|
53
|
+
end
|
54
|
+
|
55
|
+
return redis.call('set', KEYS[1], ARGV[2])
|
56
|
+
LUA
|
57
|
+
|
58
|
+
@redis.evalsha(sha, [key], [token, new_token]) or raise MutexNotLockedError
|
63
59
|
@token = new_token
|
64
60
|
end
|
65
61
|
|
@@ -70,13 +66,4 @@ class RedisMutex
|
|
70
66
|
def verify!
|
71
67
|
locked? or raise MutexNotLockedError
|
72
68
|
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def run_script(name, keys, args)
|
77
|
-
script = SCRIPTS.fetch(name)
|
78
|
-
@redis.evalsha(script.fetch(:sha), keys, args)
|
79
|
-
rescue Redis::CommandError
|
80
|
-
@redis.eval(script.fetch(:source), keys, args)
|
81
|
-
end
|
82
69
|
end
|
data/redis_mutex.gemspec
CHANGED
@@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.description = %q{}
|
17
17
|
spec.homepage = "https://github.com/GoodGuide/redis_mutex"
|
18
18
|
spec.license = "MIT"
|
19
|
-
spec.required_ruby_version = '>= 1.9.3'
|
20
19
|
|
21
20
|
spec.files = `git ls-files -z`.split("\x0")
|
22
21
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
data/test/test_redis_mutex.rb
CHANGED
@@ -43,11 +43,11 @@ class TestRedisMutex < Minitest::Test
|
|
43
43
|
assert redis.get(key) == original_token
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def test_locked_when_unlocked
|
47
47
|
assert !mutex_a.locked?
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
50
|
+
def test_locked_when_locked
|
51
51
|
mutex_a.acquire_lock(DEFAULT_TIMEOUT)
|
52
52
|
assert mutex_a.locked?
|
53
53
|
end
|
@@ -117,13 +117,4 @@ class TestRedisMutex < Minitest::Test
|
|
117
117
|
assert RedisMutex.new(redis, mutex_name, mutex_a.token).release
|
118
118
|
end
|
119
119
|
|
120
|
-
def test_automatic_script_loading
|
121
|
-
# lock to ensure a script is loaded and its SHA cached
|
122
|
-
mutex_a.acquire_lock(1)
|
123
|
-
mutex_a.release
|
124
|
-
|
125
|
-
redis.script(:flush)
|
126
|
-
assert mutex_a.acquire_lock(1)
|
127
|
-
end
|
128
|
-
|
129
120
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_mutex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Taylor Long
|
@@ -14,56 +14,56 @@ dependencies:
|
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.7'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 5.5.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 5.5.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: redis
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 3.2.0
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 3.2.0
|
69
69
|
description: ''
|
@@ -73,8 +73,8 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- .gitignore
|
77
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
78
|
- Gemfile
|
79
79
|
- Guardfile
|
80
80
|
- LICENSE.txt
|
@@ -94,17 +94,17 @@ require_paths:
|
|
94
94
|
- lib
|
95
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
|
-
- -
|
97
|
+
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
99
|
+
version: '0'
|
100
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
104
|
+
version: '0'
|
105
105
|
requirements: []
|
106
106
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
107
|
+
rubygems_version: 2.2.2
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Simple distributed mutex using Redis.
|