Class: Bearcat::RateLimiting::RedisLimiter

Inherits:
RateLimiter
  • Object
show all
Defined in:
lib/bearcat/rate_limiting.rb

Constant Summary collapse

INCR =
RedisScript.new(Pathname.new(__FILE__) + "../rate_limiting/increment_bucket.lua")

Instance Attribute Summary

Attributes inherited from RateLimiter

#namespace

Instance Method Summary collapse

Methods inherited from RateLimiter

#apply, #initialize

Constructor Details

This class inherits a constructor from Bearcat::RateLimiting::RateLimiter

Instance Method Details

#checkin_known(key, remaining_time) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/bearcat/rate_limiting.rb', line 51

def checkin_known(key, remaining_time)
  redis do |r|
    r.mapped_hmset("#{key}|reported", {
      count: MAXIMUM + 200 - remaining_time,
      timestamp: Time.now.to_f,
    })
  end
end

#increment(key, amount, pending_amount = 0) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bearcat/rate_limiting.rb', line 39

def increment(key, amount, pending_amount = 0)
  ts = Time.now.to_f

  redis do |r|
    actual, timestamp = INCR.call(r, ["#{key}|reported"], [amount, ts, LEAK_RATE, MAXIMUM + 300])
    {
      count: actual.to_f,
      timestamp: timestamp.to_f,
    }
  end
end

#redis(&blk) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/bearcat/rate_limiting.rb', line 60

def redis(&blk)
  if @params[:redis]
    @params[:redis].call(blk)
  else
    ::Bearcat.redis(&blk)
  end
end