redlock 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/Gemfile.lock +2 -2
- data/README.md +2 -2
- data/lib/redlock/client.rb +14 -2
- data/lib/redlock/version.rb +1 -1
- data/spec/client_spec.rb +27 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d883de4dd36be4eab1fd32417cdc59a99cd4a7e0a68efbbe1a3865a8f9150ea7
|
4
|
+
data.tar.gz: f732c4b1fb41bb2d9b34917879d6bc893375d9a2d4d527ecfe29d935c3d998a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b05d03ae1e3e6de8b21d9cc2d7c367d03dba26a1b72b06e821a07e81183345a9ea4e32a5a984d941eb6ef5cd628acb446946e9395d37c7e532b16b5e6444b1eb
|
7
|
+
data.tar.gz: 3066e7c68d4dd14e7e193bb58f29aad00a4624a47aabd4b7b1dfff32318bcaffad7f38910e1b2ab77a0e8572a71f828dc4c83d8af8a3cd13fe95da7e95910a6e
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
redlock (1.
|
4
|
+
redlock (1.1.0)
|
5
5
|
redis (>= 3.0.0, < 5.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
docile (1.3.1)
|
18
18
|
json (2.1.0)
|
19
19
|
rake (11.3.0)
|
20
|
-
redis (4.
|
20
|
+
redis (4.1.1)
|
21
21
|
rspec (3.5.0)
|
22
22
|
rspec-core (~> 3.5.0)
|
23
23
|
rspec-expectations (~> 3.5.0)
|
data/README.md
CHANGED
@@ -108,10 +108,10 @@ rescue Redlock::LockError
|
|
108
108
|
end
|
109
109
|
```
|
110
110
|
|
111
|
-
The above code will also acquire the lock if the previous lock has expired and the lock is currently free. Keep in mind that this means the lock could have been acquired by someone else in the meantime. To only extend the life of the lock if currently locked by yourself, use the `
|
111
|
+
The above code will also acquire the lock if the previous lock has expired and the lock is currently free. Keep in mind that this means the lock could have been acquired and released by someone else in the meantime. To only extend the life of the lock if currently locked by yourself, use the `extend_only_if_locked` parameter:
|
112
112
|
|
113
113
|
```ruby
|
114
|
-
lock_manager.lock("resource key", 3000, extend: lock_info,
|
114
|
+
lock_manager.lock("resource key", 3000, extend: lock_info, extend_only_if_locked: true)
|
115
115
|
```
|
116
116
|
|
117
117
|
## Redis client configuration
|
data/lib/redlock/client.rb
CHANGED
@@ -55,11 +55,21 @@ module Redlock
|
|
55
55
|
# +ttl+:: The time-to-live in ms for the lock.
|
56
56
|
# +options+:: Hash of optional parameters
|
57
57
|
# * +extend+: A lock ("lock_info") to extend.
|
58
|
-
# * +
|
58
|
+
# * +extend_only_if_locked+: Boolean, if +extend+ is given, only acquire lock if currently held
|
59
|
+
# * +extend_only_if_life+: Deprecated, same as +extend_only_if_locked+
|
60
|
+
# * +extend_life+: Deprecated, same as +extend_only_if_locked+
|
59
61
|
# +block+:: an optional block to be executed; after its execution, the lock (if successfully
|
60
62
|
# acquired) is automatically unlocked.
|
61
63
|
def lock(resource, ttl, options = {}, &block)
|
62
64
|
lock_info = try_lock_instances(resource, ttl, options)
|
65
|
+
if options[:extend_only_if_life] && !Gem::Deprecate.skip
|
66
|
+
warn 'DEPRECATION WARNING: The `extend_only_if_life` option has been renamed `extend_only_if_locked`.'
|
67
|
+
options[:extend_only_if_locked] = options[:extend_only_if_life]
|
68
|
+
end
|
69
|
+
if options[:extend_life] && !Gem::Deprecate.skip
|
70
|
+
warn 'DEPRECATION WARNING: The `extend_life` option has been renamed `extend_only_if_locked`.'
|
71
|
+
options[:extend_only_if_locked] = options[:extend_life]
|
72
|
+
end
|
63
73
|
|
64
74
|
if block_given?
|
65
75
|
begin
|
@@ -118,6 +128,8 @@ module Redlock
|
|
118
128
|
else
|
119
129
|
@redis = Redis.new(connection)
|
120
130
|
end
|
131
|
+
|
132
|
+
load_scripts
|
121
133
|
end
|
122
134
|
|
123
135
|
def lock(resource, val, ttl, allow_new_lock)
|
@@ -178,7 +190,7 @@ module Redlock
|
|
178
190
|
|
179
191
|
def lock_instances(resource, ttl, options)
|
180
192
|
value = (options[:extend] || { value: SecureRandom.uuid })[:value]
|
181
|
-
allow_new_lock =
|
193
|
+
allow_new_lock = options[:extend_only_if_locked] ? 'no' : 'yes'
|
182
194
|
|
183
195
|
locked, time_elapsed = timed do
|
184
196
|
@servers.select { |s| s.lock resource, value, ttl, allow_new_lock }.size
|
data/lib/redlock/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -64,9 +64,9 @@ RSpec.describe Redlock::Client do
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
context 'when
|
67
|
+
context 'when extend_only_if_locked flag is given' do
|
68
68
|
it 'does not extend a non-existent lock' do
|
69
|
-
@lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'},
|
69
|
+
@lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'}, extend_only_if_locked: true)
|
70
70
|
expect(@lock_info).to eq(false)
|
71
71
|
end
|
72
72
|
end
|
@@ -76,14 +76,14 @@ RSpec.describe Redlock::Client do
|
|
76
76
|
lock_info = lock_manager.lock(resource_key, ttl)
|
77
77
|
expect(resource_key).to_not be_lockable(lock_manager, ttl)
|
78
78
|
|
79
|
-
lock_info = lock_manager.lock(resource_key, ttl, extend: lock_info,
|
79
|
+
lock_info = lock_manager.lock(resource_key, ttl, extend: lock_info, extend_only_if_locked: true)
|
80
80
|
expect(lock_info).not_to be_nil
|
81
81
|
expect(redis_client.pttl(resource_key)).to be_within(200).of(ttl)
|
82
82
|
end
|
83
83
|
|
84
|
-
context 'when
|
84
|
+
context 'when extend_only_if_locked flag is not given' do
|
85
85
|
it "sets the given value when trying to extend a non-existent lock" do
|
86
|
-
@lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'},
|
86
|
+
@lock_info = lock_manager.lock(resource_key, ttl, extend: {value: 'hello world'}, extend_only_if_locked: false)
|
87
87
|
expect(@lock_info).to be_lock_info_for(resource_key)
|
88
88
|
expect(@lock_info[:value]).to eq('hello world') # really we should test what's in redis
|
89
89
|
end
|
@@ -94,6 +94,28 @@ RSpec.describe Redlock::Client do
|
|
94
94
|
second_attempt = lock_manager.lock(resource_key, ttl)
|
95
95
|
expect(second_attempt).to eq(false)
|
96
96
|
end
|
97
|
+
|
98
|
+
context 'when extend_life flag is given' do
|
99
|
+
it 'treats it as extend_only_if_locked but warns it is deprecated' do
|
100
|
+
ttl = 20_000
|
101
|
+
lock_info = lock_manager.lock(resource_key, ttl)
|
102
|
+
expect(resource_key).to_not be_lockable(lock_manager, ttl)
|
103
|
+
expect(lock_manager).to receive(:warn).with(/DEPRECATION WARNING: The `extend_life`/)
|
104
|
+
lock_info = lock_manager.lock(resource_key, ttl, extend: lock_info, extend_life: true)
|
105
|
+
expect(lock_info).not_to be_nil
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'when extend_only_if_life flag is given' do
|
110
|
+
it 'treats it as extend_only_if_locked but warns it is deprecated' do
|
111
|
+
ttl = 20_000
|
112
|
+
lock_info = lock_manager.lock(resource_key, ttl)
|
113
|
+
expect(resource_key).to_not be_lockable(lock_manager, ttl)
|
114
|
+
expect(lock_manager).to receive(:warn).with(/DEPRECATION WARNING: The `extend_only_if_life`/)
|
115
|
+
lock_info = lock_manager.lock(resource_key, ttl, extend: lock_info, extend_only_if_life: true)
|
116
|
+
expect(lock_info).not_to be_nil
|
117
|
+
end
|
118
|
+
end
|
97
119
|
end
|
98
120
|
|
99
121
|
context 'when lock is not available' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redlock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leandro Moreira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|