star_gates 0.3.2 → 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 +11 -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 +81 -118
- 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 +31 -12
- data/lib/star_gates/gate.rb +35 -34
- 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 +54 -58
- data/sig/star_gates.rbs +832 -0
- metadata +21 -17
|
@@ -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,6 +4,9 @@ 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
|
|
8
11
|
FIRST_SLOT = 1
|
|
9
12
|
CLAIM_WINNER = 1
|
|
@@ -48,9 +51,9 @@ module StarGates
|
|
|
48
51
|
end
|
|
49
52
|
|
|
50
53
|
def remove(name)
|
|
51
|
-
normalized =
|
|
54
|
+
normalized = flag_name(name)
|
|
52
55
|
if @store
|
|
53
|
-
store_remove(normalized)
|
|
56
|
+
store_remove(normalized)
|
|
54
57
|
else
|
|
55
58
|
@mutex.synchronize { @runtime.delete(normalized) }
|
|
56
59
|
end
|
|
@@ -83,14 +86,14 @@ module StarGates
|
|
|
83
86
|
end
|
|
84
87
|
|
|
85
88
|
def debug(name)
|
|
86
|
-
normalized =
|
|
89
|
+
normalized = flag_name(name)
|
|
87
90
|
resolution(normalized, configuration_flags, runtime_value(normalized))
|
|
88
91
|
end
|
|
89
92
|
|
|
90
93
|
def flag_status(name)
|
|
91
|
-
normalized =
|
|
94
|
+
normalized = flag_name(name)
|
|
92
95
|
details = debug(normalized)
|
|
93
|
-
{ name: normalized
|
|
96
|
+
{ name: normalized.to_sym, enabled: details[:enabled], source: details[:source],
|
|
94
97
|
metadata: runtime_metadata(normalized) }
|
|
95
98
|
end
|
|
96
99
|
|
|
@@ -103,14 +106,20 @@ module StarGates
|
|
|
103
106
|
normalized if normalized.match?(/\A[a-z0-9_]+\z/)
|
|
104
107
|
end
|
|
105
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
|
+
|
|
106
117
|
def boolean(value)
|
|
107
118
|
value == true || (value.is_a?(String) && %w[true 1 yes on].include?(value.downcase))
|
|
108
119
|
end
|
|
109
120
|
|
|
110
121
|
def update(name, value, metadata: nil)
|
|
111
|
-
normalized =
|
|
112
|
-
raise ArgumentError, 'flag name must be a nonempty string or symbol' unless normalized
|
|
113
|
-
|
|
122
|
+
normalized = flag_name(name)
|
|
114
123
|
enabled = boolean(value)
|
|
115
124
|
return store_update(normalized, enabled, metadata) if @store
|
|
116
125
|
|
|
@@ -218,15 +227,25 @@ module StarGates
|
|
|
218
227
|
end
|
|
219
228
|
|
|
220
229
|
def reclaimed_slot(entries)
|
|
221
|
-
slot_range.find { |slot| !entries.key?(slot) &&
|
|
230
|
+
slot_range.find { |slot| !entries.key?(slot) && store_increment(slot_claim_key(slot)) == CLAIM_WINNER }
|
|
222
231
|
end
|
|
223
232
|
|
|
224
233
|
def appended_slot
|
|
225
|
-
slot =
|
|
226
|
-
|
|
234
|
+
slot = store_increment(index_sequence_key)
|
|
235
|
+
store_increment(slot_claim_key(slot))
|
|
227
236
|
slot
|
|
228
237
|
end
|
|
229
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
|
+
|
|
230
249
|
def sequenced_index?
|
|
231
250
|
@store.respond_to?(:increment)
|
|
232
251
|
end
|
|
@@ -301,7 +320,7 @@ module StarGates
|
|
|
301
320
|
|
|
302
321
|
def configuration_flags
|
|
303
322
|
values = configuration.is_a?(Proc) ? configuration.call : configuration
|
|
304
|
-
raise
|
|
323
|
+
raise TypeError, 'configuration must be a Hash or a Proc returning a Hash' unless values.is_a?(Hash)
|
|
305
324
|
|
|
306
325
|
values.each_with_object({}) do |(name, value), flags|
|
|
307
326
|
normalized = normalize(name)
|
data/lib/star_gates/gate.rb
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module StarGates
|
|
4
|
+
# A named entry point: a feature decides whether the primary path runs, a
|
|
5
|
+
# circuit guards it, and the fallback answers when it does not.
|
|
6
|
+
#
|
|
7
|
+
# The fallback runs when the primary is unavailable: the feature is off for
|
|
8
|
+
# the context, the circuit refuses, the primary raises, or the time budget
|
|
9
|
+
# runs out. Without a fallback, `call` raises UnavailableError whose
|
|
10
|
+
# `refusal` says which, with the primary's exception as its `cause`.
|
|
4
11
|
class Gate
|
|
5
|
-
UNAVAILABLE = Object.new
|
|
6
|
-
private_constant :UNAVAILABLE
|
|
7
|
-
|
|
8
12
|
attr_reader :name, :feature, :circuit, :run_block, :fallback_block
|
|
9
13
|
|
|
10
14
|
def initialize(name, feature: nil, circuit: nil, run_block: nil, fallback_block: nil)
|
|
@@ -16,19 +20,20 @@ module StarGates
|
|
|
16
20
|
end
|
|
17
21
|
|
|
18
22
|
def call(context = {})
|
|
19
|
-
|
|
23
|
+
error = nil
|
|
24
|
+
refusal, result = begin
|
|
20
25
|
execute_primary(context)
|
|
21
|
-
rescue StandardError
|
|
22
|
-
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
error = e
|
|
28
|
+
[refusal_for(e), nil]
|
|
23
29
|
end
|
|
30
|
+
return result unless refusal
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
result
|
|
32
|
+
execute_fallback(context, refusal, error)
|
|
28
33
|
end
|
|
29
34
|
|
|
30
35
|
def enabled_for?(context = {})
|
|
31
|
-
feature_enabled?(context) && !
|
|
36
|
+
feature_enabled?(context) && !open?
|
|
32
37
|
end
|
|
33
38
|
|
|
34
39
|
def open?
|
|
@@ -44,18 +49,19 @@ module StarGates
|
|
|
44
49
|
name: name,
|
|
45
50
|
feature_enabled: feature.nil? || feature.enabled?,
|
|
46
51
|
feature_status: feature.respond_to?(:status) ? feature.status : nil,
|
|
47
|
-
circuit_open:
|
|
48
|
-
|
|
52
|
+
circuit_open: open?,
|
|
53
|
+
last_failure_at: circuit&.last_failure_at
|
|
49
54
|
}
|
|
50
55
|
end
|
|
51
56
|
|
|
52
57
|
private
|
|
53
58
|
|
|
59
|
+
# [nil, result] when the primary ran, [refusal, nil] when it did not.
|
|
54
60
|
def execute_primary(context)
|
|
55
61
|
return execute_feature(context) if feature.respond_to?(:execute)
|
|
56
|
-
return
|
|
62
|
+
return [:ineligible, nil] unless feature_enabled?(context)
|
|
57
63
|
|
|
58
|
-
with_circuit { execute_run_block(context) }
|
|
64
|
+
[nil, with_circuit { execute_run_block(context) }]
|
|
59
65
|
end
|
|
60
66
|
|
|
61
67
|
def execute_feature(context)
|
|
@@ -67,7 +73,15 @@ module StarGates
|
|
|
67
73
|
end
|
|
68
74
|
end
|
|
69
75
|
|
|
70
|
-
executed ? result :
|
|
76
|
+
executed ? [nil, result] : [:ineligible, nil]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def refusal_for(error)
|
|
80
|
+
case error
|
|
81
|
+
when BreakerMachines::CircuitRefusedError then error.refusal
|
|
82
|
+
when UnavailableError then error.refusal
|
|
83
|
+
else :failed
|
|
84
|
+
end
|
|
71
85
|
end
|
|
72
86
|
|
|
73
87
|
def with_circuit(&block)
|
|
@@ -80,32 +94,19 @@ module StarGates
|
|
|
80
94
|
feature.enabled_for?(context)
|
|
81
95
|
end
|
|
82
96
|
|
|
83
|
-
def circuit_open?
|
|
84
|
-
circuit&.open? || false
|
|
85
|
-
end
|
|
86
|
-
|
|
87
97
|
def execute_run_block(context)
|
|
88
98
|
return unless run_block
|
|
89
99
|
|
|
90
|
-
|
|
91
|
-
run_block.call(context)
|
|
92
|
-
else
|
|
93
|
-
run_block.call
|
|
94
|
-
end
|
|
100
|
+
run_block.arity == 1 ? run_block.call(context) : run_block.call
|
|
95
101
|
end
|
|
96
102
|
|
|
97
|
-
def execute_fallback(context)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
fallback_block.call(context)
|
|
102
|
-
else
|
|
103
|
-
fallback_block.call
|
|
103
|
+
def execute_fallback(context, refusal, error)
|
|
104
|
+
unless fallback_block
|
|
105
|
+
raise UnavailableError.new(name, refusal, "Gate #{name} is unavailable (#{refusal}) and has no fallback"),
|
|
106
|
+
cause: error
|
|
104
107
|
end
|
|
105
|
-
end
|
|
106
108
|
|
|
107
|
-
|
|
108
|
-
raise StarGates::UnavailableError, "Gate #{name} is unavailable and no fallback is defined"
|
|
109
|
+
fallback_block.arity == 1 ? fallback_block.call(context) : fallback_block.call
|
|
109
110
|
end
|
|
110
111
|
end
|
|
111
112
|
end
|