summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-06-01 18:56:21 +0000
committerStan Hu <stanhu@gmail.com>2018-06-01 18:56:21 +0000
commit2de002b3db1bc199755f8be212fa8804fcb80905 (patch)
tree0c4460d62cd6a0b653b2d77e29447093ccb4933e
parent81e899ea7141b34a7860f308327249784fb5d6cc (diff)
parentc9cf677e20737aa9f8a824ec2e51b4ed12bc8ea4 (diff)
downloadgitlab-ce-2de002b3db1bc199755f8be212fa8804fcb80905.tar.gz
Merge branch 'fix-feature-memoization' into 'master'
Use RequestStore to memoize Flipper features so that memoized values are cleared between requests See merge request gitlab-org/gitlab-ce!19281
-rw-r--r--app/services/ci/register_job_service.rb2
-rw-r--r--lib/feature.rb11
-rw-r--r--spec/lib/feature_spec.rb24
3 files changed, 34 insertions, 3 deletions
diff --git a/app/services/ci/register_job_service.rb b/app/services/ci/register_job_service.rb
index 317d1defbba..925775aea0b 100644
--- a/app/services/ci/register_job_service.rb
+++ b/app/services/ci/register_job_service.rb
@@ -90,7 +90,7 @@ module Ci
def builds_for_group_runner
# Workaround for weird Rails bug, that makes `runner.groups.to_sql` to return `runner_id = NULL`
- groups = Group.joins(:runner_namespaces).merge(runner.runner_namespaces)
+ groups = ::Group.joins(:runner_namespaces).merge(runner.runner_namespaces)
hierarchy_groups = Gitlab::GroupHierarchy.new(groups).base_and_descendants
projects = Project.where(namespace_id: hierarchy_groups)
diff --git a/lib/feature.rb b/lib/feature.rb
index 6474de6e56d..314ae224d90 100644
--- a/lib/feature.rb
+++ b/lib/feature.rb
@@ -63,8 +63,15 @@ class Feature
end
def flipper
- Thread.current[:flipper] ||=
- Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
+ if RequestStore.active?
+ RequestStore[:flipper] ||= build_flipper_instance
+ else
+ @flipper ||= build_flipper_instance
+ end
+ end
+
+ def build_flipper_instance
+ Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
end
# This method is called from config/initializers/flipper.rb and can be used
diff --git a/spec/lib/feature_spec.rb b/spec/lib/feature_spec.rb
index 10020511bf8..6eb10497428 100644
--- a/spec/lib/feature_spec.rb
+++ b/spec/lib/feature_spec.rb
@@ -64,4 +64,28 @@ describe Feature do
expect(described_class.all).to eq(features.to_a)
end
end
+
+ describe '.flipper' do
+ shared_examples 'a memoized Flipper instance' do
+ it 'memoizes the Flipper instance' do
+ expect(Flipper).to receive(:new).once.and_call_original
+
+ 2.times do
+ described_class.flipper
+ end
+ end
+ end
+
+ context 'when request store is inactive' do
+ before do
+ described_class.instance_variable_set(:@flipper, nil)
+ end
+
+ it_behaves_like 'a memoized Flipper instance'
+ end
+
+ context 'when request store is inactive', :request_store do
+ it_behaves_like 'a memoized Flipper instance'
+ end
+ end
end