Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Monitor to make sure we call RedisClient in a thread safe way. #143

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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