-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impliments a leaky bucket rate limiter, that unlike prorate continues to to count requests against the limit even when the rate limiter is in the blocking state. This means that the client has to slow down, or they will remain blocked indefinately. Optionally a penalty can be added, that adds additonal tokens to the bucket at the point that the limit is breached, to futher ensure that the block lasts longer for clients that are only marginly breaching the rate limit.
- Loading branch information
Showing
8 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "millrace/version" | ||
require_relative "millrace/rate_limited" | ||
require_relative "millrace/rate_limit" | ||
|
||
module Millrace | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "digest" | ||
require "prorate" | ||
|
||
module Millrace | ||
class RateLimit | ||
def initialize(name:, rate:, window:, penalty: 0, redis_config: nil) | ||
@name = name | ||
@rate = rate | ||
@window = window | ||
@penalty = penalty | ||
@redis_config = redis_config | ||
end | ||
|
||
attr_reader :name, :rate, :window | ||
|
||
def before(controller) | ||
bucket = get_bucket(controller.request.remote_ip) | ||
level = record_request(bucket) | ||
|
||
return unless level > threshold | ||
|
||
if level - 1 < threshold | ||
level = bucket.fillup(penalty).level | ||
end | ||
|
||
raise RateLimited.new(limit_name: name, retry_after: retry_after(level)) | ||
end | ||
|
||
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, | ||
redis_key_prefix: key(ip), | ||
leak_rate: rate, | ||
bucket_capacity: capacity, | ||
) | ||
end | ||
|
||
def key(ip) | ||
"millrace.#{name}.#{Digest::SHA1.hexdigest(ip)}" | ||
end | ||
|
||
def capacity | ||
(threshold * 2) + penalty | ||
end | ||
|
||
def threshold | ||
window * rate | ||
end | ||
|
||
def penalty | ||
@penalty * rate | ||
end | ||
|
||
def redis_config | ||
@redis_config || { url: ENV.fetch("MILLRACE_REDIS_URL", nil) }.compact | ||
end | ||
|
||
def redis | ||
Thread.current["millrace_#{name}_redis"] ||= Redis.new(redis_config) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Millrace | ||
class RateLimited < StandardError | ||
def initialize(limit_name:, retry_after:) | ||
@limit_name = limit_name | ||
@retry_after = retry_after | ||
end | ||
|
||
attr_reader :limit_name, :retry_after | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Millrace::RateLimit do | ||
let(:subject) do | ||
described_class.new( | ||
name: "test", | ||
rate: 10, | ||
window: 2, | ||
penalty: penalty, | ||
) | ||
end | ||
|
||
let(:penalty) { 1 } | ||
|
||
let(:controller) do | ||
double(:controller, request: double(:request, remote_ip: to_s)) | ||
end | ||
|
||
describe "#before" do | ||
it "rate limits" do | ||
# Fill the bucket | ||
20.times { subject.before(controller) } | ||
|
||
# hit the threshold and get a penalty | ||
expect { subject.before(controller) }.to raise_error Millrace::RateLimited | ||
|
||
sleep 1 | ||
# Still blocked for the penalty duration | ||
expect { subject.before(controller) }.to raise_error Millrace::RateLimited | ||
|
||
# Not blocked after the penalty duration is over | ||
sleep 1 | ||
subject.before(controller) | ||
end | ||
|
||
it "returns an exeption with the correct name" do | ||
# Fill the bucket | ||
20.times { subject.before(controller) } | ||
|
||
# hit the threshold and get an error | ||
expect { subject.before(controller) }.to raise_error do |exception| | ||
expect(exception.limit_name).to eq "test" | ||
end | ||
end | ||
|
||
it "returns an exeption with the correct retry time" do | ||
# Fill the bucket | ||
20.times { subject.before(controller) } | ||
|
||
# hit the threshold and get an error | ||
expect { subject.before(controller) }.to raise_error do |exception| | ||
expect(exception.retry_after).to eq 1 | ||
end | ||
end | ||
|
||
context "a longer penalty" do | ||
let(:penalty) { 10 } | ||
|
||
it "returns an exeption with the correct retry time" do | ||
# Fill the bucket | ||
20.times { subject.before(controller) } | ||
|
||
# hit the threshold and get an error | ||
expect { subject.before(controller) }.to raise_error do |exception| | ||
expect(exception.retry_after).to eq 10 | ||
end | ||
end | ||
end | ||
|
||
context "additional requests" do | ||
let(:penalty) { 0 } | ||
|
||
it "returns an exeption with the correct retry time" do | ||
# Fill the bucket | ||
40.times do | ||
subject.before(controller) | ||
# Keep making requests even though we are rate limited | ||
rescue Millrace::RateLimited | ||
nil | ||
end | ||
|
||
# hit the threshold and get an error | ||
expect { subject.before(controller) }.to raise_error do |exception| | ||
expect(exception.retry_after).to eq 2 | ||
end | ||
end | ||
end | ||
end | ||
end |