-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2028 from internetee/1017-rate-throttling
Add request throttling
- Loading branch information
Showing
74 changed files
with
1,638 additions
and
20 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shunter | ||
module_function | ||
|
||
class ThrottleError < StandardError; end | ||
|
||
BASE_LOGGER = ::Logger.new($stdout) | ||
ONE_MINUTE = 60 | ||
ONE_HUNDRED_REQUESTS = 100 | ||
|
||
BASE_CONNECTION = { | ||
host: ENV['shunter_redis_host'] || 'redis', | ||
port: (ENV['shunter_redis_port'] || '6379').to_i, | ||
}.freeze | ||
|
||
def default_error_message | ||
"Session limit exceeded. Current limit is #{default_threshold} in #{default_timespan} seconds" | ||
end | ||
|
||
def default_timespan | ||
ENV['shunter_default_timespan'] || ONE_MINUTE | ||
end | ||
|
||
def default_threshold | ||
ENV['shunter_default_threshold'] || ONE_HUNDRED_REQUESTS | ||
end | ||
|
||
def default_adapter | ||
ENV['shunter_default_adapter'] || 'Shunter::Adapters::Redis' | ||
end | ||
|
||
def feature_enabled? | ||
ActiveModel::Type::Boolean.new.cast(ENV['shunter_enabled'] || 'false') | ||
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shunter | ||
module Adapters | ||
class Memory | ||
attr_reader :store | ||
|
||
def initialize(_options = {}) | ||
@@store ||= {} | ||
end | ||
|
||
def find_counter(key) | ||
@@store[key] | ||
end | ||
|
||
def write_counter(key) | ||
@@store[key] = 1 | ||
end | ||
|
||
def increment_counter(key) | ||
@@store[key] += 1 | ||
end | ||
|
||
def clear! | ||
@@store = {} | ||
end | ||
|
||
def expire_counter(_key, _timespan); end | ||
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shunter | ||
module Adapters | ||
class Redis | ||
attr_reader :redis | ||
|
||
def initialize(options) | ||
@redis = ::Redis.new(options) | ||
end | ||
|
||
def find_counter(key) | ||
@redis.get(key) | ||
end | ||
|
||
def write_counter(key) | ||
@redis.set(key, 1) | ||
end | ||
|
||
def increment_counter(key) | ||
@redis.incr(key) | ||
end | ||
|
||
def expire_counter(key, timespan) | ||
@redis.expire(key, timespan) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.