module Debounce

Constants

VERSION

Public Class Methods

call(key, timeout: 1, stale_client_timeout: 600) { || ... } click to toggle source
# File lib/debounce.rb, line 20
def self.call(key, timeout: 1, stale_client_timeout: 600) # 10 minutes
  lock_key = "DEBOUNCE:#{key}"
  semaphore1 = Redis::Semaphore.new("#{lock_key}-1", resources: 2, stale_client_timeout: stale_client_timeout, redis: redis.dup, expiration: timeout + 1)
  semaphore1.lock(timeout) do
    start = Time.now
    semaphore2 = Redis::Semaphore.new("#{lock_key}-2", resources: 1, stale_client_timeout: stale_client_timeout, redis: redis.dup, expiration: timeout + 1)
    semaphore2.lock(timeout) do
      yield
    end
    duration = Time.now - start
    sleep timeout - duration.to_i if duration < timeout
  end
end
redis() click to toggle source
# File lib/debounce.rb, line 8
def self.redis
  @redis ||= Debounce::RedisConnection.create
end
redis=(hash) click to toggle source
# File lib/debounce.rb, line 12
def self.redis=(hash)
  @redis = if hash.is_a?(ConnectionPool)
    hash
  else
    Debounce::RedisConnection.create(hash)
  end
end