rack-attack/lib/rack/attack/store_proxy.rb
Jonathan del Strother 5cdc15b35a Add a proxy to deal with ActiveSupport::Cache::MemCacheStore
If connection pooling is used with AS::Cache::MemCacheStore,
unwrap_active_support_stores wouldn't return the underlying dalli instance(s),
and so Rack::Attack.store would be the bare unproxied MemCacheStore instance.

Calling write then increment would silently fail because :raw wasn't used.

With this commit, we no longer try to unwrap AS::Cache::MemCacheStore instances.
2018-09-03 12:00:02 +01:00

25 lines
764 B
Ruby

# frozen_string_literal: true
module Rack
class Attack
module StoreProxy
PROXIES = [DalliProxy, MemCacheStoreProxy, MemCacheProxy, RedisStoreProxy, RedisProxy, RedisCacheStoreProxy].freeze
def self.build(store)
client = unwrap_active_support_stores(store)
klass = PROXIES.find { |proxy| proxy.handle?(client) }
klass ? klass.new(client) : client
end
def self.unwrap_active_support_stores(store)
# ActiveSupport::Cache::RedisStore doesn't expose any way to set an expiry,
# so use the raw Redis::Store instead.
if store.class.name == 'ActiveSupport::Cache::RedisStore'
store.instance_variable_get(:@data)
else
store
end
end
end
end
end