mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Merge pull request #364 from grzuy/dont_require_redis_for_memory_store
[Fixes #355] Don't require redis if not necessary
This commit is contained in:
commit
01e8bf640a
19 changed files with 284 additions and 194 deletions
|
|
@ -27,6 +27,11 @@ gemfile:
|
||||||
- gemfiles/rails_5_1.gemfile
|
- gemfiles/rails_5_1.gemfile
|
||||||
- gemfiles/rails_4_2.gemfile
|
- gemfiles/rails_4_2.gemfile
|
||||||
- gemfiles/dalli2.gemfile
|
- gemfiles/dalli2.gemfile
|
||||||
|
- gemfiles/connection_pool_dalli.gemfile
|
||||||
|
- gemfiles/active_support_redis_cache_store.gemfile
|
||||||
|
- gemfiles/active_support_redis_cache_store_pooled.gemfile
|
||||||
|
- gemfiles/redis_store.gemfile
|
||||||
|
- gemfiles/active_support_redis_store.gemfile
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
|
|
|
||||||
24
Appraisals
24
Appraisals
|
|
@ -32,3 +32,27 @@ end
|
||||||
appraise 'dalli2' do
|
appraise 'dalli2' do
|
||||||
gem 'dalli', '~> 2.0'
|
gem 'dalli', '~> 2.0'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
appraise "connection_pool_dalli" do
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "dalli", "~> 2.7"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_cache_store" do
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_cache_store_pooled" do
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "redis_store" do
|
||||||
|
gem "redis-store", "~> 1.5"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_store" do
|
||||||
|
gem "redis-activesupport", "~> 5.0"
|
||||||
|
end
|
||||||
|
|
|
||||||
8
gemfiles/active_support_redis_cache_store.gemfile
Normal file
8
gemfiles/active_support_redis_cache_store.gemfile
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
9
gemfiles/active_support_redis_cache_store_pooled.gemfile
Normal file
9
gemfiles/active_support_redis_cache_store_pooled.gemfile
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
7
gemfiles/active_support_redis_store.gemfile
Normal file
7
gemfiles/active_support_redis_store.gemfile
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "redis-activesupport", "~> 5.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
8
gemfiles/connection_pool_dalli.gemfile
Normal file
8
gemfiles/connection_pool_dalli.gemfile
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "dalli", "~> 2.7"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
7
gemfiles/redis_store.gemfile
Normal file
7
gemfiles/redis_store.gemfile
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "redis-store", "~> 1.5"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
|
|
@ -5,7 +5,7 @@ module Rack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class RedisCacheStoreProxy < SimpleDelegator
|
class RedisCacheStoreProxy < SimpleDelegator
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
defined?(::ActiveSupport::Cache::RedisCacheStore) && store.is_a?(::ActiveSupport::Cache::RedisCacheStore)
|
defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore) && store.is_a?(::ActiveSupport::Cache::RedisCacheStore)
|
||||||
end
|
end
|
||||||
|
|
||||||
def increment(name, amount = 1, options = {})
|
def increment(name, amount = 1, options = {})
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,4 @@ Gem::Specification.new do |s|
|
||||||
# which rack-attack uses only for testing compatibility in test suite.
|
# which rack-attack uses only for testing compatibility in test suite.
|
||||||
s.add_development_dependency 'actionpack', '>= 3.0.0'
|
s.add_development_dependency 'actionpack', '>= 3.0.0'
|
||||||
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
||||||
s.add_development_dependency 'connection_pool'
|
|
||||||
s.add_development_dependency 'dalli'
|
|
||||||
s.add_development_dependency 'redis-activesupport'
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "active_support/cache/dalli_store"
|
if defined?(::Dalli)
|
||||||
require "timecop"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "active_support/cache/dalli_store"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::DalliStore as a cache backend" do
|
describe "ActiveSupport::Cache::DalliStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::DalliStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::DalliStore.new
|
||||||
end
|
end
|
||||||
|
|
@ -36,4 +37,5 @@ describe "ActiveSupport::Cache::DalliStore as a cache backend" do
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "timecop"
|
if defined?(::Dalli)
|
||||||
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::MemCacheStore as a cache backend" do
|
describe "ActiveSupport::Cache::MemCacheStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemCacheStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemCacheStore.new
|
||||||
end
|
end
|
||||||
|
|
@ -35,4 +36,5 @@ describe "ActiveSupport::Cache::MemCacheStore as a cache backend" do
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.get(key)
|
assert_nil Rack::Attack.cache.store.get(key)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "timecop"
|
if defined?(::ConnectionPool) && defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
|
||||||
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
if ActiveSupport.version >= Gem::Version.new("5.2.0")
|
|
||||||
describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do
|
describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2)
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "timecop"
|
if defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
|
||||||
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
if ActiveSupport.version >= Gem::Version.new("5.2.0")
|
|
||||||
describe "ActiveSupport::Cache::RedisCacheStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisCacheStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "redis-activesupport"
|
if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
require "timecop"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisStore.new
|
||||||
end
|
end
|
||||||
|
|
@ -36,4 +36,5 @@ describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.read(key)
|
assert_nil Rack::Attack.cache.store.read(key)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "connection_pool"
|
if defined?(::Dalli) && defined?(::ConnectionPool)
|
||||||
require "dalli"
|
require_relative "../../support/cache_store_helper"
|
||||||
require "timecop"
|
require "connection_pool"
|
||||||
|
require "dalli"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "ConnectionPool with Dalli::Client as a cache backend" do
|
describe "ConnectionPool with Dalli::Client as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ConnectionPool.new { Dalli::Client.new }
|
Rack::Attack.cache.store = ConnectionPool.new { Dalli::Client.new }
|
||||||
end
|
end
|
||||||
|
|
@ -37,4 +38,5 @@ describe "ConnectionPool with Dalli::Client as a cache backend" do
|
||||||
|
|
||||||
assert_nil(Rack::Attack.cache.store.with { |client| client.fetch(key) })
|
assert_nil(Rack::Attack.cache.store.with { |client| client.fetch(key) })
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "dalli"
|
if defined?(::Dalli)
|
||||||
require "timecop"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "dalli"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "Dalli::Client as a cache backend" do
|
describe "Dalli::Client as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = Dalli::Client.new
|
Rack::Attack.cache.store = Dalli::Client.new
|
||||||
end
|
end
|
||||||
|
|
@ -36,4 +37,5 @@ describe "Dalli::Client as a cache backend" do
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
|
||||||
require "redis-store"
|
if defined?(::Redis::Store)
|
||||||
require "timecop"
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ::Redis::Store.new
|
Rack::Attack.cache.store = ::Redis::Store.new
|
||||||
end
|
end
|
||||||
|
|
@ -36,4 +36,5 @@ describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.read(key)
|
assert_nil Rack::Attack.cache.store.read(key)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
require 'active_support/cache'
|
require 'active_support/cache'
|
||||||
require 'redis-activesupport'
|
|
||||||
require 'dalli'
|
|
||||||
require_relative '../spec_helper'
|
require_relative '../spec_helper'
|
||||||
|
|
||||||
OfflineExamples = Minitest::SharedExamples.new do
|
OfflineExamples = Minitest::SharedExamples.new do
|
||||||
|
|
@ -17,7 +15,8 @@ OfflineExamples = Minitest::SharedExamples.new do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when Redis is offline' do
|
if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
|
describe 'when Redis is offline' do
|
||||||
include OfflineExamples
|
include OfflineExamples
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
|
@ -25,9 +24,11 @@ describe 'when Redis is offline' do
|
||||||
# Use presumably unused port for Redis client
|
# Use presumably unused port for Redis client
|
||||||
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when Memcached is offline' do
|
if defined?(::Dalli)
|
||||||
|
describe 'when Memcached is offline' do
|
||||||
include OfflineExamples
|
include OfflineExamples
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
|
@ -40,4 +41,5 @@ describe 'when Memcached is offline' do
|
||||||
after do
|
after do
|
||||||
Dalli.logger.level = Logger::INFO
|
Dalli.logger.level = Logger::INFO
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,19 @@ if RUBY_ENGINE == "ruby"
|
||||||
require "byebug"
|
require "byebug"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def safe_require(name)
|
||||||
|
begin
|
||||||
|
require name
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
safe_require "connection_pool"
|
||||||
|
safe_require "dalli"
|
||||||
|
safe_require "redis"
|
||||||
|
safe_require "redis-activesupport"
|
||||||
|
safe_require "redis-store"
|
||||||
|
|
||||||
class MiniTest::Spec
|
class MiniTest::Spec
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue