Commit 2ad01c5a authored by Rémy Coutable's avatar Rémy Coutable

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: default avatarRémy Coutable <remy@rymai.me>
parent 9f863dbe
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 Feature.register_feature_groups
unless Rails.env.test?
require 'flipper/middleware/memoizer'
Rails.application.config.middleware.use Flipper::Middleware::Memoizer
end
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'
class Feature class Feature
# Classes to override flipper table names # Classes to override flipper table names
class FlipperFeature < Flipper::Adapters::ActiveRecord::Feature class FlipperFeature < Flipper::Adapters::ActiveRecord::Feature
...@@ -60,7 +63,8 @@ class Feature ...@@ -60,7 +63,8 @@ class Feature
end end
def flipper def flipper
@flipper ||= Flipper.instance Thread.current[:flipper] ||=
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
end end
# This method is called from config/initializers/flipper.rb and can be used # This method is called from config/initializers/flipper.rb and can be used
...@@ -68,5 +72,16 @@ class Feature ...@@ -68,5 +72,16 @@ class Feature
# See https://docs.gitlab.com/ee/development/feature_flags.html#feature-groups # See https://docs.gitlab.com/ee/development/feature_flags.html#feature-groups
def register_feature_groups def register_feature_groups
end 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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment