Skip to content

Commit

Permalink
chore: note it is safe to auto-reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Feb 16, 2023
1 parent 983ceff commit 99a24b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Redlock works with Redis versions 6.0 or later.

Redlock >= 2.0 only works with `RedisClient` instance.

If you'd like to enable auto-reconnect attempts like in Redis 5,
be sure to instantiate a RedisClient with `reconnect_attempts: 1`.

## Installation

Add this line to your application's Gemfile:
Expand Down
8 changes: 6 additions & 2 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ def initialize(connection)
def lock(resource, val, ttl, allow_new_lock)
recover_from_script_flush do
@redis.with { |conn|
conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock)
# NOTE: is not idempotent and unsafe to retry
conn.call_once('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|
conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
# NOTE: is not idempotent and unsafe to retry
conn.call_once('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val)
}
end
rescue
Expand All @@ -187,6 +189,7 @@ def unlock(resource, val)
def get_remaining_ttl(resource)
recover_from_script_flush do
@redis.with { |conn|
# NOTE: is idempotent and safe to retry
conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource)
}
end
Expand All @@ -205,6 +208,7 @@ def load_scripts

@redis.with do |connnection|
scripts.each do |script|
# NOTE: is idempotent and safe to retry
connnection.call('SCRIPT', 'LOAD', script)
end
end
Expand Down

0 comments on commit 99a24b2

Please sign in to comment.