redlock 2.0.2 → 2.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bdda4faebf87c2d1fb0026a3142497d5ae14092340bce6e35e46f51635e5c6a
4
- data.tar.gz: a9f5636898a2a3ece98b95a286bd88f417e68d119b1f1ad60bd75b5d1da94baf
3
+ metadata.gz: a0dee2cedbef9dc37a61a0708dc788287d3ab70b96bfd2561267ddd580e128f9
4
+ data.tar.gz: 7e6292d1b22149c3c538c7f3bbd0285a9a7c51ef472d2ec22e99190927da5967
5
5
  SHA512:
6
- metadata.gz: 0b10ec10f00ce9282d604b1d0c8a2e845d8d95a6684a5a235d6d39b598efa6324725c4d088e8016b11d473b0195a1e29a451e79fad115f04f6b217493be297cf
7
- data.tar.gz: e1d54bb8bb8a108e0989d1c11baa44cbfc4c971029fb23819d000758b42739698342ef803a826990757c888d113fb8a49663df1f3812603965c5498361596f86
6
+ metadata.gz: 4fccf4ff8a3c5647e828a71b6b982b67f5bc1cc702a88279752ce53fc88addc4f5ffaa657f4c7f5f5ca09bfb9ee27f2aee8f7551a9bfa655f176d99c98d76051
7
+ data.tar.gz: ef9d3a5f10ec4b62b41ee890a69eb91ff16d92832ca75d82fb1387145330f979d8d01b614bf0893ad29669056f479a44f64278effcdbcc74150be2a4a2258f3d
@@ -13,7 +13,7 @@ jobs:
13
13
 
14
14
  strategy:
15
15
  matrix:
16
- ruby-version: [3.1, "3.0", "2.7", "2.6", "2.5", "ruby-head"]
16
+ ruby-version: [3.2, 3.1, "3.0", "2.7", "2.6", "2.5", "ruby-head"]
17
17
 
18
18
  steps:
19
19
  - uses: actions/checkout@v2
data/README.md CHANGED
@@ -176,14 +176,29 @@ lock_manager.get_remaining_ttl_for_resource(resource)
176
176
 
177
177
  ## Redis client configuration
178
178
 
179
- `Redlock::Client` expects URLs or Redis objects on initialization. Redis objects should be used for configuring the connection in more detail, i.e. setting username and password.
179
+ `Redlock::Client` expects URLs, or configurations or Redis objects on initialization. Redis objects should be used for configuring the connection in more detail, i.e. setting username and password.
180
180
 
181
181
  ```ruby
182
182
  servers = [ 'redis://localhost:6379', RedisClient.new(:url => 'redis://someotherhost:6379') ]
183
183
  redlock = Redlock::Client.new(servers)
184
184
  ```
185
185
 
186
- Redlock works seamlessly with [redis sentinel](http://redis.io/topics/sentinel), which is supported in redis 3.2+.
186
+ To utilize `Redlock::Client` with sentinels you can pass an instance of `RedisClient` or just a configuration hash as part of the servers array during initialization.
187
+
188
+ ```ruby
189
+ config = {
190
+ name: "mymaster",
191
+ sentinels: [
192
+ { host: "127.0.0.1", port: 26380 },
193
+ { host: "127.0.0.1", port: 26381 },
194
+ ],
195
+ role: :master
196
+ }
197
+ client = RedisClient.sentinel(**config).new_client
198
+ servers = [ config, client ]
199
+ redlock = Redlock::Client.new(servers)
200
+ ```
201
+ Redlock supports the same configuration hash as `RedisClient`.
187
202
 
188
203
  ## Redlock configuration
189
204
 
@@ -1,10 +1,11 @@
1
+ require 'monitor'
1
2
  require 'redis-client'
2
3
  require 'securerandom'
3
4
 
4
5
  module Redlock
5
6
  include Scripts
6
7
 
7
- class LockAcquisitionError < StandardError
8
+ class LockAcquisitionError < LockError
8
9
  attr_reader :errors
9
10
 
10
11
  def initialize(message, errors)
@@ -163,21 +164,50 @@ module Redlock
163
164
  end
164
165
 
165
166
  def initialize(connection)
167
+ @monitor = Monitor.new
168
+
166
169
  if connection.respond_to?(:with)
167
170
  @redis = connection
168
171
  else
169
172
  if connection.respond_to?(:client)
170
173
  @redis = connection
171
174
  else
172
- @redis = RedisClient.new(connection)
175
+ @redis = initialize_client(connection)
173
176
  end
174
177
  @redis.extend(ConnectionPoolLike)
175
178
  end
176
179
  end
177
180
 
181
+ def initialize_client(options)
182
+ if options.key?(:sentinels)
183
+ if url = options.delete(:url)
184
+ uri = URI.parse(url)
185
+ if !options.key?(:name) && uri.host
186
+ options[:name] = uri.host
187
+ end
188
+
189
+ if !options.key?(:password) && uri.password && !uri.password.empty?
190
+ options[:password] = uri.password
191
+ end
192
+
193
+ if !options.key?(:username) && uri.user && !uri.user.empty?
194
+ options[:username] = uri.user
195
+ end
196
+ end
197
+
198
+ RedisClient.sentinel(**options).new_client
199
+ else
200
+ RedisClient.config(**options).new_client
201
+ end
202
+ end
203
+
204
+ def synchronize
205
+ @monitor.synchronize { @redis.with { |connection| yield(connection) } }
206
+ end
207
+
178
208
  def lock(resource, val, ttl, allow_new_lock)
179
209
  recover_from_script_flush do
180
- @redis.with { |conn|
210
+ synchronize { |conn|
181
211
  conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
182
212
  }
183
213
  end
@@ -185,7 +215,7 @@ module Redlock
185
215
 
186
216
  def unlock(resource, val)
187
217
  recover_from_script_flush do
188
- @redis.with { |conn|
218
+ synchronize { |conn|
189
219
  conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
190
220
  }
191
221
  end
@@ -195,7 +225,7 @@ module Redlock
195
225
 
196
226
  def get_remaining_ttl(resource)
197
227
  recover_from_script_flush do
198
- @redis.with { |conn|
228
+ synchronize { |conn|
199
229
  conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
200
230
  }
201
231
  end
@@ -212,7 +242,7 @@ module Redlock
212
242
  Scripts::PTTL_SCRIPT
213
243
  ]
214
244
 
215
- @redis.with do |connnection|
245
+ synchronize do |connnection|
216
246
  scripts.each do |script|
217
247
  connnection.call('SCRIPT', 'LOAD', script)
218
248
  end
@@ -1,3 +1,3 @@
1
1
  module Redlock
2
- VERSION = '2.0.2'
2
+ VERSION = '2.0.4'
3
3
  end
data/redlock.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'redis-client', '~> 0.14.1'
21
+ spec.add_dependency 'redis-client', '>= 0.14.1', '< 1.0.0'
22
22
 
23
23
  spec.add_development_dependency 'connection_pool', '~> 2.2'
24
24
  spec.add_development_dependency 'coveralls', '~> 0.8'
data/spec/client_spec.rb CHANGED
@@ -59,6 +59,16 @@ RSpec.describe Redlock::Client do
59
59
  lock_manager.unlock(lock_info)
60
60
  end
61
61
 
62
+ it 'accepts Configuration hashes' do
63
+ config = { url: "redis://#{redis1_host}:#{redis1_port}" }
64
+ _redlock = Redlock::Client.new([config])
65
+
66
+ lock_info = lock_manager.lock(resource_key, ttl)
67
+ expect(lock_info).to be_a(Hash)
68
+ expect(resource_key).to_not be_lockable(lock_manager, ttl)
69
+ lock_manager.unlock(lock_info)
70
+ end
71
+
62
72
  it 'does not load scripts' do
63
73
  redis_client.call('SCRIPT', 'FLUSH')
64
74
 
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Moreira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-15 00:00:00.000000000 Z
11
+ date: 2023-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.14.1
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 0.14.1
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: connection_pool
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -144,7 +150,7 @@ homepage: https://github.com/leandromoreira/redlock-rb
144
150
  licenses:
145
151
  - BSD-2-Clause
146
152
  metadata: {}
147
- post_install_message:
153
+ post_install_message:
148
154
  rdoc_options: []
149
155
  require_paths:
150
156
  - lib
@@ -159,8 +165,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
165
  - !ruby/object:Gem::Version
160
166
  version: '0'
161
167
  requirements: []
162
- rubygems_version: 3.3.7
163
- signing_key:
168
+ rubygems_version: 3.4.10
169
+ signing_key:
164
170
  specification_version: 4
165
171
  summary: Distributed lock using Redis written in Ruby.
166
172
  test_files: