class UniqueThread::Locksmith

Constants

RedisResult

Attributes

lua_scripts[R]
name[R]
stopwatch[R]

Public Class Methods

new(name:, stopwatch:) click to toggle source
# File lib/unique_thread/locksmith.rb, line 7
def initialize(name:, stopwatch:)
  @name      = name
  @stopwatch = stopwatch

  @lua_scripts = Hash[Dir[File.join(__dir__, 'redis_lua', '*.lua')].map do |lua_file|
    [File.basename(lua_file, '.lua').to_sym, UniqueThread.redis.script(:load, File.read(lua_file))]
  end]
end

Public Instance Methods

new_lock() click to toggle source
# File lib/unique_thread/locksmith.rb, line 16
def new_lock
  lock_from_redis_command(:get_lock, name, stopwatch.now, stopwatch.next_renewal)
end
renew_lock(lock) click to toggle source
# File lib/unique_thread/locksmith.rb, line 20
def renew_lock(lock)
  lock_from_redis_command(:extend_lock, name, lock.locked_until, stopwatch.next_renewal)
end

Private Instance Methods

lock_from_redis_command(script, *args) click to toggle source
# File lib/unique_thread/locksmith.rb, line 34
def lock_from_redis_command(script, *args)
  redis_result = RedisResult.new(*UniqueThread.redis.evalsha(lua_scripts[script], args))

  klass = if redis_result.lock_acquired?
            HeldLock
          else
            Lock
          end

  klass.new(redis_result.locked_until, stopwatch, self)
end