flipper 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcd315c90967e5b1cf80a834bcadda7d0439fced33abf49a76300eca785e8f27
4
- data.tar.gz: 22f0d9856d41984f0ac35be264bcfb0a07bdef7f5e59211c82c61d8535333ea0
3
+ metadata.gz: bdcdff82d4879fb65b7a6b2ef6c49a1ed5da94589aa58c4790b5d9cc50fc1ce8
4
+ data.tar.gz: 5f23aeab63bb6ade8c6ae735374c772a69dd192e098b12138483a31ebb2f31b8
5
5
  SHA512:
6
- metadata.gz: 8e826172032e682aa69f6ae04394636325f3586b1927c3fb46185da2cefd5921b684aab71dd50a9b8586425b8a928fff59c03e55db681f993076f3c319aad142
7
- data.tar.gz: fa4c786c16e36e48d246e50c673223fe88ea7d024466f807bc4d6b1747abc4a15caaf4267a1423b6713717ca675e7e905537f070a2c1f6ddc2af0c094c94a532
6
+ metadata.gz: d06d3b34274522996cd2a12dbd5a7ae839c2463fc7dd57aad0567e63125d52e3abed80bd0134fd035b310e7ef7e4afd588baace419aae42657338edd09664f89
7
+ data.tar.gz: eefd40294203d03d49a5c9eef6bba892e5b4706c79a2ad9462176c82873cf1189f96002a3fe8268819bf4da963c8d868f8889150def23c6f20abc1efcd3bd940
data/README.md CHANGED
@@ -137,7 +137,7 @@ When Cloud is your default (`FLIPPER_CLOUD_TOKEN` is set, with Rails or `Flipper
137
137
  - Memory is filled from your adapter when the process boots, so flags are there even if Cloud isn't reachable.
138
138
  - Changes you make in your app go to Cloud, your adapter, and that process's Memory. Other processes get them from Cloud like any other change, as described below.
139
139
  - **Polling** (the default): every process polls Cloud every 10 seconds, and each change is written to your adapter and Memory on the next feature check.
140
- - **Webhooks** (`FLIPPER_CLOUD_SYNC_SECRET` set): the process that gets the webhook updates your adapter and its Memory right away. Other processes reload Memory from your adapter every 10 seconds, and every process polls Cloud every 5 minutes in case a webhook is missed.
140
+ - **Webhooks** (`FLIPPER_CLOUD_SYNC_SECRET` set): the process that gets the webhook updates your adapter and its Memory right away. Other processes reload Memory from your adapter every 10 seconds if it stores the sync version (every built-in adapter does), and every process polls Cloud every 5 minutes in case a webhook is missed. With an adapter that can't store it, other processes get the change on that poll.
141
141
  - If your adapter is down, feature checks keep working from Memory. A sync that can't be written to your adapter is written again on the next sync.
142
142
  - Adapters added with `config.use` (like `Strict` and `ActorLimit`) still wrap reads and writes.
143
143
  - [Named instances](#named-instances) that call `cloud` read the same way, each from its own Memory.
@@ -48,12 +48,14 @@ module Flipper
48
48
  end
49
49
 
50
50
  # Public: Ensure that adapter is in sync with source adapter provided.
51
+ # Always applies the source's flags, even if it has an older sync version
52
+ # than this adapter, such as a backup being restored.
51
53
  #
52
54
  # source - The source dsl, adapter or export to import.
53
55
  #
54
56
  # Returns true if successful.
55
57
  def import(source)
56
- Adapters::Sync::Synchronizer.new(self, self.class.from(source), raise: true).call
58
+ Adapters::Sync::Synchronizer.new(self, self.class.from(source), raise: true, keep_newer: false).call
57
59
  true
58
60
  end
59
61
 
@@ -141,27 +141,49 @@ module Flipper
141
141
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
142
142
  end
143
143
 
144
+ # Public: Returns a copy of the flags and their sync version, read
145
+ # together, so a sync from it can't pair one import's flags with
146
+ # another's version.
147
+ def snapshot
148
+ copy = Memory.new(nil, threadsafe: false)
149
+ # Shallow is enough: writes replace a feature's gates, never edit them.
150
+ synchronize { copy.restore(@source.dup, @sync_version) }
151
+ copy
152
+ end
153
+
144
154
  # Public: a more efficient implementation of import for this adapter.
145
- # Keeps the current flags if the source's are an older sync version.
146
- # Otherwise yields the new flags and version inside the lock, before
147
- # replacing the current ones.
148
- def import(source)
155
+ # Yields the new flags and version inside the lock, before replacing the
156
+ # current ones.
157
+ #
158
+ # keep_newer - Keep the current flags if the source's are an older sync
159
+ # version (default: false). Syncs pass true; an import
160
+ # always applies, so restoring older flags works, but the
161
+ # recorded version never goes back.
162
+ def import(source, keep_newer: false)
149
163
  adapter = self.class.from(source)
150
164
  get_all = Typecast.features_hash(adapter.get_all)
151
165
  version = adapter.sync_version
152
166
 
153
167
  synchronize do
154
- unless version&.older_than?(@sync_version)
168
+ older = version&.older_than?(@sync_version)
169
+ unless older && keep_newer
155
170
  yield get_all, version if block_given?
156
171
  @features = nil
157
172
  @source.replace(get_all)
158
- @sync_version = version if version
173
+ @sync_version = version if version && !older
159
174
  end
160
175
  end
161
176
 
162
177
  true
163
178
  end
164
179
 
180
+ protected
181
+
182
+ def restore(source, version)
183
+ @source = source
184
+ @sync_version = version
185
+ end
186
+
165
187
  private
166
188
 
167
189
  def synchronize(&block)
@@ -20,7 +20,7 @@ module Flipper
20
20
  @adapter = adapter
21
21
  @poller = poller
22
22
  @synchronizer = options.fetch(:synchronizer) {
23
- -> { Flipper::Adapters::Sync::Synchronizer.new(@adapter, @poller.adapter).call }
23
+ -> { Flipper::Adapters::Sync::Synchronizer.new(@adapter, poller_snapshot).call }
24
24
  }
25
25
  @last_synced_at = 0
26
26
  # Flipper::Poller has a lock-free synced_at. Other pollers may only
@@ -83,6 +83,13 @@ module Flipper
83
83
 
84
84
  private
85
85
 
86
+ # The poller imports while threads sync from it, so read its flags and
87
+ # version together when it's a Memory adapter, as Flipper::Poller's is.
88
+ def poller_snapshot
89
+ adapter = @poller.adapter
90
+ adapter.is_a?(Memory) ? adapter.snapshot : adapter
91
+ end
92
+
86
93
  def synced_adapter
87
94
  @poller.start
88
95
  poller_last_synced_at = @poller_has_synced_at ? @poller.synced_at : @poller.last_synced_at.value
@@ -19,12 +19,17 @@ module Flipper
19
19
  # :instrumenter - The instrumenter used to instrument.
20
20
  # :raise - Should errors be raised (default: true).
21
21
  # :cache_bust - Should cache busting be used for remote get_all (default: false).
22
+ # :keep_newer - Skip remote flags with an older sync version
23
+ # than local's (default: true). Import passes
24
+ # false so restoring older flags still works,
25
+ # but local's version still never goes back.
22
26
  def initialize(local, remote, options = {})
23
27
  @local = local
24
28
  @remote = remote
25
29
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
26
30
  @raise = options.fetch(:raise, true)
27
31
  @cache_bust = options.fetch(:cache_bust, false)
32
+ @keep_newer = options.fetch(:keep_newer, true)
28
33
  end
29
34
 
30
35
  # Public: Forces a sync.
@@ -44,7 +49,8 @@ module Flipper
44
49
  # Never replace flags with older ones from a stale remote response.
45
50
  remote_version = @remote.sync_version
46
51
  local_version = @local.sync_version if remote_version
47
- if remote_version&.older_than?(local_version)
52
+ older = remote_version&.older_than?(local_version)
53
+ if older && @keep_newer
48
54
  @instrumenter.instrument("synchronizer_stale.flipper", {
49
55
  local_version: local_version,
50
56
  remote_version: remote_version,
@@ -55,7 +61,7 @@ module Flipper
55
61
  # Record before writing flags so a concurrent sync of older flags
56
62
  # into a shared adapter sees this version as soon as possible.
57
63
  # Flipper::Cloud::SharedMemory#reload copies again for this reason.
58
- if remote_version && remote_version != local_version
64
+ if remote_version && remote_version != local_version && !older
59
65
  @local.sync_version = remote_version
60
66
  end
61
67
 
data/lib/flipper/cli.rb CHANGED
@@ -356,8 +356,12 @@ module Flipper
356
356
  end
357
357
 
358
358
  def run(argv)
359
- # Parse argv and call action with arguments
360
- @action.call(*permute(argv))
359
+ args = begin
360
+ permute(argv)
361
+ rescue ZeroDivisionError
362
+ raise OptionParser::InvalidArgument, "percentage denominator must not be zero"
363
+ end
364
+ @action.call(*args)
361
365
  end
362
366
 
363
367
  def action(&block)
@@ -131,13 +131,20 @@ module Flipper
131
131
  # Public: Force a sync.
132
132
  def sync(cache_bust: false)
133
133
  remote = http_adapter
134
- return synchronize(remote, cache_bust: cache_bust) unless @memory
135
-
136
- # Fetch once for both the local adapter and memory.
134
+ # Fetch once for the poller, the local adapter and memory.
137
135
  snapshot = Flipper::Adapters::Memory.new(remote.get_all(cache_bust: cache_bust))
138
136
  snapshot.sync_version = remote.sync_version
139
- # Tracked like a poll's sync, so one that fails partway is synced again.
140
- @memory.sync(snapshot.sync_version) { sync_memory(snapshot) }
137
+ # Update the poller too, so threads that haven't read since its last
138
+ # poll don't sync its older flags over these. A local adapter without
139
+ # a sync version couldn't refuse them.
140
+ poller.adapter.import(snapshot)
141
+
142
+ if @memory
143
+ # Tracked like a poll's sync, so one that fails partway is synced again.
144
+ @memory.sync(snapshot.sync_version) { sync_memory(snapshot) }
145
+ else
146
+ synchronize(snapshot)
147
+ end
141
148
  nil
142
149
  end
143
150
 
@@ -188,10 +195,8 @@ module Flipper
188
195
  Flipper::Adapters::DualWrite.new(poll_adapter, http_adapter)
189
196
  end
190
197
 
191
- def synchronize(remote, options = {})
192
- Flipper::Adapters::Sync::Synchronizer.new(local_adapter, remote, {
193
- instrumenter: instrumenter,
194
- }.merge(options)).call
198
+ def synchronize(remote)
199
+ Flipper::Adapters::Sync::Synchronizer.new(local_adapter, remote, instrumenter: instrumenter).call
195
200
  end
196
201
 
197
202
  # Syncs the local adapter, where the version check uses the version other
@@ -258,8 +263,8 @@ module Flipper
258
263
  # Once the version is synced, one thread per poll checks whether another
259
264
  # process stored a different one, so the rest don't query the local
260
265
  # adapter. A check that fails gives the poll back to the next thread.
261
- def sync_poll(snapshot, polled_at)
262
- version = snapshot.sync_version
266
+ def sync_poll(polled, polled_at)
267
+ version = polled.sync_version
263
268
  claimed = false
264
269
  if @memory.synced?(version)
265
270
  claimed = @memory.claim_store_check(polled_at)
@@ -267,7 +272,12 @@ module Flipper
267
272
  return if @store.sync_version == version
268
273
  end
269
274
 
270
- @memory.sync(version) { sync_memory(snapshot) }
275
+ # Copied only when syncing, since most reads find the poll synced. A
276
+ # forced sync may have replaced the poll since the check above.
277
+ snapshot = polled.snapshot
278
+ return if snapshot.sync_version != version && @memory.synced?(snapshot.sync_version)
279
+
280
+ @memory.sync(snapshot.sync_version) { sync_memory(snapshot) }
271
281
  rescue StandardError
272
282
  # Reads keep working from memory if the local adapter is down.
273
283
  @memory.release_store_check(polled_at) if claimed
@@ -22,9 +22,10 @@ module Flipper
22
22
  # adapter, which another process may be syncing. A sync writes its
23
23
  # version before its flags, so the copy can have only some of them under
24
24
  # the new version. reload then copies again until a copy changes nothing
25
- # RECOPY_INTERVAL after the last one that did.
25
+ # RECOPY_INTERVAL after the last one that did. Keeps the current flags if
26
+ # the source's are an older sync version.
26
27
  def import(source, recopy: false)
27
- super(source) do |features, version|
28
+ super(source, keep_newer: true) do |features, version|
28
29
  @changed_at = now if recopy && (version != @sync_version || features != @source)
29
30
  @recopy = recopy && now - @changed_at < RECOPY_INTERVAL
30
31
  end
data/lib/flipper/cloud.rb CHANGED
@@ -48,9 +48,13 @@ module Flipper
48
48
  def self.set_default(&options)
49
49
  if ENV["FLIPPER_CLOUD_TOKEN"]
50
50
  Flipper.configure do |config|
51
- memory = Flipper::Cloud::SharedMemory.new if memory_reads?
51
+ # Built on first use so FLIPPER_CLOUD_MEMORY_READS can be set after
52
+ # this runs (for example, in a Rails initializer).
53
+ memory = nil
54
+ memory_lock = Mutex.new
52
55
  config.default do
53
- adapter_options = if memory
56
+ adapter_options = if memory_reads?
57
+ memory_lock.synchronize { memory ||= Flipper::Cloud::SharedMemory.new }
54
58
  { memory: memory, adapter_builder: config }
55
59
  else
56
60
  { local_adapter: config.adapter }
@@ -3,6 +3,9 @@ require "time"
3
3
  module Flipper
4
4
  module Expressions
5
5
  class Time
6
+ # DateTime.new(1970, 1, 1).ajd: the Unix epoch as an astronomical Julian day.
7
+ UNIX_EPOCH_AJD = Rational(4_881_175, 2)
8
+
6
9
  # Returns the value as a UTC time, or nil when it can't be read as one, so
7
10
  # a missing or malformed property fails comparisons instead of the check.
8
11
  def self.call(value)
@@ -13,8 +16,8 @@ module Flipper
13
16
  ::Time.parse(value).utc
14
17
  when ::Time
15
18
  value.getutc
16
- when ::Date
17
- ::Time.parse(value.iso8601).utc
19
+ when ::Date # and DateTime; a plain Date is midnight UTC
20
+ ::Time.at((value.ajd - UNIX_EPOCH_AJD) * 86_400).utc
18
21
  end
19
22
  rescue ArgumentError, RangeError
20
23
  nil
@@ -88,7 +88,7 @@ module Flipper
88
88
  def sync
89
89
  @instrumenter.instrument("poller.#{InstrumentationNamespace}", operation: :poll) do
90
90
  begin
91
- @adapter.import @remote_adapter
91
+ @adapter.import @remote_adapter, keep_newer: true
92
92
  now = Concurrent.monotonic_time
93
93
  @last_synced_at.update { now }
94
94
  @synced_at = now
@@ -386,4 +386,18 @@ RSpec.shared_examples 'an adapter that stores its sync version' do
386
386
 
387
387
  expect(subject.sync_version).to eq(version)
388
388
  end
389
+
390
+ it 'imports a source with an older sync version from the same scope' do
391
+ Flipper.new(subject).enable(:search)
392
+ subject.sync_version = version
393
+
394
+ source = Flipper::Adapters::Memory.new
395
+ Flipper.new(source).enable(:stats)
396
+ older = Flipper::SyncVersion.new("scope", 8)
397
+ source.sync_version = older
398
+
399
+ expect(Flipper.new(subject).import(source)).to be(true)
400
+ expect(subject.features).to eq(Set["stats"])
401
+ expect(subject.sync_version).to eq(version)
402
+ end
389
403
  end
@@ -1,5 +1,5 @@
1
1
  module Flipper
2
- VERSION = '1.5.0'.freeze
2
+ VERSION = '1.5.1'.freeze
3
3
 
4
4
  REQUIRED_RUBY_VERSION = '2.6'.freeze
5
5
  NEXT_REQUIRED_RUBY_VERSION = '3.0'.freeze
@@ -7,9 +7,9 @@ gate_index = gate_index_name
7
7
  %>class <%= migration_class %> < ActiveRecord::Migration<%= migration_version %>
8
8
  def up
9
9
  # Ensure this incremental update migration is idempotent
10
- return unless connection.column_exists? <%= gate_table.inspect %>, :value, :string
10
+ return unless column_exists? <%= gate_table.inspect %>, :value, :string
11
11
 
12
- gate_index = connection.indexes(<%= gate_table.inspect %>).find do |index|
12
+ gate_index = indexes(<%= gate_table.inspect %>).find do |index|
13
13
  index.columns == %w[feature_key key value]
14
14
  end
15
15
  remove_index <%= gate_table.inspect %>, name: gate_index.name if gate_index
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-28 00:00:00.000000000 Z
11
+ date: 2026-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -194,7 +194,7 @@ metadata:
194
194
  homepage_uri: https://www.flippercloud.io
195
195
  source_code_uri: https://github.com/flippercloud/flipper
196
196
  bug_tracker_uri: https://github.com/flippercloud/flipper/issues
197
- changelog_uri: https://github.com/flippercloud/flipper/releases/tag/v1.5.0
197
+ changelog_uri: https://github.com/flippercloud/flipper/releases/tag/v1.5.1
198
198
  funding_uri: https://github.com/sponsors/flippercloud
199
199
  post_install_message:
200
200
  rdoc_options: []