Skip to content

Commit

Permalink
Merge pull request #143 from dhruvCW/use_monitor
Browse files Browse the repository at this point in the history
Use Monitor to make sure we call `RedisClient` in a thread safe way.
  • Loading branch information
leandromoreira committed Aug 20, 2023
2 parents be5d206 + 0596901 commit b299f34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,29 @@ lock_manager.get_remaining_ttl_for_resource(resource)

## Redis client configuration

`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.
`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.

```ruby
servers = [ 'redis://localhost:6379', RedisClient.new(:url => 'redis://someotherhost:6379') ]
redlock = Redlock::Client.new(servers)
```

Redlock works seamlessly with [redis sentinel](http://redis.io/topics/sentinel), which is supported in redis 3.2+.
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.

```ruby
config = {
name: "mymaster",
sentinels: [
{ host: "127.0.0.1", port: 26380 },
{ host: "127.0.0.1", port: 26381 },
],
role: :master
}
client = RedisClient.sentinel(**config).new_client
servers = [ config, client ]
redlock = Redlock::Client.new(servers)
```
Redlock supports the same configuration hash as `RedisClient`.

## Redlock configuration

Expand Down
17 changes: 12 additions & 5 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'monitor'
require 'redis-client'
require 'securerandom'

module Redlock
include Scripts

class LockAcquisitionError < StandardError
class LockAcquisitionError < LockError
attr_reader :errors

def initialize(message, errors)
Expand Down Expand Up @@ -163,6 +164,8 @@ def with
end

def initialize(connection)
@monitor = Monitor.new

if connection.respond_to?(:with)
@redis = connection
else
Expand Down Expand Up @@ -198,17 +201,21 @@ def initialize_client(options)
end
end

def synchronize
@monitor.synchronize { @redis.with { |connection| yield(connection) } }
end

def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.with { |conn|
synchronize { |conn|
conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
}
end
end

def unlock(resource, val)
recover_from_script_flush do
@redis.with { |conn|
synchronize { |conn|
conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
}
end
Expand All @@ -218,7 +225,7 @@ def unlock(resource, val)

def get_remaining_ttl(resource)
recover_from_script_flush do
@redis.with { |conn|
synchronize { |conn|
conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
}
end
Expand All @@ -235,7 +242,7 @@ def load_scripts
Scripts::PTTL_SCRIPT
]

@redis.with do |connnection|
synchronize do |connnection|
scripts.each do |script|
connnection.call('SCRIPT', 'LOAD', script)
end
Expand Down

0 comments on commit b299f34

Please sign in to comment.