Class: Redstream::Lock Private

Inherits:
Object
  • Object
show all
Defined in:
lib/redstream/lock.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

As the name suggests, the Redstream::Lock class implements a redis based locking mechanism. It atomically (lua script) gets/sets the lock key and updates its expire timeout, in case it currently holds the lock. Moreover, once it got the lock, it tries to keep it by updating the lock expire timeout from within a thread every 3 seconds.

Examples:

lock = Redstream::Lock.new(name: "user_stream_lock")

loop do
  got_lock = lock.acquire do
    # ...
  end

  sleep(5) unless got_lock
end

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Lock

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Lock.



24
25
26
27
# File 'lib/redstream/lock.rb', line 24

def initialize(name:)
  @name = name
  @id = SecureRandom.hex
end

Instance Method Details

#acquire(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
# File 'lib/redstream/lock.rb', line 29

def acquire(&block)
  got_lock = get_lock
  keep_lock(&block) if got_lock
  got_lock
end