-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move from proxies to well-defined adapters
- Loading branch information
Showing
34 changed files
with
325 additions
and
531 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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
class Attack | ||
class StoreAdapter | ||
class << self | ||
def adapters | ||
@@adapters ||= [] | ||
end | ||
|
||
def inherited(klass) | ||
adapters << klass | ||
end | ||
|
||
def lookup(store) | ||
adapters.find { |adapter| adapter.handle?(store) } | ||
end | ||
|
||
def handle?(store) | ||
raise NotImplementedError | ||
end | ||
end | ||
|
||
attr_reader :store | ||
|
||
def initialize(store) | ||
@store = store | ||
end | ||
|
||
def read(key) | ||
raise NotImplementedError | ||
end | ||
|
||
def write(key, value, options = {}) | ||
raise NotImplementedError | ||
end | ||
|
||
def increment(key, amount, options = {}) | ||
raise NotImplementedError | ||
end | ||
|
||
def delete(key, options = {}) | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
lib/rack/attack/store_adapters/active_support_redis_store_adapter.rb
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rack/attack/store_adapter' | ||
|
||
module Rack | ||
class Attack | ||
module StoreAdapters | ||
class ActiveSupportRedisStoreAdapter < StoreAdapter | ||
def self.handle?(store) | ||
defined?(::Redis) && | ||
defined?(::ActiveSupport::Cache::RedisStore) && | ||
store.is_a?(::ActiveSupport::Cache::RedisStore) | ||
end | ||
|
||
def read(key, options = {}) | ||
store.read(key, options.merge!(raw: true)) | ||
end | ||
|
||
def write(key, value, options = {}) | ||
store.write(key, value, options.merge!(raw: true)) | ||
end | ||
|
||
def increment(key, amount = 1, options = {}) | ||
# #increment ignores options[:expires_in]. | ||
# | ||
# So in order to workaround this we use #write (which sets expiration) to initialize | ||
# the counter. After that we continue using the original #increment. | ||
if options[:expires_in] && !read(key) | ||
write(key, amount, options) | ||
|
||
amount | ||
else | ||
store.increment(key, amount, options) | ||
end | ||
end | ||
|
||
def delete(key, options = {}) | ||
store.delete(key, options) | ||
end | ||
|
||
def delete_matched(matcher, options = nil) | ||
store.delete_matched(matcher, options) | ||
end | ||
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
13 changes: 8 additions & 5 deletions
13
...tack/store_proxy/mem_cache_store_proxy.rb → ...store_adapters/mem_cache_store_adapter.rb
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
Oops, something went wrong.