redlock 2.0.3 → 2.0.5

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: 7672717848770741429fd13a59ea4b6a6f734ffd8b519a9c3e27c5562f553cec
4
+ data.tar.gz: 627314f12eaf57b1e44bde9cfe2388716705c93b27f95479aa5c70553b3c754b
5
5
  SHA512:
6
- metadata.gz: 79ba3e5ab3dc1704b2af22efb3f038a43b8b9fd773492ba8e6d8f8e0b162cdd4df3671e2267ac80ff3e1f1a3d0edb3b3cc29430637f6d3efa146fd1f6d4fd100
7
- data.tar.gz: '0929d107e6ffb002498a26b5afe0d59e41eacc7265ade1402584b1c25a52509ddbedd40eb24acfacfefa718f343ffe9f055adff4f3f7b390e61971543dea4451'
6
+ metadata.gz: 31b8beb93afa3cd62e741637aa221b71522810a8df54d33ea54874eed7d8fac894da1622f97528704027b870dd7945bdb53dd7424fbfb47c47430f6f659d2a90
7
+ data.tar.gz: 3602f85cafd155cdaef70499231a0885f1df13f58abc1859c526d11f43ea303a2ee746928166002b5495e53b5ce8fa84700d76c583463bed3adef25da263c5d8
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)
@@ -156,22 +157,19 @@ module Redlock
156
157
  private
157
158
 
158
159
  class RedisInstance
159
- module ConnectionPoolLike
160
- def with
161
- yield self
162
- end
163
- end
164
-
165
160
  def initialize(connection)
166
- if connection.respond_to?(:with)
161
+ @monitor = Monitor.new
162
+
163
+ if connection.respond_to?(:call)
167
164
  @redis = connection
168
165
  else
169
166
  if connection.respond_to?(:client)
170
167
  @redis = connection
171
- else
168
+ elsif connection.respond_to?(:key?)
172
169
  @redis = initialize_client(connection)
170
+ else
171
+ @redis = connection
173
172
  end
174
- @redis.extend(ConnectionPoolLike)
175
173
  end
176
174
  end
177
175
 
@@ -198,9 +196,13 @@ module Redlock
198
196
  end
199
197
  end
200
198
 
199
+ def synchronize
200
+ @monitor.synchronize { @redis.then { |connection| yield(connection) } }
201
+ end
202
+
201
203
  def lock(resource, val, ttl, allow_new_lock)
202
204
  recover_from_script_flush do
203
- @redis.with { |conn|
205
+ synchronize { |conn|
204
206
  conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
205
207
  }
206
208
  end
@@ -208,7 +210,7 @@ module Redlock
208
210
 
209
211
  def unlock(resource, val)
210
212
  recover_from_script_flush do
211
- @redis.with { |conn|
213
+ synchronize { |conn|
212
214
  conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
213
215
  }
214
216
  end
@@ -218,7 +220,7 @@ module Redlock
218
220
 
219
221
  def get_remaining_ttl(resource)
220
222
  recover_from_script_flush do
221
- @redis.with { |conn|
223
+ synchronize { |conn|
222
224
  conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
223
225
  }
224
226
  end
@@ -235,7 +237,7 @@ module Redlock
235
237
  Scripts::PTTL_SCRIPT
236
238
  ]
237
239
 
238
- @redis.with do |connnection|
240
+ synchronize do |connnection|
239
241
  scripts.each do |script|
240
242
  connnection.call('SCRIPT', 'LOAD', script)
241
243
  end
@@ -1,3 +1,3 @@
1
1
  module Redlock
2
- VERSION = '2.0.3'
2
+ VERSION = '2.0.5'
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -30,11 +30,7 @@ RSpec.describe Redlock::Client do
30
30
  let(:redis3_host) { ENV["REDIS3_HOST"] || "127.0.0.1" }
31
31
  let(:redis3_port) { ENV["REDIS3_PORT"] || "6379" }
32
32
  let(:unreachable_redis) {
33
- redis = RedisClient.new(url: 'redis://localhost:46864')
34
- def redis.with
35
- yield self
36
- end
37
- redis
33
+ RedisClient.new(url: 'redis://localhost:46864')
38
34
  }
39
35
 
40
36
  describe 'initialize' 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: 2.0.3
4
+ version: 2.0.5
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-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client