flipper 1.4.1 → 1.5.0.rc1
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 +136 -21
- 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 +84 -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 -19
- 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 -257
- data/spec/flipper/cloud/dsl_spec.rb +0 -90
- data/spec/flipper/cloud/message_verifier_spec.rb +0 -104
- data/spec/flipper/cloud/middleware_spec.rb +0 -307
- 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
data/lib/flipper/cloud.rb
CHANGED
|
@@ -24,13 +24,15 @@ module Flipper
|
|
|
24
24
|
def self.app(flipper = nil, options = {})
|
|
25
25
|
env_key = options.fetch(:env_key, 'flipper')
|
|
26
26
|
memoizer_options = options.fetch(:memoizer_options, {})
|
|
27
|
+
middleware_options = {env_key: env_key}
|
|
28
|
+
middleware_options[:signature_tolerance] = options[:signature_tolerance] if options.key?(:signature_tolerance)
|
|
27
29
|
|
|
28
30
|
app = ->(_) { [404, { Rack::CONTENT_TYPE => 'application/json'.freeze }, ['{}'.freeze]] }
|
|
29
31
|
builder = Rack::Builder.new
|
|
30
32
|
yield builder if block_given?
|
|
31
33
|
builder.use Flipper::Middleware::SetupEnv, flipper, env_key: env_key
|
|
32
34
|
builder.use Flipper::Middleware::Memoizer, memoizer_options.merge(env_key: env_key)
|
|
33
|
-
builder.use Flipper::Cloud::Middleware,
|
|
35
|
+
builder.use Flipper::Cloud::Middleware, middleware_options
|
|
34
36
|
builder.run app
|
|
35
37
|
klass = self
|
|
36
38
|
app = builder.to_app
|
|
@@ -38,16 +40,32 @@ module Flipper
|
|
|
38
40
|
app
|
|
39
41
|
end
|
|
40
42
|
|
|
41
|
-
# Private: Configure Flipper to use Cloud by default
|
|
42
|
-
|
|
43
|
+
# Private: Configure Flipper to use Cloud by default. Reads are served
|
|
44
|
+
# from one Memory adapter that every thread in the process shares, unless
|
|
45
|
+
# FLIPPER_CLOUD_MEMORY_READS is false, which reads the local adapter.
|
|
46
|
+
#
|
|
47
|
+
# options - A block that returns more options for each new instance.
|
|
48
|
+
def self.set_default(&options)
|
|
43
49
|
if ENV["FLIPPER_CLOUD_TOKEN"]
|
|
44
50
|
Flipper.configure do |config|
|
|
51
|
+
memory = Flipper::Cloud::SharedMemory.new if memory_reads?
|
|
45
52
|
config.default do
|
|
46
|
-
|
|
53
|
+
adapter_options = if memory
|
|
54
|
+
{ memory: memory, adapter_builder: config }
|
|
55
|
+
else
|
|
56
|
+
{ local_adapter: config.adapter }
|
|
57
|
+
end
|
|
58
|
+
self.new((options ? options.call : {}).merge(adapter_options))
|
|
47
59
|
end
|
|
48
60
|
end
|
|
49
61
|
end
|
|
50
62
|
end
|
|
63
|
+
|
|
64
|
+
# Private: Returns false when FLIPPER_CLOUD_MEMORY_READS turns reading
|
|
65
|
+
# from memory off (default: true).
|
|
66
|
+
def self.memory_reads?
|
|
67
|
+
Flipper::Typecast.to_boolean(ENV.fetch("FLIPPER_CLOUD_MEMORY_READS", "true"))
|
|
68
|
+
end
|
|
51
69
|
end
|
|
52
70
|
end
|
|
53
71
|
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
require 'concurrent/map'
|
|
2
|
+
|
|
1
3
|
module Flipper
|
|
2
4
|
class Configuration
|
|
3
5
|
def initialize(options = {})
|
|
4
6
|
@builder = AdapterBuilder.new { store Flipper::Adapters::Memory }
|
|
5
7
|
@default = -> { Flipper.new(@builder.to_adapter) }
|
|
8
|
+
@named_configurations = Concurrent::Map.new
|
|
6
9
|
end
|
|
7
10
|
|
|
8
11
|
# The default adapter to use.
|
|
@@ -24,20 +27,37 @@ module Flipper
|
|
|
24
27
|
#
|
|
25
28
|
def adapter(&block)
|
|
26
29
|
if block_given?
|
|
27
|
-
@builder.store(block)
|
|
30
|
+
result = @builder.store(block)
|
|
31
|
+
changed!
|
|
32
|
+
result
|
|
28
33
|
else
|
|
29
34
|
@builder.to_adapter
|
|
30
35
|
end
|
|
31
36
|
end
|
|
32
37
|
|
|
38
|
+
# Internal: Builds a new storage adapter, without the adapters added with
|
|
39
|
+
# #use. Flipper::Cloud uses it and #wrap to put Memory in front of storage.
|
|
40
|
+
def build_store
|
|
41
|
+
@builder.build_store
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Internal: Wraps adapter with the adapters added with #use.
|
|
45
|
+
def wrap(adapter)
|
|
46
|
+
@builder.wrap(adapter)
|
|
47
|
+
end
|
|
48
|
+
|
|
33
49
|
# An adapter to use to augment the primary storage adapter. See `AdapterBuilder#use`
|
|
34
50
|
if RUBY_VERSION >= '3.0'
|
|
35
51
|
def use(klass, *args, **kwargs, &block)
|
|
36
|
-
@builder.use(klass, *args, **kwargs, &block)
|
|
52
|
+
result = @builder.use(klass, *args, **kwargs, &block)
|
|
53
|
+
changed!
|
|
54
|
+
result
|
|
37
55
|
end
|
|
38
56
|
else
|
|
39
57
|
def use(klass, *args, &block)
|
|
40
|
-
@builder.use(klass, *args, &block)
|
|
58
|
+
result = @builder.use(klass, *args, &block)
|
|
59
|
+
changed!
|
|
60
|
+
result
|
|
41
61
|
end
|
|
42
62
|
end
|
|
43
63
|
|
|
@@ -61,11 +81,67 @@ module Flipper
|
|
|
61
81
|
def default(&block)
|
|
62
82
|
if block_given?
|
|
63
83
|
@default = block
|
|
84
|
+
changed!
|
|
85
|
+
block
|
|
64
86
|
else
|
|
65
87
|
@default.call
|
|
66
88
|
end
|
|
67
89
|
end
|
|
68
90
|
|
|
91
|
+
# Public: Configure a named Flipper instance. Calling it again with the
|
|
92
|
+
# same name reopens that instance's configuration, like calling adapter
|
|
93
|
+
# again replaces the adapter.
|
|
94
|
+
#
|
|
95
|
+
# config.named(:cross_app) do |cross_app|
|
|
96
|
+
# cross_app.adapter { ... }
|
|
97
|
+
# end
|
|
98
|
+
#
|
|
99
|
+
# name - Lowercase snake-case name used by Flipper.named and Flipper.<name>.
|
|
100
|
+
# block - Configuration block yielded a Flipper::NamedConfiguration.
|
|
101
|
+
#
|
|
102
|
+
# Returns the named configuration.
|
|
103
|
+
def named(name)
|
|
104
|
+
name = Flipper.send(:normalize_named_instance_name, name)
|
|
105
|
+
Flipper.send(:validate_named_instance_name!, name)
|
|
106
|
+
|
|
107
|
+
if (existing = named_configuration(name))
|
|
108
|
+
yield existing if block_given?
|
|
109
|
+
return existing
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A new name is only registered once its block finishes, so a block that
|
|
113
|
+
# raises leaves no half-configured instance behind. No lock is held
|
|
114
|
+
# while the block runs; if another thread registered the name first,
|
|
115
|
+
# reopen theirs with this block so neither thread's settings are lost.
|
|
116
|
+
created = NamedConfiguration.new(name)
|
|
117
|
+
yield created if block_given?
|
|
118
|
+
published = @named_configurations.put_if_absent(name, created) || created
|
|
119
|
+
yield published if block_given? && !published.equal?(created)
|
|
120
|
+
published
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Public: Returns a configured named child without creating it.
|
|
124
|
+
def named_configuration(name)
|
|
125
|
+
# Stored names are already normalized, so a Symbol hit needs no checks.
|
|
126
|
+
# This is on the path of every Flipper.<name> call.
|
|
127
|
+
if name.is_a?(Symbol)
|
|
128
|
+
found = @named_configurations[name]
|
|
129
|
+
return found if found
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
@named_configurations[Flipper.send(:normalize_named_instance_name, name)]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Public: Returns the configured named instance names.
|
|
136
|
+
def named_instance_names
|
|
137
|
+
@named_configurations.keys
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Public: Returns the configured named configurations.
|
|
141
|
+
def named_configurations
|
|
142
|
+
@named_configurations.values
|
|
143
|
+
end
|
|
144
|
+
|
|
69
145
|
def statsd
|
|
70
146
|
require 'flipper/instrumentation/statsd_subscriber'
|
|
71
147
|
Flipper::Instrumentation::StatsdSubscriber.client
|
|
@@ -75,5 +151,12 @@ module Flipper
|
|
|
75
151
|
require "flipper/instrumentation/statsd"
|
|
76
152
|
Flipper::Instrumentation::StatsdSubscriber.client = client
|
|
77
153
|
end
|
|
154
|
+
|
|
155
|
+
private
|
|
156
|
+
|
|
157
|
+
# Internal: Called after the adapter, adapter stack or default changes.
|
|
158
|
+
# NamedConfiguration uses it to invalidate cached instances.
|
|
159
|
+
def changed!
|
|
160
|
+
end
|
|
78
161
|
end
|
|
79
162
|
end
|
data/lib/flipper/dsl.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'forwardable'
|
|
2
|
+
require 'concurrent/map'
|
|
2
3
|
|
|
3
4
|
module Flipper
|
|
4
5
|
class DSL
|
|
@@ -20,12 +21,38 @@ module Flipper
|
|
|
20
21
|
# :memoize - Should adapter be wrapped by memoize adapter or not.
|
|
21
22
|
def initialize(adapter, options = {})
|
|
22
23
|
@instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
|
|
24
|
+
@group_resolver = Flipper
|
|
25
|
+
@feature_resolver = Flipper
|
|
26
|
+
@instance_key = nil
|
|
23
27
|
memoize = options.fetch(:memoize, true)
|
|
24
28
|
adapter = Adapters::Memoizable.new(adapter) if memoize
|
|
25
29
|
@adapter = adapter
|
|
26
|
-
|
|
30
|
+
# Concurrent::Map so a single DSL instance shared across threads (e.g. a
|
|
31
|
+
# Flipper.new(adapter) or Flipper::Cloud.new held in a constant) can
|
|
32
|
+
# memoize features without a non-atomic Hash mutation racing.
|
|
33
|
+
@memoized_features = Concurrent::Map.new
|
|
27
34
|
end
|
|
28
35
|
|
|
36
|
+
# Internal: Claim this DSL for the default or one named instance. Called
|
|
37
|
+
# while Flipper's instance owner lock is held.
|
|
38
|
+
def claim_instance_owner(configuration)
|
|
39
|
+
return self if @instance_owner.equal?(configuration)
|
|
40
|
+
|
|
41
|
+
if @instance_owner
|
|
42
|
+
raise InvalidConfigurationValue, "A Flipper DSL cannot be shared by multiple Flipper instances"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
@instance_owner = configuration
|
|
46
|
+
unless configuration.equal?(Flipper)
|
|
47
|
+
@group_resolver = configuration
|
|
48
|
+
@feature_resolver = self
|
|
49
|
+
@instance_key = configuration.name
|
|
50
|
+
@memoized_features.clear
|
|
51
|
+
end
|
|
52
|
+
self
|
|
53
|
+
end
|
|
54
|
+
private :claim_instance_owner
|
|
55
|
+
|
|
29
56
|
# Public: Check if a feature is enabled.
|
|
30
57
|
#
|
|
31
58
|
# name - The String or Symbol name of the feature.
|
|
@@ -218,7 +245,13 @@ module Flipper
|
|
|
218
245
|
raise ArgumentError, "#{name} must be a String or Symbol"
|
|
219
246
|
end
|
|
220
247
|
|
|
221
|
-
@memoized_features
|
|
248
|
+
@memoized_features.compute_if_absent(name.to_sym) do
|
|
249
|
+
Feature.new(name, @adapter,
|
|
250
|
+
instrumenter: instrumenter,
|
|
251
|
+
group_resolver: @group_resolver,
|
|
252
|
+
feature_resolver: @feature_resolver,
|
|
253
|
+
instance_key: @instance_key)
|
|
254
|
+
end
|
|
222
255
|
end
|
|
223
256
|
|
|
224
257
|
# Public: Preload the features with the given names.
|
|
@@ -240,6 +273,12 @@ module Flipper
|
|
|
240
273
|
keys.map { |key| feature(key) }
|
|
241
274
|
end
|
|
242
275
|
|
|
276
|
+
# Internal: Returns false when preloading saves no adapter reads, so
|
|
277
|
+
# Flipper::Middleware::Memoizer skips it.
|
|
278
|
+
def preload?
|
|
279
|
+
true
|
|
280
|
+
end
|
|
281
|
+
|
|
243
282
|
# Public: Shortcut access to a feature instance by name.
|
|
244
283
|
#
|
|
245
284
|
# name - The String or Symbol name of the feature.
|
|
@@ -253,9 +292,14 @@ module Flipper
|
|
|
253
292
|
#
|
|
254
293
|
# Returns an instance of Flipper::Group.
|
|
255
294
|
def group(name)
|
|
256
|
-
|
|
295
|
+
@group_resolver.group(name)
|
|
257
296
|
end
|
|
258
297
|
|
|
298
|
+
# Public: Group registration for this DSL's owning instance (Flipper for
|
|
299
|
+
# the default instance, the named configuration for a named one).
|
|
300
|
+
def_delegators :@group_resolver,
|
|
301
|
+
:register, :groups, :group_names, :group_exists?, :unregister_groups
|
|
302
|
+
|
|
259
303
|
# Public: Gets the expression for the feature.
|
|
260
304
|
#
|
|
261
305
|
# name - The String or Symbol name of the feature.
|
data/lib/flipper/engine.rb
CHANGED
|
@@ -44,14 +44,11 @@ module Flipper
|
|
|
44
44
|
|
|
45
45
|
require 'flipper/cloud' if cloud?
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
config.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
instrumenter: app.config.flipper.instrumenter
|
|
53
|
-
)
|
|
54
|
-
else
|
|
47
|
+
if cloud?
|
|
48
|
+
Flipper::Cloud.set_default { { instrumenter: app.config.flipper.instrumenter } }
|
|
49
|
+
else
|
|
50
|
+
Flipper.configure do |config|
|
|
51
|
+
config.default do
|
|
55
52
|
Flipper.new(config.adapter, instrumenter: app.config.flipper.instrumenter)
|
|
56
53
|
end
|
|
57
54
|
end
|
|
@@ -73,6 +70,23 @@ module Flipper
|
|
|
73
70
|
config.use Flipper::Adapters::Strict, flipper.strict if flipper.strict
|
|
74
71
|
config.use Flipper::Adapters::ActorLimit, flipper.actor_limit if flipper.actor_limit
|
|
75
72
|
end
|
|
73
|
+
|
|
74
|
+
if flipper.test_help
|
|
75
|
+
require "flipper/test_help"
|
|
76
|
+
Flipper::TestHelp.send(:flipper_configure_named_instances)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
Flipper.send(:named_configurations).each do |named|
|
|
80
|
+
named.inherit_rails_configuration(flipper)
|
|
81
|
+
if named.cloud? && !flipper.test_help
|
|
82
|
+
named.resolve_cloud_credentials({
|
|
83
|
+
token: app.credentials.dig(:flipper, named.name, :cloud_token),
|
|
84
|
+
sync_secret: app.credentials.dig(:flipper, named.name, :cloud_sync_secret),
|
|
85
|
+
})
|
|
86
|
+
end
|
|
87
|
+
named.use Flipper::Adapters::Strict, named.strict if named.strict
|
|
88
|
+
named.use Flipper::Adapters::ActorLimit, named.actor_limit if named.actor_limit
|
|
89
|
+
end
|
|
76
90
|
end
|
|
77
91
|
|
|
78
92
|
initializer "flipper.memoizer", after: :load_config_initializers do |app|
|
|
@@ -85,6 +99,43 @@ module Flipper
|
|
|
85
99
|
if: flipper.memoize.respond_to?(:call) ? flipper.memoize : nil
|
|
86
100
|
}
|
|
87
101
|
end
|
|
102
|
+
|
|
103
|
+
named_configurations = Flipper.send(:named_configurations)
|
|
104
|
+
env_keys = [flipper.env_key] + named_configurations.map(&:env_key)
|
|
105
|
+
if env_keys.uniq.length != env_keys.length
|
|
106
|
+
raise InvalidConfigurationValue, "Flipper Rack environment keys must be unique"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
named_configurations.each do |named|
|
|
110
|
+
next unless named.memoize
|
|
111
|
+
|
|
112
|
+
app.middleware.use Flipper::Middleware::SetupEnv, Flipper.named(named.name), {
|
|
113
|
+
env_key: named.env_key,
|
|
114
|
+
}
|
|
115
|
+
app.middleware.use Flipper::Middleware::Memoizer, {
|
|
116
|
+
env_key: named.env_key,
|
|
117
|
+
preload: named.preload,
|
|
118
|
+
if: named.memoize.respond_to?(:call) ? named.memoize : nil,
|
|
119
|
+
}
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
initializer "flipper.named_cloud_paths", after: :load_config_initializers do |app|
|
|
124
|
+
next if app.config.flipper.test_help
|
|
125
|
+
|
|
126
|
+
named_paths = Flipper.send(:named_configurations).map(&:cloud_webhook_path).compact
|
|
127
|
+
default_paths = if cloud? && !ENV.fetch("FLIPPER_CLOUD_SYNC_SECRET", "").empty?
|
|
128
|
+
[app.config.flipper.cloud_path]
|
|
129
|
+
else
|
|
130
|
+
[]
|
|
131
|
+
end
|
|
132
|
+
paths = (default_paths + named_paths).map { |path| Engine.normalize_cloud_path(path) }
|
|
133
|
+
endpoints = paths.flat_map do |path|
|
|
134
|
+
[path, [path, "webhooks"].reject(&:empty?).join("/")]
|
|
135
|
+
end
|
|
136
|
+
if endpoints.uniq.length != endpoints.length
|
|
137
|
+
raise InvalidConfigurationValue, "Flipper Cloud webhook paths must be unique"
|
|
138
|
+
end
|
|
88
139
|
end
|
|
89
140
|
|
|
90
141
|
initializer "flipper.test" do |app|
|
|
@@ -95,6 +146,11 @@ module Flipper
|
|
|
95
146
|
!!ENV["FLIPPER_CLOUD_TOKEN"]
|
|
96
147
|
end
|
|
97
148
|
|
|
149
|
+
# Internal: Cloud mount path without leading or trailing slashes.
|
|
150
|
+
def self.normalize_cloud_path(path)
|
|
151
|
+
path.to_s.sub(%r{\A/+}, "").sub(%r{/+\z}, "")
|
|
152
|
+
end
|
|
153
|
+
|
|
98
154
|
def self.deprecated_rails_version?
|
|
99
155
|
Gem::Version.new(Rails.version) < Gem::Version.new(Flipper::NEXT_REQUIRED_RAILS_VERSION)
|
|
100
156
|
end
|
data/lib/flipper/errors.rb
CHANGED
|
@@ -9,9 +9,27 @@ module Flipper
|
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
# Raised when writing a feature whose key starts with
|
|
13
|
+
# Flipper::RESERVED_KEY_PREFIX. An ArgumentError, like the adapters' other
|
|
14
|
+
# reserved name errors.
|
|
15
|
+
class ReservedKeyError < ArgumentError
|
|
16
|
+
attr_reader :key
|
|
17
|
+
|
|
18
|
+
def initialize(key)
|
|
19
|
+
@key = key.to_s
|
|
20
|
+
super "#{@key.inspect} is reserved for Flipper's internal use and cannot be used as a feature name"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
12
24
|
# Raised when attempting to declare a group name that has already been used.
|
|
13
25
|
class DuplicateGroup < Error; end
|
|
14
26
|
|
|
27
|
+
# Raised when a named instance has not been configured.
|
|
28
|
+
class NamedInstanceNotFound < Error; end
|
|
29
|
+
|
|
30
|
+
# Raised when a named instance cannot be exposed as a Flipper method.
|
|
31
|
+
class InvalidNamedInstanceName < Error; end
|
|
32
|
+
|
|
15
33
|
# Raised when an invalid value is set to a configuration property
|
|
16
34
|
class InvalidConfigurationValue < Flipper::Error
|
|
17
35
|
def initialize(message = nil)
|
data/lib/flipper/export.rb
CHANGED
|
@@ -11,6 +11,12 @@ module Flipper
|
|
|
11
11
|
# Internal: JSON export class that knows how to build features hash
|
|
12
12
|
# from data.
|
|
13
13
|
class Export < ::Flipper::Export
|
|
14
|
+
# The maximum size, in bytes, of an import payload. A legitimate export
|
|
15
|
+
# is small: even 100k feature-heavy gates is only ~20 MB. Anything past
|
|
16
|
+
# this ceiling is almost certainly abuse, so import endpoints reject it
|
|
17
|
+
# before reading the whole body into memory.
|
|
18
|
+
MAX_BYTES = 50 * 1024 * 1024
|
|
19
|
+
|
|
14
20
|
def initialize(contents:, version: 1)
|
|
15
21
|
super contents: contents, version: version, format: :json
|
|
16
22
|
end
|
|
@@ -8,15 +8,11 @@ module Flipper
|
|
|
8
8
|
VERSION = 1
|
|
9
9
|
|
|
10
10
|
def call(adapter)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
case value
|
|
17
|
-
when Set
|
|
18
|
-
features[feature_key][key] = value.to_a
|
|
19
|
-
end
|
|
11
|
+
# Build a new structure rather than mutating the hash returned by
|
|
12
|
+
# the adapter, which may be a reference to the adapter's live data.
|
|
13
|
+
features = adapter.get_all.transform_values do |gates|
|
|
14
|
+
gates.transform_values do |value|
|
|
15
|
+
value.is_a?(Set) ? value.to_a : value
|
|
20
16
|
end
|
|
21
17
|
end
|
|
22
18
|
|
|
@@ -20,6 +20,12 @@ module Flipper
|
|
|
20
20
|
other.is_a?(self.class) && other.value == value
|
|
21
21
|
end
|
|
22
22
|
alias_method :==, :eql?
|
|
23
|
+
|
|
24
|
+
# eql? uses ==, so 1 and 1.0 are equal. Hash numbers as floats so
|
|
25
|
+
# they hash the same too.
|
|
26
|
+
def hash
|
|
27
|
+
[self.class, value.is_a?(Numeric) && value.real? ? value.to_f : value].hash
|
|
28
|
+
end
|
|
23
29
|
end
|
|
24
30
|
end
|
|
25
31
|
end
|
data/lib/flipper/expression.rb
CHANGED
|
@@ -5,6 +5,14 @@ module Flipper
|
|
|
5
5
|
class Expression
|
|
6
6
|
include Builder
|
|
7
7
|
|
|
8
|
+
PruneResult = Struct.new(:expression, :constant_value, keyword_init: true)
|
|
9
|
+
private_constant :PruneResult
|
|
10
|
+
|
|
11
|
+
# Raised by .build for a function this version of Flipper doesn't have,
|
|
12
|
+
# like one added in a newer release. A NameError, so existing rescues of
|
|
13
|
+
# NameError keep working.
|
|
14
|
+
class UnknownFunction < NameError; end
|
|
15
|
+
|
|
8
16
|
def self.build(object)
|
|
9
17
|
return object if object.is_a?(self) || object.is_a?(Constant)
|
|
10
18
|
|
|
@@ -16,6 +24,11 @@ module Flipper
|
|
|
16
24
|
raise ArgumentError, "#{object.inspect} cannot be converted into an expression"
|
|
17
25
|
end
|
|
18
26
|
|
|
27
|
+
# A single function argument can skip the array, like
|
|
28
|
+
# { "Number" => { "Property" => "age" } }. Array() would split the
|
|
29
|
+
# hash into key/value pairs, so wrap it instead.
|
|
30
|
+
args = [args] if args.is_a?(Hash)
|
|
31
|
+
|
|
19
32
|
new(name, Array(args).map { |o| build(o) })
|
|
20
33
|
when String, Numeric, FalseClass, TrueClass
|
|
21
34
|
Expression::Constant.new(object)
|
|
@@ -33,7 +46,13 @@ module Flipper
|
|
|
33
46
|
|
|
34
47
|
def initialize(name, args = [])
|
|
35
48
|
@name = name.to_s
|
|
36
|
-
|
|
49
|
+
# Only constants defined directly on Expressions are functions. A plain
|
|
50
|
+
# const_get would also find top-level ones like Integer or Object.
|
|
51
|
+
function_name = @name.to_sym
|
|
52
|
+
unless Expressions.constants(false).include?(function_name)
|
|
53
|
+
raise UnknownFunction.new("uninitialized constant Flipper::Expressions::#{@name}", function_name)
|
|
54
|
+
end
|
|
55
|
+
@function = Expressions.const_get(function_name, false)
|
|
37
56
|
@args = args
|
|
38
57
|
end
|
|
39
58
|
|
|
@@ -50,12 +69,97 @@ module Flipper
|
|
|
50
69
|
end
|
|
51
70
|
alias_method :==, :eql?
|
|
52
71
|
|
|
72
|
+
def hash
|
|
73
|
+
[self.class, function, args].hash
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Public: Returns true if this expression contains an Any/All group with
|
|
77
|
+
# no arguments anywhere in its tree. An empty All evaluates to true for
|
|
78
|
+
# every actor and an empty Any to false for every actor, so persisting
|
|
79
|
+
# one is almost always a mistake.
|
|
80
|
+
def empty_groups?
|
|
81
|
+
return true if group? && args.empty?
|
|
82
|
+
|
|
83
|
+
args.any? { |arg| arg.is_a?(Expression) && arg.empty_groups? }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Public: Returns a copy of this expression with empty Any/All groups
|
|
87
|
+
# removed wherever removal cannot broaden the expression, or nil when no
|
|
88
|
+
# conditions remain. An empty All can be removed from an All parent (it
|
|
89
|
+
# contributes a constant true) and an empty Any from an Any parent (a
|
|
90
|
+
# constant false), but an empty Any inside an All is kept because
|
|
91
|
+
# removing it would broaden the expression from never-true.
|
|
92
|
+
def prune_empty_groups
|
|
93
|
+
prune_empty_groups_with_identity.expression
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Public: Returns true when this expression is guaranteed to match every
|
|
97
|
+
# actor, e.g. an empty All group reaches an Any group or the root.
|
|
98
|
+
def always_true?
|
|
99
|
+
constant_group_value == true
|
|
100
|
+
end
|
|
101
|
+
|
|
53
102
|
def value
|
|
54
103
|
{
|
|
55
104
|
name => args.map(&:value)
|
|
56
105
|
}
|
|
57
106
|
end
|
|
58
107
|
|
|
108
|
+
protected
|
|
109
|
+
|
|
110
|
+
def prune_empty_groups_with_identity
|
|
111
|
+
return PruneResult.new(expression: self) unless group?
|
|
112
|
+
return PruneResult.new(constant_value: all?) if args.empty?
|
|
113
|
+
|
|
114
|
+
pruned_args = args.map do |arg|
|
|
115
|
+
if arg.is_a?(Expression)
|
|
116
|
+
arg.prune_empty_groups_with_identity
|
|
117
|
+
else
|
|
118
|
+
PruneResult.new(expression: arg)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
kept = pruned_args.map do |result|
|
|
123
|
+
if result.constant_value == false
|
|
124
|
+
result.expression || build("Any" => []) if all?
|
|
125
|
+
elsif result.constant_value.nil?
|
|
126
|
+
result.expression
|
|
127
|
+
end
|
|
128
|
+
end.compact
|
|
129
|
+
|
|
130
|
+
# No condition remains. Preserve this group's constant value for an
|
|
131
|
+
# enclosing group, which lets an empty All disappear from All without
|
|
132
|
+
# treating it as the unsafe empty-Any-in-All case.
|
|
133
|
+
if kept.empty?
|
|
134
|
+
return PruneResult.new(constant_value: constant_group_value)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
pruned = build(name => kept)
|
|
138
|
+
PruneResult.new(
|
|
139
|
+
expression: pruned,
|
|
140
|
+
constant_value: pruned.constant_group_value
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def constant_group_value
|
|
145
|
+
return nil unless group?
|
|
146
|
+
return all? if args.empty?
|
|
147
|
+
|
|
148
|
+
values = args.map do |arg|
|
|
149
|
+
arg.is_a?(Expression) ? arg.constant_group_value : nil
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
if all?
|
|
153
|
+
return false if values.include?(false)
|
|
154
|
+
return true if values.all?(true)
|
|
155
|
+
else
|
|
156
|
+
return true if values.include?(true)
|
|
157
|
+
return false if values.all?(false)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
nil
|
|
161
|
+
end
|
|
162
|
+
|
|
59
163
|
private
|
|
60
164
|
|
|
61
165
|
def call_with_context?
|
|
@@ -7,6 +7,11 @@ module Flipper
|
|
|
7
7
|
|
|
8
8
|
def self.call(left, right)
|
|
9
9
|
left.respond_to?(operator) && right.respond_to?(operator) && left.public_send(operator, right)
|
|
10
|
+
rescue ArgumentError, TypeError
|
|
11
|
+
# Operands respond to the operator but aren't type-compatible
|
|
12
|
+
# (e.g. "25" > 21 raises ArgumentError, {} > 21 raises TypeError).
|
|
13
|
+
# Treat as false.
|
|
14
|
+
false
|
|
10
15
|
end
|
|
11
16
|
end
|
|
12
17
|
end
|
|
@@ -9,24 +9,28 @@ module Flipper
|
|
|
9
9
|
evaluating = Thread.current[EVALUATING_KEY] ||= Set.new
|
|
10
10
|
feature_name = feature_name.to_s
|
|
11
11
|
current_feature = context[:feature_name].to_s
|
|
12
|
+
instance_key = context[:flipper_instance_key]
|
|
13
|
+
feature_identity = instance_key ? [instance_key, feature_name] : feature_name
|
|
14
|
+
current_identity = instance_key ? [instance_key, current_feature] : current_feature
|
|
12
15
|
|
|
13
16
|
# Track the current feature so A -> B -> A is caught
|
|
14
|
-
added_current = evaluating.add?(
|
|
17
|
+
added_current = evaluating.add?(current_identity)
|
|
15
18
|
|
|
16
19
|
begin
|
|
17
20
|
# Circular dependency: return false to break the cycle
|
|
18
|
-
return false if evaluating.include?(
|
|
21
|
+
return false if evaluating.include?(feature_identity)
|
|
19
22
|
|
|
20
|
-
evaluating.add(
|
|
23
|
+
evaluating.add(feature_identity)
|
|
21
24
|
actor = context[:actor]
|
|
25
|
+
feature_resolver = context.fetch(:feature_resolver, Flipper)
|
|
22
26
|
if actor
|
|
23
|
-
|
|
27
|
+
feature_resolver.enabled?(feature_name, actor)
|
|
24
28
|
else
|
|
25
|
-
|
|
29
|
+
feature_resolver.enabled?(feature_name)
|
|
26
30
|
end
|
|
27
31
|
ensure
|
|
28
|
-
evaluating.delete(
|
|
29
|
-
evaluating.delete(
|
|
32
|
+
evaluating.delete(feature_identity)
|
|
33
|
+
evaluating.delete(current_identity) if added_current
|
|
30
34
|
end
|
|
31
35
|
end
|
|
32
36
|
end
|