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

Failed to acquire lock on #150

Open
ppascualv opened this issue Apr 9, 2024 · 3 comments
Open

Failed to acquire lock on #150

ppascualv opened this issue Apr 9, 2024 · 3 comments

Comments

@ppascualv
Copy link

ppascualv commented Apr 9, 2024

I'm receiving a Redlock::LockError Failed to acquire lock on "name-of-key" sometimes using the gem. I know this error comes from Redis but I would like to know how can I prevent it.
This is the code that we have at the moment:

class Locker
  extend Memoist

  DEFAULT_EXPIRATION = 5.seconds

  def with_lock(key, expiration: DEFAULT_EXPIRATION, retries: 1, wait: 0.1.seconds, &block)
    current_retry ||= 0
    lock!(key, expiration:, &block)
  rescue Redlock::LockError => e
    current_retry += 1
    raise e if current_retry >= retries

    sleep(wait)
    retry
  end

  private

  memoize def lock_manager
    Redlock::Client.new([Rails.application.credentials.redis.url])
  end

  def lock!(key, expiration: DEFAULT_EXPIRATION, &block)
    lock_manager.lock!(key, (expiration * 1000).to_i, &block)
  end
end

And this is how we call the lock:

Locker.new.with_lock("key") do
    action
end
@venalen
Copy link

venalen commented Oct 4, 2024

Probably way too late on this, but I managed to get around this by explicitly using the RedisClient. So for your application:

Redlock::Client.new([ RedisClient.new(url: Rails.application.credentials.redis.url) ])

I noticed this nuance in the Compatibility section of the docs

@ppascualv
Copy link
Author

I'm going to try it, thanks @venalen !

@ppascualv
Copy link
Author

still happening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants