Commit 2de002b3 authored by Stan Hu's avatar Stan Hu

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
parents 81e899ea c9cf677e
...@@ -90,7 +90,7 @@ module Ci ...@@ -90,7 +90,7 @@ module Ci
def builds_for_group_runner def builds_for_group_runner
# Workaround for weird Rails bug, that makes `runner.groups.to_sql` to return `runner_id = NULL` # 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 hierarchy_groups = Gitlab::GroupHierarchy.new(groups).base_and_descendants
projects = Project.where(namespace_id: hierarchy_groups) projects = Project.where(namespace_id: hierarchy_groups)
......
...@@ -63,7 +63,14 @@ class Feature ...@@ -63,7 +63,14 @@ class Feature
end end
def flipper def flipper
Thread.current[:flipper] ||= 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 } Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
end end
......
...@@ -64,4 +64,28 @@ describe Feature do ...@@ -64,4 +64,28 @@ describe Feature do
expect(described_class.all).to eq(features.to_a) expect(described_class.all).to eq(features.to_a)
end end
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 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