Use RequestStore to memoize Flipper features so that memoized values are cleared between requests
Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
parent
c5f89e5bd7
commit
e1ffd6a271
2 changed files with 33 additions and 2 deletions
|
@ -63,8 +63,15 @@ class Feature
|
||||||
end
|
end
|
||||||
|
|
||||||
def flipper
|
def flipper
|
||||||
Thread.current[:flipper] ||=
|
if RequestStore.active?
|
||||||
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
|
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
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue