redlock 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/redlock/client.rb +9 -3
- data/lib/redlock/version.rb +1 -1
- data/spec/client_spec.rb +23 -6
- data/spec/testing_spec.rb +3 -3
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13a2ef1c0096e1135a1996719fbeb679234b639e0debecba4fa6d72f3fcd174a
|
4
|
+
data.tar.gz: e8e247b8be0ded9272f04addfbcbc6b99c1c6ad4429f344c74d61101af7e1682
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81bf9233769810cc1a3f08a6927dc5ffddfa5c31ed158071daf1ad2ffc8303060094dbd78aa4af4185148b49878e36fcf54483a41378277e3bb306dd45c483e6
|
7
|
+
data.tar.gz: b0f198c408d7281c8d1d45ef57ce612a964571bf728969601ecfd448f6e482c83567cef66d94fab6bcf722909bd19ef3120ad2ae56171dc83fae815ad81111a9
|
data/lib/redlock/client.rb
CHANGED
@@ -168,13 +168,17 @@ module Redlock
|
|
168
168
|
|
169
169
|
def lock(resource, val, ttl, allow_new_lock)
|
170
170
|
recover_from_script_flush do
|
171
|
-
@redis.
|
171
|
+
@redis.with { |conn|
|
172
|
+
conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
|
173
|
+
}
|
172
174
|
end
|
173
175
|
end
|
174
176
|
|
175
177
|
def unlock(resource, val)
|
176
178
|
recover_from_script_flush do
|
177
|
-
@redis.
|
179
|
+
@redis.with { |conn|
|
180
|
+
conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
|
181
|
+
}
|
178
182
|
end
|
179
183
|
rescue
|
180
184
|
# Nothing to do, unlocking is just a best-effort attempt.
|
@@ -182,7 +186,9 @@ module Redlock
|
|
182
186
|
|
183
187
|
def get_remaining_ttl(resource)
|
184
188
|
recover_from_script_flush do
|
185
|
-
@redis.
|
189
|
+
@redis.with { |conn|
|
190
|
+
conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
|
191
|
+
}
|
186
192
|
end
|
187
193
|
rescue RedisClient::ConnectionError
|
188
194
|
nil
|
data/lib/redlock/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -5,7 +5,21 @@ require 'connection_pool'
|
|
5
5
|
RSpec.describe Redlock::Client do
|
6
6
|
# It is recommended to have at least 3 servers in production
|
7
7
|
let(:lock_manager_opts) { { retry_count: 3 } }
|
8
|
-
let(:
|
8
|
+
let(:redis_urls_or_clients) {
|
9
|
+
urls = Redlock::Client::DEFAULT_REDIS_URLS
|
10
|
+
if rand(0..1).zero?
|
11
|
+
RSpec.configuration.reporter.message "variant: client urls"
|
12
|
+
urls
|
13
|
+
else
|
14
|
+
RSpec.configuration.reporter.message "variant: client objects"
|
15
|
+
urls.map {|url|
|
16
|
+
ConnectionPool.new { RedisClient.new(url: url) }
|
17
|
+
}
|
18
|
+
end
|
19
|
+
}
|
20
|
+
let(:lock_manager) {
|
21
|
+
Redlock::Client.new(redis_urls_or_clients, lock_manager_opts)
|
22
|
+
}
|
9
23
|
let(:redis_client) { RedisClient.new(url: "redis://#{redis1_host}:#{redis1_port}") }
|
10
24
|
let(:resource_key) { SecureRandom.hex(3) }
|
11
25
|
let(:ttl) { 1000 }
|
@@ -37,9 +51,10 @@ RSpec.describe Redlock::Client do
|
|
37
51
|
|
38
52
|
it 'accepts ConnectionPool objects' do
|
39
53
|
pool = ConnectionPool.new { RedisClient.new(url: "redis://#{redis1_host}:#{redis1_port}") }
|
40
|
-
|
54
|
+
_redlock = Redlock::Client.new([pool])
|
41
55
|
|
42
56
|
lock_info = lock_manager.lock(resource_key, ttl)
|
57
|
+
expect(lock_info).to be_a(Hash)
|
43
58
|
expect(resource_key).to_not be_lockable(lock_manager, ttl)
|
44
59
|
lock_manager.unlock(lock_info)
|
45
60
|
end
|
@@ -48,7 +63,7 @@ RSpec.describe Redlock::Client do
|
|
48
63
|
redis_client.call('SCRIPT', 'FLUSH')
|
49
64
|
|
50
65
|
pool = ConnectionPool.new { RedisClient.new(url: "redis://#{redis1_host}:#{redis1_port}") }
|
51
|
-
|
66
|
+
_redlock = Redlock::Client.new([pool])
|
52
67
|
|
53
68
|
raw_info = redis_client.call('INFO')
|
54
69
|
number_of_cached_scripts = raw_info[/number_of_cached_scripts\:\d+/].split(':').last
|
@@ -197,7 +212,7 @@ RSpec.describe Redlock::Client do
|
|
197
212
|
2000
|
198
213
|
end
|
199
214
|
|
200
|
-
lock_manager = Redlock::Client.new(
|
215
|
+
lock_manager = Redlock::Client.new(redis_urls_or_clients, retry_count: 1, retry_delay: retry_delay)
|
201
216
|
another_lock_info = lock_manager.lock(resource_key, ttl)
|
202
217
|
|
203
218
|
expect(lock_manager).to receive(:sleep) do |sleep|
|
@@ -272,7 +287,9 @@ RSpec.describe Redlock::Client do
|
|
272
287
|
context 'when script cache has been flushed' do
|
273
288
|
before(:each) do
|
274
289
|
@manipulated_instance = lock_manager.instance_variable_get(:@servers).first
|
275
|
-
@manipulated_instance.instance_variable_get(:@redis).
|
290
|
+
@manipulated_instance.instance_variable_get(:@redis).with { |conn|
|
291
|
+
conn.call('SCRIPT', 'FLUSH')
|
292
|
+
}
|
276
293
|
end
|
277
294
|
|
278
295
|
it 'does not raise a RedisClient::CommandError: NOSCRIPT error' do
|
@@ -475,7 +492,7 @@ RSpec.describe Redlock::Client do
|
|
475
492
|
|
476
493
|
# Replace redis with unreachable instance
|
477
494
|
redis_instance = lock_manager.instance_variable_get(:@servers).first
|
478
|
-
|
495
|
+
_old_redis = redis_instance.instance_variable_get(:@redis)
|
479
496
|
redis_instance.instance_variable_set(:@redis, unreachable_redis)
|
480
497
|
|
481
498
|
expect {
|
data/spec/testing_spec.rb
CHANGED
@@ -11,7 +11,7 @@ RSpec.describe Redlock::Client do
|
|
11
11
|
describe '(testing mode)' do
|
12
12
|
describe 'try_lock_instances' do
|
13
13
|
context 'when testing with bypass mode' do
|
14
|
-
before {
|
14
|
+
before { Redlock::Client.testing_mode = :bypass }
|
15
15
|
|
16
16
|
it 'bypasses the redis servers' do
|
17
17
|
expect(lock_manager).to_not receive(:try_lock_instances_without_testing)
|
@@ -22,7 +22,7 @@ RSpec.describe Redlock::Client do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'when testing with fail mode' do
|
25
|
-
before {
|
25
|
+
before { Redlock::Client.testing_mode = :fail }
|
26
26
|
|
27
27
|
it 'fails' do
|
28
28
|
expect(lock_manager).to_not receive(:try_lock_instances_without_testing)
|
@@ -33,7 +33,7 @@ RSpec.describe Redlock::Client do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'when testing is disabled' do
|
36
|
-
before {
|
36
|
+
before { Redlock::Client.testing_mode = nil }
|
37
37
|
|
38
38
|
it 'works as usual' do
|
39
39
|
expect(lock_manager).to receive(:try_lock_instances_without_testing)
|
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: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leandro Moreira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis-client
|
@@ -96,22 +96,22 @@ dependencies:
|
|
96
96
|
name: rspec
|
97
97
|
requirement: !ruby/object:Gem::Requirement
|
98
98
|
requirements:
|
99
|
-
- - ">="
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
version: 3.0.0
|
102
99
|
- - "~>"
|
103
100
|
- !ruby/object:Gem::Version
|
104
101
|
version: '3'
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 3.0.0
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- - ">="
|
110
|
-
- !ruby/object:Gem::Version
|
111
|
-
version: 3.0.0
|
112
109
|
- - "~>"
|
113
110
|
- !ruby/object:Gem::Version
|
114
111
|
version: '3'
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: 3.0.0
|
115
115
|
description: Distributed lock using Redis written in Ruby. Highly inspired by https://github.com/antirez/redlock-rb.
|
116
116
|
email:
|
117
117
|
- leandro.ribeiro.moreira@gmail.com
|
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
requirements: []
|
161
|
-
rubygems_version: 3.
|
161
|
+
rubygems_version: 3.2.3
|
162
162
|
signing_key:
|
163
163
|
specification_version: 4
|
164
164
|
summary: Distributed lock using Redis written in Ruby.
|