Module: Debounce

Defined in:
lib/debounce.rb,
lib/debounce/version.rb,
lib/debounce/redis_connection.rb

Defined Under Namespace

Classes: RedisConnection

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.call(key, timeout: 1, stale_client_timeout: 600) ⇒ Object

10 minutes



20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

.redisObject



8
9
10
# File 'lib/debounce.rb', line 8

def self.redis
  @redis ||= Debounce::RedisConnection.create
end

.redis=(hash) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/debounce.rb', line 12

def self.redis=(hash)
  @redis = if hash.is_a?(ConnectionPool)
    hash
  else
    Debounce::RedisConnection.create(hash)
  end
end