redlock 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb95cd103dd1856b7c75459b121bd5905d2884f520850552552bdb1c39f7502b
4
- data.tar.gz: 165945b2aca8e081ad2704fa9eb699bd2e08e75f973fc7ae802163eed88dce8b
3
+ metadata.gz: a0dee2cedbef9dc37a61a0708dc788287d3ab70b96bfd2561267ddd580e128f9
4
+ data.tar.gz: 7e6292d1b22149c3c538c7f3bbd0285a9a7c51ef472d2ec22e99190927da5967
5
5
  SHA512:
6
- metadata.gz: 79ba3e5ab3dc1704b2af22efb3f038a43b8b9fd773492ba8e6d8f8e0b162cdd4df3671e2267ac80ff3e1f1a3d0edb3b3cc29430637f6d3efa146fd1f6d4fd100
7
- data.tar.gz: '0929d107e6ffb002498a26b5afe0d59e41eacc7265ade1402584b1c25a52509ddbedd40eb24acfacfefa718f343ffe9f055adff4f3f7b390e61971543dea4451'
6
+ metadata.gz: 4fccf4ff8a3c5647e828a71b6b982b67f5bc1cc702a88279752ce53fc88addc4f5ffaa657f4c7f5f5ca09bfb9ee27f2aee8f7551a9bfa655f176d99c98d76051
7
+ data.tar.gz: ef9d3a5f10ec4b62b41ee890a69eb91ff16d92832ca75d82fb1387145330f979d8d01b614bf0893ad29669056f479a44f64278effcdbcc74150be2a4a2258f3d
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,6 +164,8 @@ 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
@@ -198,9 +201,13 @@ module Redlock
198
201
  end
199
202
  end
200
203
 
204
+ def synchronize
205
+ @monitor.synchronize { @redis.with { |connection| yield(connection) } }
206
+ end
207
+
201
208
  def lock(resource, val, ttl, allow_new_lock)
202
209
  recover_from_script_flush do
203
- @redis.with { |conn|
210
+ synchronize { |conn|
204
211
  conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
205
212
  }
206
213
  end
@@ -208,7 +215,7 @@ module Redlock
208
215
 
209
216
  def unlock(resource, val)
210
217
  recover_from_script_flush do
211
- @redis.with { |conn|
218
+ synchronize { |conn|
212
219
  conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
213
220
  }
214
221
  end
@@ -218,7 +225,7 @@ module Redlock
218
225
 
219
226
  def get_remaining_ttl(resource)
220
227
  recover_from_script_flush do
221
- @redis.with { |conn|
228
+ synchronize { |conn|
222
229
  conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
223
230
  }
224
231
  end
@@ -235,7 +242,7 @@ module Redlock
235
242
  Scripts::PTTL_SCRIPT
236
243
  ]
237
244
 
238
- @redis.with do |connnection|
245
+ synchronize do |connnection|
239
246
  scripts.each do |script|
240
247
  connnection.call('SCRIPT', 'LOAD', script)
241
248
  end
@@ -1,3 +1,3 @@
1
1
  module Redlock
2
- VERSION = '2.0.3'
2
+ VERSION = '2.0.4'
3
3
  end
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.3
4
+ version: 2.0.4
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-08-18 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