diff options
author | Rémy Coutable <remy@rymai.me> | 2018-04-19 18:53:07 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2018-05-18 12:25:54 +0200 |
commit | 2ad01c5ab079959205c236df7bf8cbb5c1fab573 (patch) | |
tree | 3151e48d210b853f7c65dafc2fa0f4069ea442fc | |
parent | 9f863dbe1a5ed324b7fb202eea11397d1fcdd61f (diff) | |
download | gitlab-ce-2ad01c5ab079959205c236df7bf8cbb5c1fab573.tar.gz |
Ensure Flipper memoizer is used in Sidekiq's context45528-repeated-calls-to-redis-for-flipper-feature-flag
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>
-rw-r--r-- | config/initializers/flipper.rb | 21 | ||||
-rw-r--r-- | lib/feature.rb | 17 |
2 files changed, 16 insertions, 22 deletions
diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb index c60ad535fd5..80cab7273e5 100644 --- a/config/initializers/flipper.rb +++ b/config/initializers/flipper.rb @@ -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 diff --git a/lib/feature.rb b/lib/feature.rb index 8e9ba5c530a..6474de6e56d 100644 --- a/lib/feature.rb +++ b/lib/feature.rb @@ -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 |