flipper 1.4.2 → 1.5.0.rc2
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 +4 -4
- data/README.md +70 -10
- data/lib/flipper/adapter.rb +24 -0
- data/lib/flipper/adapter_builder.rb +12 -1
- data/lib/flipper/adapters/actor_limit.rb +4 -3
- data/lib/flipper/adapters/cache_base.rb +17 -0
- data/lib/flipper/adapters/dual_write.rb +12 -0
- data/lib/flipper/adapters/failover.rb +15 -0
- data/lib/flipper/adapters/failsafe.rb +16 -0
- data/lib/flipper/adapters/http/client.rb +7 -3
- data/lib/flipper/adapters/http/redacted_debug_output.rb +70 -0
- data/lib/flipper/adapters/http.rb +42 -8
- data/lib/flipper/adapters/instrumented.rb +28 -0
- data/lib/flipper/adapters/memoizable.rb +21 -5
- data/lib/flipper/adapters/memory.rb +63 -31
- data/lib/flipper/adapters/poll.rb +51 -6
- data/lib/flipper/adapters/pstore.rb +15 -0
- data/lib/flipper/adapters/read_only.rb +1 -1
- data/lib/flipper/adapters/strict.rb +17 -1
- data/lib/flipper/adapters/sync/feature_synchronizer.rb +3 -1
- data/lib/flipper/adapters/sync/synchronizer.rb +20 -1
- data/lib/flipper/adapters/sync.rb +8 -0
- data/lib/flipper/adapters/wrapper.rb +7 -0
- data/lib/flipper/cli.rb +87 -22
- data/lib/flipper/cloud/configuration.rb +149 -19
- data/lib/flipper/cloud/dsl.rb +6 -0
- data/lib/flipper/cloud/memory_adapter.rb +43 -0
- data/lib/flipper/cloud/message_verifier.rb +1 -0
- data/lib/flipper/cloud/middleware.rb +8 -3
- data/lib/flipper/cloud/migrate.rb +3 -1
- data/lib/flipper/cloud/routes.rb +24 -2
- data/lib/flipper/cloud/shared_memory.rb +111 -0
- data/lib/flipper/cloud/telemetry.rb +56 -28
- data/lib/flipper/cloud/url_validator.rb +26 -0
- data/lib/flipper/cloud.rb +22 -4
- data/lib/flipper/configuration.rb +86 -3
- data/lib/flipper/dsl.rb +47 -3
- data/lib/flipper/engine.rb +64 -8
- data/lib/flipper/errors.rb +18 -0
- data/lib/flipper/export.rb +4 -0
- data/lib/flipper/exporters/json/export.rb +6 -0
- data/lib/flipper/exporters/json/v1.rb +5 -9
- data/lib/flipper/expression/constant.rb +6 -0
- data/lib/flipper/expression.rb +105 -1
- data/lib/flipper/expressions/comparable.rb +5 -0
- data/lib/flipper/expressions/feature_enabled.rb +11 -7
- data/lib/flipper/expressions/number.rb +2 -0
- data/lib/flipper/expressions/percentage.rb +3 -1
- data/lib/flipper/expressions/percentage_of_actors.rb +9 -0
- data/lib/flipper/expressions/random.rb +2 -0
- data/lib/flipper/expressions/sem_ver.rb +24 -0
- data/lib/flipper/expressions/time.rb +9 -1
- data/lib/flipper/feature.rb +153 -43
- data/lib/flipper/feature_check_context.rb +19 -1
- data/lib/flipper/gates/actor.rb +4 -1
- data/lib/flipper/gates/expression.rb +17 -2
- data/lib/flipper/gates/group.rb +1 -1
- data/lib/flipper/gates/percentage_of_actors.rb +19 -2
- data/lib/flipper/gates/percentage_of_time.rb +4 -1
- data/lib/flipper/groups.rb +66 -0
- data/lib/flipper/instrumentation/log_subscriber.rb +6 -1
- data/lib/flipper/instrumentation/subscriber.rb +21 -2
- data/lib/flipper/middleware/memoizer.rb +5 -2
- data/lib/flipper/named_configuration.rb +194 -0
- data/lib/flipper/named_proxy.rb +46 -0
- data/lib/flipper/poller.rb +39 -17
- data/lib/flipper/sem_ver.rb +84 -0
- data/lib/flipper/spec/shared_adapter_specs.rb +40 -0
- data/lib/flipper/sync_version.rb +61 -0
- data/lib/flipper/test_help.rb +50 -0
- data/lib/flipper/typecast.rb +15 -0
- data/lib/flipper/types/group.rb +2 -2
- data/lib/flipper/version.rb +1 -1
- data/lib/flipper.rb +183 -65
- data/lib/generators/flipper/setup_generator.rb +18 -0
- data/lib/generators/flipper/table_prefix.rb +44 -0
- data/lib/generators/flipper/templates/initializer.rb +7 -0
- data/lib/generators/flipper/templates/update/migrations/01_create_flipper_tables.rb.erb +13 -7
- data/lib/generators/flipper/templates/update/migrations/02_change_flipper_gates_value_to_text.rb.erb +12 -7
- data/lib/generators/flipper/update_generator.rb +3 -0
- metadata +42 -171
- data/.codeclimate.yml +0 -3
- data/.github/FUNDING.yml +0 -1
- data/.github/dependabot.yml +0 -6
- data/.github/workflows/ci.yml +0 -110
- data/.github/workflows/examples.yml +0 -105
- data/.github/workflows/release.yml +0 -54
- data/.rspec +0 -1
- data/.superset/config.json +0 -4
- data/CLAUDE.md +0 -93
- data/CODE_OF_CONDUCT.md +0 -46
- data/Dockerfile +0 -27
- data/Gemfile +0 -41
- data/Rakefile +0 -54
- data/benchmark/enabled_ips.rb +0 -10
- data/benchmark/enabled_multiple_actors_ips.rb +0 -20
- data/benchmark/enabled_profile.rb +0 -20
- data/benchmark/instrumentation_ips.rb +0 -21
- data/benchmark/typecast_ips.rb +0 -27
- data/docker-compose.yml +0 -37
- data/docs/DockerCompose.md +0 -18
- data/docs/README.md +0 -1
- data/docs/images/banner.jpg +0 -0
- data/docs/images/flipper_cloud.png +0 -0
- data/examples/api/basic.ru +0 -18
- data/examples/api/custom_memoized.ru +0 -36
- data/examples/api/memoized.ru +0 -42
- data/examples/basic.rb +0 -19
- data/examples/cloud/app.ru +0 -12
- data/examples/cloud/backoff_policy.rb +0 -13
- data/examples/cloud/basic.rb +0 -22
- data/examples/cloud/cloud_setup.rb +0 -20
- data/examples/cloud/forked.rb +0 -36
- data/examples/cloud/import.rb +0 -17
- data/examples/cloud/poll_interval/README.md +0 -111
- data/examples/cloud/poll_interval/client.rb +0 -108
- data/examples/cloud/poll_interval/server.rb +0 -98
- data/examples/cloud/threaded.rb +0 -33
- data/examples/configuring_default.rb +0 -19
- data/examples/dsl.rb +0 -57
- data/examples/enabled_for_actor.rb +0 -37
- data/examples/expressions.rb +0 -237
- data/examples/group.rb +0 -38
- data/examples/group_dynamic_lookup.rb +0 -75
- data/examples/group_with_members.rb +0 -83
- data/examples/importing.rb +0 -29
- data/examples/individual_actor.rb +0 -28
- data/examples/instrumentation.rb +0 -42
- data/examples/instrumentation_last_accessed_at.rb +0 -38
- data/examples/memoizing.rb +0 -35
- data/examples/mirroring.rb +0 -59
- data/examples/percentage_of_actors.rb +0 -31
- data/examples/percentage_of_actors_enabled_check.rb +0 -35
- data/examples/percentage_of_actors_group.rb +0 -35
- data/examples/percentage_of_time.rb +0 -27
- data/examples/strict.rb +0 -18
- data/flipper-cloud.gemspec +0 -19
- data/flipper.gemspec +0 -42
- data/package-lock.json +0 -41
- data/package.json +0 -10
- data/spec/fixtures/environment.rb +0 -1
- data/spec/fixtures/feature.json +0 -31
- data/spec/fixtures/flipper_pstore_1679087600.json +0 -46
- data/spec/flipper/actor_spec.rb +0 -56
- data/spec/flipper/adapter_builder_spec.rb +0 -72
- data/spec/flipper/adapter_spec.rb +0 -166
- data/spec/flipper/adapters/actor_limit_spec.rb +0 -75
- data/spec/flipper/adapters/dual_write_spec.rb +0 -82
- data/spec/flipper/adapters/failover_spec.rb +0 -141
- data/spec/flipper/adapters/failsafe_spec.rb +0 -58
- data/spec/flipper/adapters/http/client_spec.rb +0 -61
- data/spec/flipper/adapters/http_spec.rb +0 -596
- data/spec/flipper/adapters/instrumented_spec.rb +0 -167
- data/spec/flipper/adapters/memoizable_spec.rb +0 -377
- data/spec/flipper/adapters/memory_spec.rb +0 -36
- data/spec/flipper/adapters/operation_logger_spec.rb +0 -128
- data/spec/flipper/adapters/poll_spec.rb +0 -41
- data/spec/flipper/adapters/pstore_spec.rb +0 -16
- data/spec/flipper/adapters/read_only_spec.rb +0 -100
- data/spec/flipper/adapters/strict_spec.rb +0 -122
- data/spec/flipper/adapters/sync/feature_synchronizer_spec.rb +0 -247
- data/spec/flipper/adapters/sync/interval_synchronizer_spec.rb +0 -33
- data/spec/flipper/adapters/sync/synchronizer_spec.rb +0 -175
- data/spec/flipper/adapters/sync_spec.rb +0 -213
- data/spec/flipper/cli_spec.rb +0 -217
- data/spec/flipper/cloud/configuration_spec.rb +0 -251
- data/spec/flipper/cloud/dsl_spec.rb +0 -82
- data/spec/flipper/cloud/message_verifier_spec.rb +0 -104
- data/spec/flipper/cloud/middleware_spec.rb +0 -289
- data/spec/flipper/cloud/migrate_spec.rb +0 -160
- data/spec/flipper/cloud/telemetry/backoff_policy_spec.rb +0 -107
- data/spec/flipper/cloud/telemetry/metric_spec.rb +0 -87
- data/spec/flipper/cloud/telemetry/metric_storage_spec.rb +0 -58
- data/spec/flipper/cloud/telemetry/submitter_spec.rb +0 -145
- data/spec/flipper/cloud/telemetry_spec.rb +0 -208
- data/spec/flipper/cloud_spec.rb +0 -186
- data/spec/flipper/configuration_spec.rb +0 -50
- data/spec/flipper/dsl_spec.rb +0 -346
- data/spec/flipper/engine_spec.rb +0 -374
- data/spec/flipper/export_spec.rb +0 -13
- data/spec/flipper/exporter_spec.rb +0 -16
- data/spec/flipper/exporters/json/export_spec.rb +0 -60
- data/spec/flipper/exporters/json/v1_spec.rb +0 -33
- data/spec/flipper/expression/builder_spec.rb +0 -248
- data/spec/flipper/expression_spec.rb +0 -188
- data/spec/flipper/expressions/all_spec.rb +0 -15
- data/spec/flipper/expressions/any_spec.rb +0 -15
- data/spec/flipper/expressions/boolean_spec.rb +0 -15
- data/spec/flipper/expressions/equal_spec.rb +0 -24
- data/spec/flipper/expressions/greater_than_or_equal_to_spec.rb +0 -28
- data/spec/flipper/expressions/greater_than_spec.rb +0 -28
- data/spec/flipper/expressions/less_than_or_equal_to_spec.rb +0 -28
- data/spec/flipper/expressions/less_than_spec.rb +0 -32
- data/spec/flipper/expressions/not_equal_spec.rb +0 -15
- data/spec/flipper/expressions/now_spec.rb +0 -11
- data/spec/flipper/expressions/number_spec.rb +0 -21
- data/spec/flipper/expressions/percentage_of_actors_spec.rb +0 -20
- data/spec/flipper/expressions/percentage_spec.rb +0 -15
- data/spec/flipper/expressions/property_spec.rb +0 -13
- data/spec/flipper/expressions/random_spec.rb +0 -9
- data/spec/flipper/expressions/string_spec.rb +0 -11
- data/spec/flipper/expressions/time_spec.rb +0 -29
- data/spec/flipper/feature_check_context_spec.rb +0 -65
- data/spec/flipper/feature_spec.rb +0 -1227
- data/spec/flipper/gate_spec.rb +0 -39
- data/spec/flipper/gate_values_spec.rb +0 -103
- data/spec/flipper/gates/actor_spec.rb +0 -7
- data/spec/flipper/gates/boolean_spec.rb +0 -74
- data/spec/flipper/gates/expression_spec.rb +0 -190
- data/spec/flipper/gates/group_spec.rb +0 -62
- data/spec/flipper/gates/percentage_of_actors_spec.rb +0 -131
- data/spec/flipper/gates/percentage_of_time_spec.rb +0 -37
- data/spec/flipper/identifier_spec.rb +0 -12
- data/spec/flipper/instrumentation/log_subscriber_spec.rb +0 -122
- data/spec/flipper/instrumentation/statsd_subscriber_spec.rb +0 -95
- data/spec/flipper/instrumenters/memory_spec.rb +0 -43
- data/spec/flipper/instrumenters/noop_spec.rb +0 -27
- data/spec/flipper/middleware/memoizer_spec.rb +0 -518
- data/spec/flipper/middleware/setup_env_spec.rb +0 -94
- data/spec/flipper/model/active_record_spec.rb +0 -72
- data/spec/flipper/poller_spec.rb +0 -390
- data/spec/flipper/registry_spec.rb +0 -140
- data/spec/flipper/serializers/gzip_spec.rb +0 -13
- data/spec/flipper/serializers/json_spec.rb +0 -13
- data/spec/flipper/typecast_spec.rb +0 -232
- data/spec/flipper/types/actor_spec.rb +0 -132
- data/spec/flipper/types/boolean_spec.rb +0 -23
- data/spec/flipper/types/group_spec.rb +0 -112
- data/spec/flipper/types/percentage_of_actors_spec.rb +0 -5
- data/spec/flipper/types/percentage_of_time_spec.rb +0 -5
- data/spec/flipper/types/percentage_spec.rb +0 -46
- data/spec/flipper_integration_spec.rb +0 -783
- data/spec/flipper_spec.rb +0 -422
- data/spec/spec_helper.rb +0 -112
- data/spec/support/actor_names.yml +0 -1
- data/spec/support/descriptions.yml +0 -1
- data/spec/support/fail_on_output.rb +0 -8
- data/spec/support/fake_backoff_policy.rb +0 -15
- data/spec/support/fake_udp_socket.rb +0 -27
- data/spec/support/skippable.rb +0 -18
- data/spec/support/spec_helpers.rb +0 -111
- data/test/adapters/actor_limit_test.rb +0 -20
- data/test/adapters/memory_test.rb +0 -9
- data/test/adapters/pstore_test.rb +0 -17
- data/test/test_helper.rb +0 -7
- data/test_rails/generators/flipper/setup_generator_test.rb +0 -69
- data/test_rails/generators/flipper/update_generator_test.rb +0 -96
- data/test_rails/helper.rb +0 -31
- data/test_rails/system/test_help_test.rb +0 -52
|
@@ -21,6 +21,7 @@ module Flipper
|
|
|
21
21
|
@memoize = false
|
|
22
22
|
@features_key = :flipper_features
|
|
23
23
|
@get_all_key = :all_memoized
|
|
24
|
+
@cache_key_by_feature_key = Hash.new { |hash, key| hash[key] = "feature/#{key}".freeze }
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# Public
|
|
@@ -53,7 +54,9 @@ module Flipper
|
|
|
53
54
|
# Public
|
|
54
55
|
def get(feature)
|
|
55
56
|
if memoizing?
|
|
56
|
-
cache.fetch(key_for(feature.key))
|
|
57
|
+
cache.fetch(key_for(feature.key)) do
|
|
58
|
+
cache[key_for(feature.key)] = preloaded_missing?(feature) ? default_config : @adapter.get(feature)
|
|
59
|
+
end
|
|
57
60
|
else
|
|
58
61
|
@adapter.get(feature)
|
|
59
62
|
end
|
|
@@ -62,7 +65,10 @@ module Flipper
|
|
|
62
65
|
# Public
|
|
63
66
|
def get_multi(features)
|
|
64
67
|
if memoizing?
|
|
65
|
-
uncached_features = features.reject
|
|
68
|
+
uncached_features = features.reject do |feature|
|
|
69
|
+
cache[key_for(feature.key)] ||= default_config if preloaded_missing?(feature)
|
|
70
|
+
cache[key_for(feature.key)]
|
|
71
|
+
end
|
|
66
72
|
|
|
67
73
|
if uncached_features.any?
|
|
68
74
|
response = @adapter.get_multi(uncached_features)
|
|
@@ -155,16 +161,26 @@ module Flipper
|
|
|
155
161
|
|
|
156
162
|
private
|
|
157
163
|
|
|
164
|
+
def preloaded_missing?(feature)
|
|
165
|
+
cache[@get_all_key] && cache[@features_key] && !cache[@features_key].include?(feature.key)
|
|
166
|
+
end
|
|
167
|
+
|
|
158
168
|
def key_for(key)
|
|
159
|
-
|
|
169
|
+
@cache_key_by_feature_key[key]
|
|
160
170
|
end
|
|
161
171
|
|
|
162
172
|
def expire_feature(feature)
|
|
163
|
-
|
|
173
|
+
if memoizing?
|
|
174
|
+
cache.delete(key_for(feature.key))
|
|
175
|
+
cache.delete(@get_all_key)
|
|
176
|
+
end
|
|
164
177
|
end
|
|
165
178
|
|
|
166
179
|
def expire_features_set
|
|
167
|
-
|
|
180
|
+
if memoizing?
|
|
181
|
+
cache.delete(@features_key)
|
|
182
|
+
cache.delete(@get_all_key)
|
|
183
|
+
end
|
|
168
184
|
end
|
|
169
185
|
end
|
|
170
186
|
end
|
|
@@ -8,47 +8,71 @@ module Flipper
|
|
|
8
8
|
class Memory
|
|
9
9
|
include ::Flipper::Adapter
|
|
10
10
|
|
|
11
|
+
# Private: Shared frozen config for unknown features. Gate values
|
|
12
|
+
# returned by get are treated as read-only.
|
|
13
|
+
DEFAULT_CONFIG = default_config.each_value(&:freeze).freeze
|
|
14
|
+
|
|
11
15
|
# Public
|
|
12
16
|
def initialize(source = nil, threadsafe: true)
|
|
13
17
|
@source = Typecast.features_hash(source)
|
|
14
18
|
@lock = Mutex.new if threadsafe
|
|
15
|
-
|
|
19
|
+
@sync_version = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Public
|
|
23
|
+
def sync_version
|
|
24
|
+
synchronize { @sync_version }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Public
|
|
28
|
+
def sync_version=(version)
|
|
29
|
+
synchronize { @sync_version = version }
|
|
16
30
|
end
|
|
17
31
|
|
|
18
|
-
# Public: The set of known features.
|
|
32
|
+
# Public: The set of known features. It's frozen and reused until a
|
|
33
|
+
# write, because Strict checks it on every read.
|
|
19
34
|
def features
|
|
20
|
-
synchronize { @source.keys }
|
|
35
|
+
synchronize { @features ||= @source.keys.to_set.freeze }
|
|
21
36
|
end
|
|
22
37
|
|
|
23
38
|
# Public: Adds a feature to the set of known features.
|
|
24
39
|
def add(feature)
|
|
25
|
-
synchronize
|
|
40
|
+
synchronize do
|
|
41
|
+
@features = nil
|
|
42
|
+
@source[feature.key] ||= default_config
|
|
43
|
+
end
|
|
26
44
|
true
|
|
27
45
|
end
|
|
28
46
|
|
|
29
47
|
# Public: Removes a feature from the set of known features and clears
|
|
30
48
|
# all the values for the feature.
|
|
31
49
|
def remove(feature)
|
|
32
|
-
synchronize
|
|
50
|
+
synchronize do
|
|
51
|
+
@features = nil
|
|
52
|
+
@source.delete(feature.key)
|
|
53
|
+
end
|
|
33
54
|
true
|
|
34
55
|
end
|
|
35
56
|
|
|
36
57
|
# Public: Clears all the gate values for a feature.
|
|
37
58
|
def clear(feature)
|
|
38
|
-
synchronize
|
|
59
|
+
synchronize do
|
|
60
|
+
@features = nil
|
|
61
|
+
@source[feature.key] = default_config
|
|
62
|
+
end
|
|
39
63
|
true
|
|
40
64
|
end
|
|
41
65
|
|
|
42
66
|
# Public
|
|
43
67
|
def get(feature)
|
|
44
|
-
synchronize { @source[feature.key] } ||
|
|
68
|
+
synchronize { @source[feature.key] } || DEFAULT_CONFIG
|
|
45
69
|
end
|
|
46
70
|
|
|
47
71
|
def get_multi(features)
|
|
48
72
|
synchronize do
|
|
49
73
|
result = {}
|
|
50
74
|
features.each do |feature|
|
|
51
|
-
result[feature.key] = @source[feature.key] ||
|
|
75
|
+
result[feature.key] = @source[feature.key] || DEFAULT_CONFIG
|
|
52
76
|
end
|
|
53
77
|
result
|
|
54
78
|
end
|
|
@@ -61,22 +85,25 @@ module Flipper
|
|
|
61
85
|
# Public
|
|
62
86
|
def enable(feature, gate, thing)
|
|
63
87
|
synchronize do
|
|
64
|
-
|
|
88
|
+
# Write a copy, because get hands out the stored hash and its sets.
|
|
89
|
+
gates = (@source[feature.key] || default_config).dup
|
|
65
90
|
|
|
66
91
|
case gate.data_type
|
|
67
92
|
when :boolean
|
|
68
|
-
|
|
69
|
-
|
|
93
|
+
gates = default_config
|
|
94
|
+
gates[gate.key] = thing.value.to_s
|
|
70
95
|
when :integer
|
|
71
|
-
|
|
96
|
+
gates[gate.key] = thing.value.to_s
|
|
72
97
|
when :set
|
|
73
|
-
|
|
98
|
+
gates[gate.key] = gates[gate.key] | [thing.value.to_s]
|
|
74
99
|
when :json
|
|
75
|
-
|
|
100
|
+
gates[gate.key] = thing.value
|
|
76
101
|
else
|
|
77
102
|
raise "#{gate} is not supported by this adapter yet"
|
|
78
103
|
end
|
|
79
104
|
|
|
105
|
+
@features = nil
|
|
106
|
+
@source[feature.key] = gates
|
|
80
107
|
true
|
|
81
108
|
end
|
|
82
109
|
end
|
|
@@ -84,21 +111,23 @@ module Flipper
|
|
|
84
111
|
# Public
|
|
85
112
|
def disable(feature, gate, thing)
|
|
86
113
|
synchronize do
|
|
87
|
-
@source[feature.key]
|
|
114
|
+
gates = (@source[feature.key] || default_config).dup
|
|
88
115
|
|
|
89
116
|
case gate.data_type
|
|
90
117
|
when :boolean
|
|
91
|
-
|
|
118
|
+
gates = default_config
|
|
92
119
|
when :integer
|
|
93
|
-
|
|
120
|
+
gates[gate.key] = thing.value.to_s
|
|
94
121
|
when :set
|
|
95
|
-
|
|
122
|
+
gates[gate.key] = gates[gate.key] - [thing.value.to_s]
|
|
96
123
|
when :json
|
|
97
|
-
|
|
124
|
+
gates.delete(gate.key)
|
|
98
125
|
else
|
|
99
126
|
raise "#{gate} is not supported by this adapter yet"
|
|
100
127
|
end
|
|
101
128
|
|
|
129
|
+
@features = nil
|
|
130
|
+
@source[feature.key] = gates
|
|
102
131
|
true
|
|
103
132
|
end
|
|
104
133
|
end
|
|
@@ -112,28 +141,31 @@ module Flipper
|
|
|
112
141
|
"#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
|
|
113
142
|
end
|
|
114
143
|
|
|
115
|
-
# Public: a more efficient implementation of import for this adapter
|
|
144
|
+
# 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.
|
|
116
148
|
def import(source)
|
|
117
149
|
adapter = self.class.from(source)
|
|
118
150
|
get_all = Typecast.features_hash(adapter.get_all)
|
|
119
|
-
|
|
120
|
-
true
|
|
121
|
-
end
|
|
151
|
+
version = adapter.sync_version
|
|
122
152
|
|
|
123
|
-
|
|
153
|
+
synchronize do
|
|
154
|
+
unless version&.older_than?(@sync_version)
|
|
155
|
+
yield get_all, version if block_given?
|
|
156
|
+
@features = nil
|
|
157
|
+
@source.replace(get_all)
|
|
158
|
+
@sync_version = version if version
|
|
159
|
+
end
|
|
160
|
+
end
|
|
124
161
|
|
|
125
|
-
|
|
126
|
-
@pid = Process.pid
|
|
127
|
-
@lock&.unlock if @lock&.locked?
|
|
162
|
+
true
|
|
128
163
|
end
|
|
129
164
|
|
|
130
|
-
|
|
131
|
-
@pid != Process.pid
|
|
132
|
-
end
|
|
165
|
+
private
|
|
133
166
|
|
|
134
167
|
def synchronize(&block)
|
|
135
168
|
if @lock
|
|
136
|
-
reset if forked?
|
|
137
169
|
@lock.synchronize(&block)
|
|
138
170
|
else
|
|
139
171
|
block.call
|
|
@@ -4,7 +4,6 @@ require 'flipper/poller'
|
|
|
4
4
|
module Flipper
|
|
5
5
|
module Adapters
|
|
6
6
|
class Poll
|
|
7
|
-
extend Forwardable
|
|
8
7
|
include ::Flipper::Adapter
|
|
9
8
|
|
|
10
9
|
# Deprecated
|
|
@@ -12,12 +11,21 @@ module Flipper
|
|
|
12
11
|
|
|
13
12
|
attr_reader :adapter, :poller
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
# poller - The Flipper::Poller that fetches the remote flags.
|
|
15
|
+
# adapter - The adapter that serves reads and writes.
|
|
16
|
+
# options - The Hash of options.
|
|
17
|
+
# :synchronizer - Internal: called instead of syncing the
|
|
18
|
+
# poller's flags into adapter.
|
|
19
|
+
def initialize(poller, adapter, options = {})
|
|
18
20
|
@adapter = adapter
|
|
19
21
|
@poller = poller
|
|
22
|
+
@synchronizer = options.fetch(:synchronizer) {
|
|
23
|
+
-> { Flipper::Adapters::Sync::Synchronizer.new(@adapter, @poller.adapter).call }
|
|
24
|
+
}
|
|
20
25
|
@last_synced_at = 0
|
|
26
|
+
# Flipper::Poller has a lock-free synced_at. Other pollers may only
|
|
27
|
+
# have last_synced_at.
|
|
28
|
+
@poller_has_synced_at = poller.respond_to?(:synced_at)
|
|
21
29
|
|
|
22
30
|
# If the adapter is empty, we need to sync before starting the poller.
|
|
23
31
|
# Yes, this will block the main thread, but that's better than thinking
|
|
@@ -36,13 +44,50 @@ module Flipper
|
|
|
36
44
|
@poller.start
|
|
37
45
|
end
|
|
38
46
|
|
|
47
|
+
# Written out rather than delegated so hot reads don't allocate args.
|
|
48
|
+
def features
|
|
49
|
+
synced_adapter.features
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get(feature)
|
|
53
|
+
synced_adapter.get(feature)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def get_multi(features)
|
|
57
|
+
synced_adapter.get_multi(features)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def get_all(**kwargs)
|
|
61
|
+
synced_adapter.get_all(**kwargs)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def add(feature)
|
|
65
|
+
synced_adapter.add(feature)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def remove(feature)
|
|
69
|
+
synced_adapter.remove(feature)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def clear(feature)
|
|
73
|
+
synced_adapter.clear(feature)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def enable(feature, gate, thing)
|
|
77
|
+
synced_adapter.enable(feature, gate, thing)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def disable(feature, gate, thing)
|
|
81
|
+
synced_adapter.disable(feature, gate, thing)
|
|
82
|
+
end
|
|
83
|
+
|
|
39
84
|
private
|
|
40
85
|
|
|
41
86
|
def synced_adapter
|
|
42
87
|
@poller.start
|
|
43
|
-
poller_last_synced_at = @poller.last_synced_at.value
|
|
88
|
+
poller_last_synced_at = @poller_has_synced_at ? @poller.synced_at : @poller.last_synced_at.value
|
|
44
89
|
if poller_last_synced_at > @last_synced_at
|
|
45
|
-
|
|
90
|
+
@synchronizer.call
|
|
46
91
|
@last_synced_at = poller_last_synced_at
|
|
47
92
|
end
|
|
48
93
|
@adapter
|
|
@@ -27,6 +27,21 @@ module Flipper
|
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
# Public: The Flipper::SyncVersion of the flags last synced into this
|
|
31
|
+
# adapter, shared by every process using the same file.
|
|
32
|
+
def sync_version
|
|
33
|
+
@store.transaction do
|
|
34
|
+
SyncVersion.parse(read(SyncVersion::KEY))
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Public: Records the Flipper::SyncVersion of the flags just synced.
|
|
39
|
+
def sync_version=(version)
|
|
40
|
+
@store.transaction do
|
|
41
|
+
write SyncVersion::KEY, version
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
30
45
|
# Public: Adds a feature to the set of known features.
|
|
31
46
|
def add(feature)
|
|
32
47
|
@store.transaction do
|
|
@@ -4,7 +4,7 @@ module Flipper
|
|
|
4
4
|
module Adapters
|
|
5
5
|
# Public: Adapter that wraps another adapter and raises for any writes.
|
|
6
6
|
class ReadOnly < Wrapper
|
|
7
|
-
WRITE_METHODS = %i[add remove clear enable disable]
|
|
7
|
+
WRITE_METHODS = %i[import add remove clear enable disable sync_version=]
|
|
8
8
|
|
|
9
9
|
class WriteAttempted < Error
|
|
10
10
|
def initialize(message = nil)
|
|
@@ -32,6 +32,22 @@ module Flipper
|
|
|
32
32
|
ensure
|
|
33
33
|
self.sync_mode = old_value
|
|
34
34
|
end
|
|
35
|
+
|
|
36
|
+
# Internal: Runs the block while the feature with key is added on
|
|
37
|
+
# purpose (Feature#add), so add lets that one feature be created.
|
|
38
|
+
# Other features, and reads, stay strict inside the block.
|
|
39
|
+
def adding(key)
|
|
40
|
+
old_value = Thread.current[:flipper_strict_adding]
|
|
41
|
+
Thread.current[:flipper_strict_adding] = key
|
|
42
|
+
yield
|
|
43
|
+
ensure
|
|
44
|
+
Thread.current[:flipper_strict_adding] = old_value
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Internal: Whether the feature with key is being added on purpose.
|
|
48
|
+
def adding?(key)
|
|
49
|
+
Thread.current[:flipper_strict_adding] == key
|
|
50
|
+
end
|
|
35
51
|
end
|
|
36
52
|
|
|
37
53
|
def initialize(adapter, handler = nil, &block)
|
|
@@ -40,7 +56,7 @@ module Flipper
|
|
|
40
56
|
end
|
|
41
57
|
|
|
42
58
|
def add(feature)
|
|
43
|
-
assert_feature_exists(feature) unless self.class.
|
|
59
|
+
assert_feature_exists(feature) unless self.class.adding?(feature.key)
|
|
44
60
|
super
|
|
45
61
|
end
|
|
46
62
|
|
|
@@ -58,7 +58,9 @@ module Flipper
|
|
|
58
58
|
if remote_expression.nil?
|
|
59
59
|
@feature.disable_expression
|
|
60
60
|
else
|
|
61
|
-
|
|
61
|
+
# Remote adapters can hold legacy empty-group expressions, which
|
|
62
|
+
# must be mirrored faithfully without weakening public enables.
|
|
63
|
+
@feature.__send__ :enable_expression_from_sync, remote_expression
|
|
62
64
|
end
|
|
63
65
|
end
|
|
64
66
|
|
|
@@ -39,9 +39,28 @@ module Flipper
|
|
|
39
39
|
private
|
|
40
40
|
|
|
41
41
|
def sync
|
|
42
|
-
local_get_all = @local.get_all
|
|
43
42
|
remote_get_all = @remote.get_all(cache_bust: @cache_bust)
|
|
44
43
|
|
|
44
|
+
# Never replace flags with older ones from a stale remote response.
|
|
45
|
+
remote_version = @remote.sync_version
|
|
46
|
+
local_version = @local.sync_version if remote_version
|
|
47
|
+
if remote_version&.older_than?(local_version)
|
|
48
|
+
@instrumenter.instrument("synchronizer_stale.flipper", {
|
|
49
|
+
local_version: local_version,
|
|
50
|
+
remote_version: remote_version,
|
|
51
|
+
})
|
|
52
|
+
return
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Record before writing flags so a concurrent sync of older flags
|
|
56
|
+
# into a shared adapter sees this version as soon as possible.
|
|
57
|
+
# Flipper::Cloud::SharedMemory#reload copies again for this reason.
|
|
58
|
+
if remote_version && remote_version != local_version
|
|
59
|
+
@local.sync_version = remote_version
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
local_get_all = @local.get_all
|
|
63
|
+
|
|
45
64
|
# Sync all the gate values.
|
|
46
65
|
remote_get_all.each do |feature_key, remote_gates_hash|
|
|
47
66
|
feature = Feature.new(feature_key, @local, instrumenter: @instrumenter)
|
|
@@ -57,6 +57,14 @@ module Flipper
|
|
|
57
57
|
@local.get_all(**kwargs)
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def sync_version
|
|
61
|
+
@local.sync_version
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def sync_version=(version)
|
|
65
|
+
@local.sync_version = version
|
|
66
|
+
end
|
|
67
|
+
|
|
60
68
|
def add(feature)
|
|
61
69
|
@remote.add(feature).tap { @local.add(feature) }
|
|
62
70
|
end
|
|
@@ -18,6 +18,8 @@ module Flipper
|
|
|
18
18
|
:get_all,
|
|
19
19
|
:enable,
|
|
20
20
|
:disable,
|
|
21
|
+
:sync_version,
|
|
22
|
+
:sync_version=,
|
|
21
23
|
].freeze
|
|
22
24
|
|
|
23
25
|
attr_reader :adapter
|
|
@@ -38,6 +40,11 @@ module Flipper
|
|
|
38
40
|
end
|
|
39
41
|
end
|
|
40
42
|
|
|
43
|
+
# Wrapping an adapter doesn't make it writable.
|
|
44
|
+
def read_only?
|
|
45
|
+
@adapter.read_only?
|
|
46
|
+
end
|
|
47
|
+
|
|
41
48
|
# Override this method to customize the behavior of all delegated methods, and just yield to
|
|
42
49
|
# the block to call the wrapped adapter.
|
|
43
50
|
if RUBY_VERSION >= '3.0'
|
data/lib/flipper/cli.rb
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
require 'optparse'
|
|
2
|
+
require 'bundler/vendored_thor'
|
|
3
|
+
require 'bundler/ui/shell'
|
|
4
|
+
require 'flipper/version'
|
|
2
5
|
|
|
3
6
|
module Flipper
|
|
4
7
|
class CLI < OptionParser
|
|
@@ -16,6 +19,7 @@ module Flipper
|
|
|
16
19
|
|
|
17
20
|
# Program is always flipper, no matter how it's invoked
|
|
18
21
|
@program_name = 'flipper'
|
|
22
|
+
self.version = Flipper::VERSION
|
|
19
23
|
@require = ENV.fetch("FLIPPER_REQUIRE", DEFAULT_REQUIRE)
|
|
20
24
|
@commands = {}
|
|
21
25
|
|
|
@@ -37,19 +41,19 @@ module Flipper
|
|
|
37
41
|
values << Types::Group.new(name)
|
|
38
42
|
end
|
|
39
43
|
c.on('-p NUM', '--percentage-of-actors=NUM', Numeric, "#{action} for a percentage of actors") do |num|
|
|
40
|
-
values << Types::PercentageOfActors
|
|
44
|
+
values << percentage(Types::PercentageOfActors, num)
|
|
41
45
|
end
|
|
42
46
|
c.on('-t NUM', '--percentage-of-time=NUM', Numeric, "#{action} for a percentage of time") do |num|
|
|
43
|
-
values << Types::PercentageOfTime
|
|
47
|
+
values << percentage(Types::PercentageOfTime, num)
|
|
44
48
|
end
|
|
45
|
-
c.on('-x expressions', '--expression=
|
|
49
|
+
c.on('-x expressions', '--expression=JSON', "#{action} for the given expression") do |expression|
|
|
46
50
|
begin
|
|
47
51
|
values << Flipper::Expression.build(JSON.parse(expression))
|
|
48
52
|
rescue JSON::ParserError => e
|
|
49
53
|
ui.error "JSON parse error #{e.message}"
|
|
50
54
|
ui.trace(e)
|
|
51
55
|
exit 1
|
|
52
|
-
rescue ArgumentError => e
|
|
56
|
+
rescue ArgumentError, NameError => e
|
|
53
57
|
ui.error "Invalid expression: #{e.message}"
|
|
54
58
|
ui.trace(e)
|
|
55
59
|
exit 1
|
|
@@ -57,6 +61,7 @@ module Flipper
|
|
|
57
61
|
end
|
|
58
62
|
|
|
59
63
|
c.action do |feature|
|
|
64
|
+
usage_error!(c.help, "Missing feature name") unless feature
|
|
60
65
|
f = Flipper.feature(feature)
|
|
61
66
|
|
|
62
67
|
if values.empty?
|
|
@@ -78,8 +83,10 @@ module Flipper
|
|
|
78
83
|
end
|
|
79
84
|
|
|
80
85
|
command 'show' do |c|
|
|
86
|
+
c.banner = "Usage: #{c.program_name} [options] <feature>"
|
|
81
87
|
c.description = "Show a defined feature"
|
|
82
88
|
c.action do |feature|
|
|
89
|
+
usage_error!(c.help, "Missing feature name") unless feature
|
|
83
90
|
ui.info feature_details(Flipper.feature(feature))
|
|
84
91
|
end
|
|
85
92
|
end
|
|
@@ -94,6 +101,11 @@ module Flipper
|
|
|
94
101
|
|
|
95
102
|
command 'cloud' do |c|
|
|
96
103
|
c.description = "Flipper Cloud commands"
|
|
104
|
+
c.banner = "Usage: #{c.program_name} <command>"
|
|
105
|
+
c.separator ""
|
|
106
|
+
c.separator "Commands:"
|
|
107
|
+
c.separator " migrate Migrate features to a new Flipper Cloud account"
|
|
108
|
+
c.separator " push Push features to an existing Flipper Cloud project (asks for the token)"
|
|
97
109
|
c.action do |subcommand = nil, *args|
|
|
98
110
|
require 'flipper/cloud/migrate'
|
|
99
111
|
|
|
@@ -102,8 +114,8 @@ module Flipper
|
|
|
102
114
|
result = Flipper::Cloud.migrate(Flipper)
|
|
103
115
|
if result.url
|
|
104
116
|
ui.info "Migrating to Flipper Cloud..."
|
|
117
|
+
ui.info "Open this URL in your browser to finish migrating your features:"
|
|
105
118
|
ui.info result.url
|
|
106
|
-
system("open", result.url)
|
|
107
119
|
else
|
|
108
120
|
message = "Migration failed (HTTP #{result.code})"
|
|
109
121
|
message << ": #{result.message}" if result.message
|
|
@@ -111,9 +123,9 @@ module Flipper
|
|
|
111
123
|
exit 1
|
|
112
124
|
end
|
|
113
125
|
when 'push'
|
|
114
|
-
token = args.first
|
|
115
|
-
|
|
116
|
-
ui.error "Usage: flipper cloud push
|
|
126
|
+
token = args.first || prompt_for_cloud_token
|
|
127
|
+
if token.to_s.empty?
|
|
128
|
+
ui.error "Usage: flipper cloud push [token]"
|
|
117
129
|
exit 1
|
|
118
130
|
end
|
|
119
131
|
result = Flipper::Cloud.push(token, Flipper)
|
|
@@ -125,12 +137,10 @@ module Flipper
|
|
|
125
137
|
ui.error message
|
|
126
138
|
exit 1
|
|
127
139
|
end
|
|
140
|
+
when nil
|
|
141
|
+
ui.info c.help
|
|
128
142
|
else
|
|
129
|
-
|
|
130
|
-
ui.info ""
|
|
131
|
-
ui.info "Commands:"
|
|
132
|
-
ui.info " migrate Migrate features to a new Flipper Cloud account"
|
|
133
|
-
ui.info " push Push features to an existing Flipper Cloud project"
|
|
143
|
+
usage_error!(c.help, "Unknown command: cloud #{subcommand}")
|
|
134
144
|
end
|
|
135
145
|
end
|
|
136
146
|
end
|
|
@@ -138,6 +148,7 @@ module Flipper
|
|
|
138
148
|
command 'help' do |c|
|
|
139
149
|
c.load_environment = false
|
|
140
150
|
c.action do |command = nil|
|
|
151
|
+
usage_error!(help, "Unknown command: #{command}") if command && !@commands[command]
|
|
141
152
|
ui.info command ? @commands[command].help : help
|
|
142
153
|
end
|
|
143
154
|
end
|
|
@@ -152,6 +163,11 @@ module Flipper
|
|
|
152
163
|
exit
|
|
153
164
|
end
|
|
154
165
|
|
|
166
|
+
on_tail('-v', '--version', 'Print version') do
|
|
167
|
+
ui.info ver
|
|
168
|
+
exit
|
|
169
|
+
end
|
|
170
|
+
|
|
155
171
|
# Set help documentation
|
|
156
172
|
self.banner = "Usage: #{program_name} [options] <command>"
|
|
157
173
|
separator ""
|
|
@@ -169,18 +185,21 @@ module Flipper
|
|
|
169
185
|
def run(argv)
|
|
170
186
|
command, *args = order(argv)
|
|
171
187
|
|
|
172
|
-
if @commands[command]
|
|
173
|
-
|
|
174
|
-
|
|
188
|
+
if (cmd = @commands[command])
|
|
189
|
+
# Print help before loading the app, so it works outside one too.
|
|
190
|
+
if cmd.help_requested?(args)
|
|
191
|
+
ui.info cmd.help
|
|
192
|
+
exit
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
load_environment! if cmd.load_environment
|
|
196
|
+
cmd.run(args)
|
|
197
|
+
elsif command
|
|
198
|
+
usage_error!(help, "Unknown command: #{command}")
|
|
175
199
|
else
|
|
176
200
|
ui.info help
|
|
177
|
-
|
|
178
|
-
if command
|
|
179
|
-
ui.error "Unknown command: #{command}"
|
|
180
|
-
exit 1
|
|
181
|
-
end
|
|
182
201
|
end
|
|
183
|
-
rescue OptionParser::
|
|
202
|
+
rescue OptionParser::ParseError => e
|
|
184
203
|
ui.error e.message
|
|
185
204
|
exit 1
|
|
186
205
|
end
|
|
@@ -191,6 +210,32 @@ module Flipper
|
|
|
191
210
|
block.call(@commands[name])
|
|
192
211
|
end
|
|
193
212
|
|
|
213
|
+
# Build a percentage type, reporting an out of range value as a bad argument.
|
|
214
|
+
def percentage(type, value)
|
|
215
|
+
type.new(value)
|
|
216
|
+
rescue ArgumentError => e
|
|
217
|
+
raise OptionParser::InvalidArgument, e.message
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Print usage to stdout, the error to stderr, and exit non-zero.
|
|
221
|
+
def usage_error!(usage, message)
|
|
222
|
+
ui.info usage
|
|
223
|
+
ui.error message
|
|
224
|
+
exit 1
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Ask for the Cloud token without echoing it, so it stays out of shell
|
|
228
|
+
# history and the process list. Returns nil when stdin isn't a terminal.
|
|
229
|
+
def prompt_for_cloud_token
|
|
230
|
+
return unless $stdin.tty?
|
|
231
|
+
|
|
232
|
+
require "io/console"
|
|
233
|
+
shell.stderr.print "Flipper Cloud token: "
|
|
234
|
+
token = $stdin.noecho(&:gets)
|
|
235
|
+
shell.stderr.puts
|
|
236
|
+
token&.strip
|
|
237
|
+
end
|
|
238
|
+
|
|
194
239
|
def load_environment!
|
|
195
240
|
ENV["FLIPPER_CLOUD_LOGGING_ENABLED"] ||= "false"
|
|
196
241
|
require File.expand_path(@require)
|
|
@@ -309,6 +354,26 @@ module Flipper
|
|
|
309
354
|
def action(&block)
|
|
310
355
|
@action = block
|
|
311
356
|
end
|
|
357
|
+
|
|
358
|
+
# Whether argv asks for help: -h or --help before any --, and not the
|
|
359
|
+
# value of an option that takes one (enable -a --help is an actor).
|
|
360
|
+
# Abbreviated (--act) and bundled (-ah) options aren't followed.
|
|
361
|
+
def help_requested?(argv)
|
|
362
|
+
argv = argv.take_while { |arg| arg != "--" }
|
|
363
|
+
|
|
364
|
+
until argv.empty?
|
|
365
|
+
arg = argv.shift
|
|
366
|
+
return true if arg == "-h" || arg == "--help"
|
|
367
|
+
|
|
368
|
+
switch = case arg
|
|
369
|
+
when /\A--([^=]+)\z/ then search(:long, $1)
|
|
370
|
+
when /\A-(\w)\z/ then search(:short, $1)
|
|
371
|
+
end
|
|
372
|
+
argv.shift if switch.is_a?(OptionParser::Switch::RequiredArgument)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
false
|
|
376
|
+
end
|
|
312
377
|
end
|
|
313
378
|
end
|
|
314
379
|
end
|