Class: Bearcat::RateLimiting::RedisLimiter
- Inherits:
-
RateLimiter
- Object
- RateLimiter
- Bearcat::RateLimiting::RedisLimiter
- 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
Instance Method Summary collapse
- #checkin_known(key, remaining_time) ⇒ Object
- #increment(key, amount, pending_amount = 0) ⇒ Object
- #redis(&blk) ⇒ Object
Methods inherited from RateLimiter
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, = INCR.call(r, ["#{key}|reported"], [amount, ts, LEAK_RATE, MAXIMUM + 300]) { count: actual.to_f, timestamp: .to_f, } end end |