star_gates 0.3.1 → 0.4.0
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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +26 -0
- data/README.md +86 -22
- data/Rakefile +6 -1
- data/lib/star_gates/adapters/base_adapter.rb +2 -0
- data/lib/star_gates/adapters/redis_adapter.rb +15 -10
- data/lib/star_gates/application_star_gate.rb +107 -117
- data/lib/star_gates/circuit.rb +74 -109
- data/lib/star_gates/configuration.rb +26 -9
- data/lib/star_gates/dsl.rb +7 -10
- data/lib/star_gates/errors.rb +15 -3
- data/lib/star_gates/event_store/event_store.rb +6 -16
- data/lib/star_gates/event_store/memory_adapter.rb +7 -1
- data/lib/star_gates/events/base_event.rb +3 -1
- data/lib/star_gates/feature.rb +36 -60
- data/lib/star_gates/flags.rb +102 -25
- data/lib/star_gates/gate.rb +58 -46
- data/lib/star_gates/hexagonal/feature_aggregate.rb +49 -55
- data/lib/star_gates/hexagonal/feature_repository.rb +5 -27
- data/lib/star_gates/hexagonal/feature_service.rb +31 -33
- data/lib/star_gates/jsonrpc_rails_adapter.rb +24 -7
- data/lib/star_gates/projections/feature_projection.rb +40 -13
- data/lib/star_gates/registry.rb +3 -2
- data/lib/star_gates/retry_policy.rb +23 -0
- data/lib/star_gates/rollout.rb +15 -0
- data/lib/star_gates/telemetry/opentelemetry_adapter.rb +71 -75
- data/lib/star_gates/telemetry/prometheus_adapter.rb +82 -74
- data/lib/star_gates/telemetry/telemetry_adapter.rb +16 -15
- data/lib/star_gates/version.rb +1 -1
- data/lib/star_gates.rb +56 -58
- data/sig/star_gates.rbs +832 -0
- metadata +42 -10
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module StarGates
|
|
4
|
+
# Process-wide defaults. `default_timeout` and `cache_ttl` are Integer
|
|
5
|
+
# nanoseconds.
|
|
4
6
|
class Configuration
|
|
7
|
+
NS_PER_SECOND = 1_000_000_000
|
|
8
|
+
private_constant :NS_PER_SECOND
|
|
9
|
+
|
|
5
10
|
attr_accessor :instrumentation, :telemetry_adapter, :default_timeout, :default_retry_count,
|
|
6
11
|
:circuit_breaker_threshold, :cache_ttl, :environment, :feature_defaults,
|
|
7
12
|
:enable_performance_monitoring, :log_level, :error_reporting
|
|
@@ -9,10 +14,10 @@ module StarGates
|
|
|
9
14
|
def initialize
|
|
10
15
|
@instrumentation = true
|
|
11
16
|
@telemetry_adapter = detect_default_telemetry_adapter
|
|
12
|
-
@default_timeout = 5
|
|
17
|
+
@default_timeout = 5 * NS_PER_SECOND
|
|
13
18
|
@default_retry_count = 3
|
|
14
19
|
@circuit_breaker_threshold = 5
|
|
15
|
-
@cache_ttl = 300
|
|
20
|
+
@cache_ttl = 300 * NS_PER_SECOND
|
|
16
21
|
@environment = detect_environment
|
|
17
22
|
@feature_defaults = {}
|
|
18
23
|
@enable_performance_monitoring = true
|
|
@@ -53,9 +58,12 @@ module StarGates
|
|
|
53
58
|
base_config.merge(feature_specific)
|
|
54
59
|
end
|
|
55
60
|
|
|
56
|
-
# Configure feature-specific defaults
|
|
61
|
+
# Configure feature-specific defaults. Keys are stored as Symbols, so an
|
|
62
|
+
# override read from YAML (String keys) replaces the Symbol-keyed default
|
|
63
|
+
# it names instead of sitting beside it; a key that is neither a String
|
|
64
|
+
# nor a Symbol is a TypeError.
|
|
57
65
|
def configure_feature(feature_name, config = {})
|
|
58
|
-
feature_defaults[feature_name.to_sym] = config
|
|
66
|
+
feature_defaults[feature_name.to_sym] = config.to_h { |key, value| [config_key(key), value] }
|
|
59
67
|
end
|
|
60
68
|
|
|
61
69
|
# Load from YAML file (Rails-style)
|
|
@@ -87,21 +95,21 @@ module StarGates
|
|
|
87
95
|
case environment
|
|
88
96
|
when :development
|
|
89
97
|
@log_level = :debug
|
|
90
|
-
@default_timeout = 10
|
|
98
|
+
@default_timeout = 10 * NS_PER_SECOND
|
|
91
99
|
@telemetry_adapter = :memory
|
|
92
100
|
when :test
|
|
93
101
|
@instrumentation = false
|
|
94
102
|
@telemetry_adapter = :memory
|
|
95
|
-
@default_timeout = 1
|
|
103
|
+
@default_timeout = 1 * NS_PER_SECOND
|
|
96
104
|
@enable_performance_monitoring = false
|
|
97
105
|
when :production
|
|
98
106
|
@log_level = :warn
|
|
99
|
-
@default_timeout = 2
|
|
107
|
+
@default_timeout = 2 * NS_PER_SECOND
|
|
100
108
|
@telemetry_adapter = :prometheus
|
|
101
109
|
@error_reporting = true
|
|
102
110
|
when :staging
|
|
103
111
|
@log_level = :info
|
|
104
|
-
@default_timeout = 5
|
|
112
|
+
@default_timeout = 5 * NS_PER_SECOND
|
|
105
113
|
@telemetry_adapter = :prometheus
|
|
106
114
|
end
|
|
107
115
|
end
|
|
@@ -125,6 +133,12 @@ module StarGates
|
|
|
125
133
|
|
|
126
134
|
private
|
|
127
135
|
|
|
136
|
+
def config_key(key)
|
|
137
|
+
return key.to_sym if key.is_a?(String) || key.is_a?(Symbol)
|
|
138
|
+
|
|
139
|
+
raise TypeError, "feature config key must be a String or Symbol, got #{key.inspect}"
|
|
140
|
+
end
|
|
141
|
+
|
|
128
142
|
def detect_environment
|
|
129
143
|
if defined?(Rails) && Rails.respond_to?(:env)
|
|
130
144
|
Rails.env.to_sym
|
|
@@ -162,8 +176,11 @@ module StarGates
|
|
|
162
176
|
configuration.load_from_file(file_path) if file_path
|
|
163
177
|
end
|
|
164
178
|
|
|
179
|
+
# The application's config/star_gates.yml: under Rails.root inside a Rails
|
|
180
|
+
# application, else under the working directory. Rails can be loaded with
|
|
181
|
+
# no application (Rails.root nil), so that falls back too.
|
|
165
182
|
def self.default_config_file_path
|
|
166
|
-
if defined?(Rails)
|
|
183
|
+
if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
167
184
|
Rails.root.join('config', 'star_gates.yml')
|
|
168
185
|
else
|
|
169
186
|
File.join(Dir.pwd, 'config', 'star_gates.yml')
|
data/lib/star_gates/dsl.rb
CHANGED
|
@@ -8,7 +8,7 @@ module StarGates
|
|
|
8
8
|
def initialize(name)
|
|
9
9
|
@name = name
|
|
10
10
|
@feature_config = {}
|
|
11
|
-
@circuit_config =
|
|
11
|
+
@circuit_config = nil
|
|
12
12
|
@run_block = nil
|
|
13
13
|
@fallback_block = nil
|
|
14
14
|
end
|
|
@@ -17,13 +17,10 @@ module StarGates
|
|
|
17
17
|
@feature_config = { rollout: rollout, group: group }
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
reset_after: reset_after,
|
|
25
|
-
retry_config: retry_config
|
|
26
|
-
}
|
|
20
|
+
# A BreakerMachines::Policy and an optional RetryPolicy, as
|
|
21
|
+
# Circuit.new takes them; the circuit is wired to this gate's feature.
|
|
22
|
+
def circuit(policy, retry_policy = nil)
|
|
23
|
+
@circuit_config = [policy, retry_policy]
|
|
27
24
|
end
|
|
28
25
|
|
|
29
26
|
def run(&block)
|
|
@@ -36,7 +33,7 @@ module StarGates
|
|
|
36
33
|
|
|
37
34
|
def build
|
|
38
35
|
feature_instance = build_feature if feature_config.any?
|
|
39
|
-
circuit_instance = build_circuit(feature_instance) if circuit_config
|
|
36
|
+
circuit_instance = build_circuit(feature_instance) if circuit_config
|
|
40
37
|
|
|
41
38
|
StarGates::Gate.new(
|
|
42
39
|
name,
|
|
@@ -55,7 +52,7 @@ module StarGates
|
|
|
55
52
|
|
|
56
53
|
def build_circuit(feature)
|
|
57
54
|
# Wire circuit to feature for self-healing behavior
|
|
58
|
-
StarGates::Circuit.new(
|
|
55
|
+
StarGates::Circuit.new(*circuit_config, feature: feature)
|
|
59
56
|
end
|
|
60
57
|
end
|
|
61
58
|
|
data/lib/star_gates/errors.rb
CHANGED
|
@@ -2,7 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
module StarGates
|
|
4
4
|
class Error < StandardError; end
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
|
|
6
|
+
# A gate answered without running its primary path and had no fallback.
|
|
7
|
+
# `refusal` says why: :ineligible (the feature is off for this context),
|
|
8
|
+
# :failed (the primary raised; it is the `cause`), :exceeded (the time
|
|
9
|
+
# budget ran out), or the circuit's own refusal symbol (:open, :busy,
|
|
10
|
+
# :full), carried whole from breaker_machines.
|
|
11
|
+
class UnavailableError < Error
|
|
12
|
+
attr_reader :gate, :refusal
|
|
13
|
+
|
|
14
|
+
def initialize(gate, refusal, message = nil)
|
|
15
|
+
@gate = gate
|
|
16
|
+
@refusal = refusal
|
|
17
|
+
super(message || "#{gate} is unavailable (#{refusal})")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
8
20
|
end
|
|
@@ -8,21 +8,12 @@ module StarGates
|
|
|
8
8
|
@subscribers = Hash.new { |h, k| h[k] = [] }
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
#
|
|
11
|
+
# Appends the events and answers how many, or :version_conflict when
|
|
12
|
+
# `expected_version` is not the aggregate's version (nothing stored).
|
|
12
13
|
def append(aggregate_id, events, expected_version: nil)
|
|
13
14
|
events = Array(events)
|
|
14
|
-
return if events.empty?
|
|
15
|
-
|
|
16
|
-
# Validate expected version for optimistic concurrency
|
|
17
|
-
if expected_version
|
|
18
|
-
current_version = @adapter.get_version(aggregate_id)
|
|
19
|
-
if current_version != expected_version
|
|
20
|
-
raise ConcurrencyError, "Expected version #{expected_version}, got #{current_version}"
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Store events
|
|
25
|
-
@adapter.append(aggregate_id, events)
|
|
15
|
+
return 0 if events.empty?
|
|
16
|
+
return :version_conflict unless @adapter.append(aggregate_id, events, expected_version)
|
|
26
17
|
|
|
27
18
|
# Notify subscribers
|
|
28
19
|
events.each do |event|
|
|
@@ -37,7 +28,8 @@ module StarGates
|
|
|
37
28
|
@adapter.get_events(aggregate_id, from_version: from_version)
|
|
38
29
|
end
|
|
39
30
|
|
|
40
|
-
#
|
|
31
|
+
# Events at or after `timestamp` (wall-clock Integer nanoseconds),
|
|
32
|
+
# newest first.
|
|
41
33
|
def get_events_from(timestamp, limit: 100)
|
|
42
34
|
@adapter.get_events_from(timestamp, limit: limit)
|
|
43
35
|
end
|
|
@@ -73,7 +65,5 @@ module StarGates
|
|
|
73
65
|
@subscribers[:all].each { |subscriber| subscriber.call(event) }
|
|
74
66
|
end
|
|
75
67
|
end
|
|
76
|
-
|
|
77
|
-
class ConcurrencyError < StandardError; end
|
|
78
68
|
end
|
|
79
69
|
end
|
|
@@ -12,8 +12,13 @@ module StarGates
|
|
|
12
12
|
@mutex = Mutex.new
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
# Stores the events and answers true, or false without storing them
|
|
16
|
+
# when `expected_version` is given and is not the current one. Checked
|
|
17
|
+
# and stored under one lock, so two writers cannot both pass.
|
|
18
|
+
def append(aggregate_id, events, expected_version = nil)
|
|
16
19
|
@mutex.synchronize do
|
|
20
|
+
return false unless expected_version.nil? || @versions[aggregate_id] == expected_version
|
|
21
|
+
|
|
17
22
|
events.each do |event|
|
|
18
23
|
# Increment version
|
|
19
24
|
@versions[aggregate_id] += 1
|
|
@@ -27,6 +32,7 @@ module StarGates
|
|
|
27
32
|
# Store globally for cross-aggregate queries
|
|
28
33
|
@global_events << versioned_event
|
|
29
34
|
end
|
|
35
|
+
true
|
|
30
36
|
end
|
|
31
37
|
end
|
|
32
38
|
|
|
@@ -4,6 +4,8 @@ require 'securerandom'
|
|
|
4
4
|
|
|
5
5
|
module StarGates
|
|
6
6
|
module Events
|
|
7
|
+
# An immutable domain event. `occurred_at` is wall-clock Integer
|
|
8
|
+
# nanoseconds since the Unix epoch: events outlive the process.
|
|
7
9
|
class BaseEvent
|
|
8
10
|
attr_reader :event_id, :aggregate_id, :event_type, :data, :metadata, :occurred_at, :version
|
|
9
11
|
|
|
@@ -16,7 +18,7 @@ module StarGates
|
|
|
16
18
|
source: 'star_gates',
|
|
17
19
|
correlation_id: Thread.current[:correlation_id] || SecureRandom.uuid
|
|
18
20
|
).freeze
|
|
19
|
-
@occurred_at =
|
|
21
|
+
@occurred_at = StarGates.wall_time
|
|
20
22
|
@version = version
|
|
21
23
|
freeze
|
|
22
24
|
end
|
data/lib/star_gates/feature.rb
CHANGED
|
@@ -5,13 +5,21 @@ require 'state_machines'
|
|
|
5
5
|
require 'concurrent'
|
|
6
6
|
|
|
7
7
|
module StarGates
|
|
8
|
+
# A feature's lifecycle (disabled, canary, enabled) and who it reaches.
|
|
9
|
+
#
|
|
10
|
+
# `rollout:` is an Integer percentage, 0..100; `group:` is nil, a Symbol
|
|
11
|
+
# matched against `context[:groups]`, or a Proc called with the context.
|
|
12
|
+
# TypeError for another type, RangeError for a rollout outside 0..100.
|
|
13
|
+
# Instants and spans are monotonic Integer nanoseconds.
|
|
8
14
|
class Feature
|
|
9
|
-
|
|
15
|
+
TRANSITIONS_KEPT = 50
|
|
16
|
+
|
|
17
|
+
attr_reader :rollout_percentage, :group_matcher, :created_at
|
|
10
18
|
|
|
11
19
|
def initialize(rollout: 0, group: nil)
|
|
12
|
-
@rollout_percentage =
|
|
13
|
-
@group_matcher = group
|
|
14
|
-
@created_at =
|
|
20
|
+
@rollout_percentage = Rollout.validated(rollout)
|
|
21
|
+
@group_matcher = validated_group(group)
|
|
22
|
+
@created_at = StarGates.monotonic_time
|
|
15
23
|
@transition_history = Concurrent::Array.new
|
|
16
24
|
super()
|
|
17
25
|
end
|
|
@@ -153,7 +161,7 @@ module StarGates
|
|
|
153
161
|
status_name: status_name,
|
|
154
162
|
rollout_percentage: rollout_percentage,
|
|
155
163
|
group_matcher: group_matcher_info,
|
|
156
|
-
created_at:
|
|
164
|
+
created_at: created_at,
|
|
157
165
|
state_duration: state_duration,
|
|
158
166
|
total_transitions: transition_count
|
|
159
167
|
}
|
|
@@ -170,7 +178,8 @@ module StarGates
|
|
|
170
178
|
}
|
|
171
179
|
end
|
|
172
180
|
|
|
173
|
-
# Human-readable feature status summary
|
|
181
|
+
# Human-readable feature status summary. `status` is always one of the
|
|
182
|
+
# state machine's three states, so there is no other branch.
|
|
174
183
|
def summary
|
|
175
184
|
case status
|
|
176
185
|
when 'disabled'
|
|
@@ -183,8 +192,6 @@ module StarGates
|
|
|
183
192
|
else
|
|
184
193
|
"Feature '#{feature_name}' is in CANARY mode with #{rollout_percentage}% rollout."
|
|
185
194
|
end
|
|
186
|
-
else
|
|
187
|
-
"Feature '#{feature_name}' is in unknown state: #{status}."
|
|
188
195
|
end
|
|
189
196
|
end
|
|
190
197
|
|
|
@@ -197,7 +204,7 @@ module StarGates
|
|
|
197
204
|
stats: stats,
|
|
198
205
|
configuration: configuration,
|
|
199
206
|
summary: summary,
|
|
200
|
-
introspected_at:
|
|
207
|
+
introspected_at: StarGates.monotonic_time
|
|
201
208
|
}
|
|
202
209
|
end
|
|
203
210
|
|
|
@@ -277,22 +284,15 @@ module StarGates
|
|
|
277
284
|
scenarios
|
|
278
285
|
end
|
|
279
286
|
|
|
280
|
-
#
|
|
287
|
+
# The last `limit` transitions, newest first. `at` is the monotonic
|
|
288
|
+
# instant of each, `age` the span since, `occurred_at` the wall-clock
|
|
289
|
+
# instant, Integer nanoseconds.
|
|
281
290
|
def transition_history(limit: 10)
|
|
291
|
+
now = StarGates.monotonic_time
|
|
282
292
|
{
|
|
283
293
|
feature_name: feature_name,
|
|
284
294
|
total_transitions: @transition_history.size,
|
|
285
|
-
transitions: @transition_history.last(limit).reverse.map
|
|
286
|
-
{
|
|
287
|
-
timestamp: transition[:timestamp],
|
|
288
|
-
from_state: transition[:from_state],
|
|
289
|
-
to_state: transition[:to_state],
|
|
290
|
-
event: transition[:event],
|
|
291
|
-
feature_name: transition[:feature_name],
|
|
292
|
-
feature_class: transition[:feature_class],
|
|
293
|
-
duration_since: Time.now - transition[:timestamp]
|
|
294
|
-
}
|
|
295
|
-
end,
|
|
295
|
+
transitions: @transition_history.last(limit).reverse.map { |entry| entry.merge(age: now - entry[:at]) },
|
|
296
296
|
limit: limit
|
|
297
297
|
}
|
|
298
298
|
end
|
|
@@ -315,25 +315,14 @@ module StarGates
|
|
|
315
315
|
{ type: 'symbol', value: group_matcher }
|
|
316
316
|
when Proc
|
|
317
317
|
{ type: 'proc', value: '<Proc>' }
|
|
318
|
-
when nil
|
|
319
|
-
nil
|
|
320
|
-
else
|
|
321
|
-
{ type: group_matcher.class.name, value: group_matcher.to_s }
|
|
322
318
|
end
|
|
323
319
|
end
|
|
324
320
|
|
|
321
|
+
# Integer nanoseconds in the current state, since creation when it never
|
|
322
|
+
# changed.
|
|
325
323
|
def state_duration
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
duration = Time.now - last_transition[:timestamp]
|
|
330
|
-
if duration < 60
|
|
331
|
-
"#{duration.round(1)}s"
|
|
332
|
-
elsif duration < 3600
|
|
333
|
-
"#{(duration / 60).round(1)}m"
|
|
334
|
-
else
|
|
335
|
-
"#{(duration / 3600).round(1)}h"
|
|
336
|
-
end
|
|
324
|
+
since = @transition_history.last&.fetch(:at) || @created_at
|
|
325
|
+
StarGates.monotonic_time - since
|
|
337
326
|
end
|
|
338
327
|
|
|
339
328
|
def state_machine_config
|
|
@@ -364,24 +353,17 @@ module StarGates
|
|
|
364
353
|
end
|
|
365
354
|
end
|
|
366
355
|
|
|
367
|
-
def
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
else 0
|
|
372
|
-
end
|
|
356
|
+
def validated_group(group)
|
|
357
|
+
return group if group.nil? || group.is_a?(Symbol) || group.is_a?(Proc)
|
|
358
|
+
|
|
359
|
+
raise TypeError, "group must be nil, a Symbol or a Proc, got #{group.inspect}"
|
|
373
360
|
end
|
|
374
361
|
|
|
375
362
|
def group_matches?(context)
|
|
376
|
-
return false unless group_matcher
|
|
377
|
-
|
|
378
363
|
case group_matcher
|
|
379
|
-
when Symbol
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
group_matcher.call(context)
|
|
383
|
-
else
|
|
384
|
-
false
|
|
364
|
+
when Symbol then Array(context[:groups]).include?(group_matcher)
|
|
365
|
+
when Proc then group_matcher.call(context) ? true : false
|
|
366
|
+
else false
|
|
385
367
|
end
|
|
386
368
|
end
|
|
387
369
|
|
|
@@ -401,7 +383,8 @@ module StarGates
|
|
|
401
383
|
to_state = transition.to.is_a?(String) ? transition.to.to_sym : transition.to
|
|
402
384
|
|
|
403
385
|
transition_record = {
|
|
404
|
-
|
|
386
|
+
at: StarGates.monotonic_time,
|
|
387
|
+
occurred_at: StarGates.wall_time,
|
|
405
388
|
from_state: from_state,
|
|
406
389
|
to_state: to_state,
|
|
407
390
|
event: transition.event,
|
|
@@ -411,16 +394,9 @@ module StarGates
|
|
|
411
394
|
|
|
412
395
|
@transition_history << transition_record
|
|
413
396
|
|
|
414
|
-
|
|
415
|
-
return unless @transition_history.size > 50
|
|
416
|
-
|
|
417
|
-
@transition_history.replace(@transition_history.last(50))
|
|
418
|
-
end
|
|
419
|
-
end
|
|
397
|
+
return unless @transition_history.size > TRANSITIONS_KEPT
|
|
420
398
|
|
|
421
|
-
|
|
422
|
-
def self.percent(value)
|
|
423
|
-
value
|
|
399
|
+
@transition_history.replace(@transition_history.last(TRANSITIONS_KEPT))
|
|
424
400
|
end
|
|
425
401
|
end
|
|
426
402
|
end
|
data/lib/star_gates/flags.rb
CHANGED
|
@@ -4,7 +4,13 @@ module StarGates
|
|
|
4
4
|
# Boolean flags with environment > configuration > runtime > false precedence.
|
|
5
5
|
# Runtime state uses Feature's lifecycle and is local to this process unless
|
|
6
6
|
# a shared store is configured.
|
|
7
|
+
#
|
|
8
|
+
# A flag name is a String or Symbol of letters, digits, `_` or `-` (case and
|
|
9
|
+
# dashes fold): TypeError for another type, RangeError for another String.
|
|
7
10
|
class Flags
|
|
11
|
+
FIRST_SLOT = 1
|
|
12
|
+
CLAIM_WINNER = 1
|
|
13
|
+
|
|
8
14
|
attr_accessor :configuration
|
|
9
15
|
attr_reader :store
|
|
10
16
|
|
|
@@ -45,9 +51,9 @@ module StarGates
|
|
|
45
51
|
end
|
|
46
52
|
|
|
47
53
|
def remove(name)
|
|
48
|
-
normalized =
|
|
54
|
+
normalized = flag_name(name)
|
|
49
55
|
if @store
|
|
50
|
-
store_remove(normalized)
|
|
56
|
+
store_remove(normalized)
|
|
51
57
|
else
|
|
52
58
|
@mutex.synchronize { @runtime.delete(normalized) }
|
|
53
59
|
end
|
|
@@ -80,14 +86,14 @@ module StarGates
|
|
|
80
86
|
end
|
|
81
87
|
|
|
82
88
|
def debug(name)
|
|
83
|
-
normalized =
|
|
89
|
+
normalized = flag_name(name)
|
|
84
90
|
resolution(normalized, configuration_flags, runtime_value(normalized))
|
|
85
91
|
end
|
|
86
92
|
|
|
87
93
|
def flag_status(name)
|
|
88
|
-
normalized =
|
|
94
|
+
normalized = flag_name(name)
|
|
89
95
|
details = debug(normalized)
|
|
90
|
-
{ name: normalized
|
|
96
|
+
{ name: normalized.to_sym, enabled: details[:enabled], source: details[:source],
|
|
91
97
|
metadata: runtime_metadata(normalized) }
|
|
92
98
|
end
|
|
93
99
|
|
|
@@ -100,14 +106,20 @@ module StarGates
|
|
|
100
106
|
normalized if normalized.match?(/\A[a-z0-9_]+\z/)
|
|
101
107
|
end
|
|
102
108
|
|
|
109
|
+
def flag_name(name)
|
|
110
|
+
unless name.is_a?(String) || name.is_a?(Symbol)
|
|
111
|
+
raise TypeError, "flag name must be a String or Symbol, got #{name.inspect}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
normalize(name) || raise(RangeError, "flag name must be letters, digits, _ or -, got #{name.inspect}")
|
|
115
|
+
end
|
|
116
|
+
|
|
103
117
|
def boolean(value)
|
|
104
118
|
value == true || (value.is_a?(String) && %w[true 1 yes on].include?(value.downcase))
|
|
105
119
|
end
|
|
106
120
|
|
|
107
121
|
def update(name, value, metadata: nil)
|
|
108
|
-
normalized =
|
|
109
|
-
raise ArgumentError, 'flag name must be a nonempty string or symbol' unless normalized
|
|
110
|
-
|
|
122
|
+
normalized = flag_name(name)
|
|
111
123
|
enabled = boolean(value)
|
|
112
124
|
return store_update(normalized, enabled, metadata) if @store
|
|
113
125
|
|
|
@@ -146,40 +158,96 @@ module StarGates
|
|
|
146
158
|
end
|
|
147
159
|
|
|
148
160
|
def store_update(name, enabled, metadata)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
161
|
+
keeping_indexed(name) do
|
|
162
|
+
store_write(flag_key(name), enabled)
|
|
163
|
+
store_write(metadata_key(name), metadata.dup) if metadata
|
|
164
|
+
end
|
|
152
165
|
enabled
|
|
153
166
|
end
|
|
154
167
|
|
|
168
|
+
def keeping_indexed(name)
|
|
169
|
+
index_add(name)
|
|
170
|
+
yield
|
|
171
|
+
index_add(name)
|
|
172
|
+
end
|
|
173
|
+
|
|
155
174
|
def store_remove(name)
|
|
175
|
+
store_delete(index_remove(name))
|
|
156
176
|
store_delete([flag_key(name), metadata_key(name)])
|
|
157
177
|
end
|
|
158
178
|
|
|
159
179
|
def store_reset
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
keys
|
|
163
|
-
store_delete(keys + (slots ? slots + [index_sequence_key] : [index_key]))
|
|
180
|
+
keys = store_names.flat_map { |name| [flag_key(name), metadata_key(name)] }
|
|
181
|
+
store_delete(index_keys)
|
|
182
|
+
store_delete(keys)
|
|
164
183
|
end
|
|
165
184
|
|
|
166
185
|
def index_add(name)
|
|
167
|
-
return
|
|
168
|
-
|
|
186
|
+
return @mutex.synchronize { legacy_index_add(name) } unless sequenced_index?
|
|
187
|
+
|
|
188
|
+
entries = slot_entries
|
|
189
|
+
return if entries.value?(name)
|
|
169
190
|
|
|
170
|
-
|
|
171
|
-
|
|
191
|
+
store_write(index_slot_key(claim_slot(entries)), name)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def index_remove(name)
|
|
195
|
+
unless sequenced_index?
|
|
196
|
+
@mutex.synchronize { legacy_index_remove(name) }
|
|
197
|
+
return []
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
slot = slot_entries.key(name)
|
|
201
|
+
return [] unless slot
|
|
202
|
+
|
|
203
|
+
[index_slot_key(slot), slot_claim_key(slot)]
|
|
172
204
|
end
|
|
173
205
|
|
|
174
206
|
def store_names
|
|
175
|
-
|
|
176
|
-
|
|
207
|
+
sequenced_index? ? slot_entries.values.uniq : legacy_index
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def index_keys
|
|
211
|
+
return [index_key] unless sequenced_index?
|
|
212
|
+
|
|
213
|
+
slot_range.flat_map { |slot| [index_slot_key(slot), slot_claim_key(slot)] } + [index_sequence_key]
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def slot_entries
|
|
217
|
+
slots = slot_range.to_h { |slot| [index_slot_key(slot), slot] }
|
|
218
|
+
store_read(slots.keys).transform_keys { |key| slots.fetch(key) }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def slot_range
|
|
222
|
+
FIRST_SLOT..@store.read(index_sequence_key, raw: true).to_i
|
|
177
223
|
end
|
|
178
224
|
|
|
179
|
-
def
|
|
180
|
-
|
|
225
|
+
def claim_slot(entries)
|
|
226
|
+
reclaimed_slot(entries) || appended_slot
|
|
227
|
+
end
|
|
181
228
|
|
|
182
|
-
|
|
229
|
+
def reclaimed_slot(entries)
|
|
230
|
+
slot_range.find { |slot| !entries.key?(slot) && store_increment(slot_claim_key(slot)) == CLAIM_WINNER }
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def appended_slot
|
|
234
|
+
slot = store_increment(index_sequence_key)
|
|
235
|
+
store_increment(slot_claim_key(slot))
|
|
236
|
+
slot
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# The store's new count. A store whose increment answers anything but an
|
|
240
|
+
# Integer (ActiveSupport::Cache::NullStore answers nil) cannot keep the
|
|
241
|
+
# index: raised before the name or the flag is written.
|
|
242
|
+
def store_increment(key)
|
|
243
|
+
count = @store.increment(key)
|
|
244
|
+
return count if count.is_a?(Integer)
|
|
245
|
+
|
|
246
|
+
raise Error, "#{@store.class} did not increment #{key}"
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def sequenced_index?
|
|
250
|
+
@store.respond_to?(:increment)
|
|
183
251
|
end
|
|
184
252
|
|
|
185
253
|
def legacy_index_add(name)
|
|
@@ -187,6 +255,11 @@ module StarGates
|
|
|
187
255
|
store_write(index_key, names + [name]) unless names.include?(name)
|
|
188
256
|
end
|
|
189
257
|
|
|
258
|
+
def legacy_index_remove(name)
|
|
259
|
+
names = legacy_index
|
|
260
|
+
store_write(index_key, names - [name]) if names.include?(name)
|
|
261
|
+
end
|
|
262
|
+
|
|
190
263
|
def legacy_index
|
|
191
264
|
names = @store.read(index_key)
|
|
192
265
|
names.is_a?(Array) ? names : []
|
|
@@ -241,9 +314,13 @@ module StarGates
|
|
|
241
314
|
"#{@prefix}:name:#{slot}"
|
|
242
315
|
end
|
|
243
316
|
|
|
317
|
+
def slot_claim_key(slot)
|
|
318
|
+
"#{@prefix}:name_claim:#{slot}"
|
|
319
|
+
end
|
|
320
|
+
|
|
244
321
|
def configuration_flags
|
|
245
322
|
values = configuration.is_a?(Proc) ? configuration.call : configuration
|
|
246
|
-
raise
|
|
323
|
+
raise TypeError, 'configuration must be a Hash or a Proc returning a Hash' unless values.is_a?(Hash)
|
|
247
324
|
|
|
248
325
|
values.each_with_object({}) do |(name, value), flags|
|
|
249
326
|
normalized = normalize(name)
|