lib/millrace/rate_limit.rb in millrace-0.1.0 vs lib/millrace/rate_limit.rb in millrace-0.1.1

- old
+ new

@@ -1,6 +1,6 @@ -require "digest" +girequire "digest" require "prorate" module Millrace class RateLimit def initialize(name:, rate:, window:, penalty: 0, redis_config: nil) @@ -13,13 +13,13 @@ attr_reader :name, :rate, :window def before(controller) bucket = get_bucket(controller.request.remote_ip) - level = record_request(bucket) + level = bucket.fillup(1).level - return unless level > threshold + return if level < threshold if level - 1 < threshold level = bucket.fillup(penalty).level end @@ -28,13 +28,9 @@ private def retry_after(level) ((level - threshold) / rate).to_i - end - - def record_request(bucket) - bucket.fillup(1).level end def get_bucket(ip) Prorate::LeakyBucket.new( redis: redis,