Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poll cold start fix #795

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/flipper/adapters/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def initialize(poller, adapter)
@adapter = adapter
@poller = poller
@last_synced_at = 0

# If the adapter is empty, we need to sync before starting the poller.
# Yes, this will block the main thread, but that's better than thinking
# nothing is enabled.
if adapter.features.empty?
@poller.sync
end

@poller.start
end

Expand Down
41 changes: 41 additions & 0 deletions spec/flipper/adapters/poll_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'flipper/adapters/poll'

RSpec.describe Flipper::Adapters::Poll do
let(:remote_adapter) {
adapter = Flipper::Adapters::Memory.new(threadsafe: true)
flipper = Flipper.new(adapter)
flipper.enable(:search)
flipper.enable(:analytics)
adapter
}
let(:local_adapter) { Flipper::Adapters::Memory.new(threadsafe: true) }
let(:poller) {
Flipper::Poller.get("for_spec", {
start_automatically: false,
remote_adapter: remote_adapter,
})
}

it "syncs in main thread if local adapter is empty" do
instance = described_class.new(poller, local_adapter)
instance.features # call something to force sync
expect(local_adapter.features).to eq(remote_adapter.features)
end

it "does not sync in main thread if local adapter is not empty" do
# make local not empty by importing remote
flipper = Flipper.new(local_adapter)
flipper.import(remote_adapter)

# make a fake poller to verify calls
poller = double("Poller", last_synced_at: Concurrent::AtomicFixnum.new(0))
expect(poller).to receive(:start).twice
expect(poller).not_to receive(:sync)

# create new instance and call something to force sync
instance = described_class.new(poller, local_adapter)
instance.features # call something to force sync

expect(local_adapter.features).to eq(remote_adapter.features)
end
end