Ensure Flipper memoizer is used in Sidekiq's context

Also, don't use the provided Middleware, which isn't thread-safe, and
instantiate a new Flipper instance per thread instead.

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2018-04-19 18:53:07 +02:00
parent 9f863dbe1a
commit 2ad01c5ab0
No known key found for this signature in database
GPG Key ID: 98DFFD1C0C62B70B
2 changed files with 16 additions and 22 deletions

View File

@ -1,22 +1 @@
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'
Flipper.configure do |config|
config.default do
adapter = Flipper::Adapters::ActiveRecord.new(
feature_class: Feature::FlipperFeature, gate_class: Feature::FlipperGate)
cached_adapter = Flipper::Adapters::ActiveSupportCacheStore.new(
adapter,
Rails.cache,
expires_in: 1.hour)
Flipper.new(cached_adapter)
end
end
Feature.register_feature_groups
unless Rails.env.test?
require 'flipper/middleware/memoizer'
Rails.application.config.middleware.use Flipper::Middleware::Memoizer
end

View File

@ -1,3 +1,6 @@
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'
class Feature
# Classes to override flipper table names
class FlipperFeature < Flipper::Adapters::ActiveRecord::Feature
@ -60,7 +63,8 @@ class Feature
end
def flipper
@flipper ||= Flipper.instance
Thread.current[:flipper] ||=
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
end
# This method is called from config/initializers/flipper.rb and can be used
@ -68,5 +72,16 @@ class Feature
# See https://docs.gitlab.com/ee/development/feature_flags.html#feature-groups
def register_feature_groups
end
def flipper_adapter
active_record_adapter = Flipper::Adapters::ActiveRecord.new(
feature_class: FlipperFeature,
gate_class: FlipperGate)
Flipper::Adapters::ActiveSupportCacheStore.new(
active_record_adapter,
Rails.cache,
expires_in: 1.hour)
end
end
end