star_gates 0.3.2 → 0.5.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 +22 -0
- data/README.md +93 -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 -67
- data/sig/star_gates.rbs +832 -0
- metadata +21 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a002dcc04a225a907c9eaf817027c94df7d26e3fd8ed88128f0393955fe14d52
|
|
4
|
+
data.tar.gz: 5811c785b94f131d354ad46a34d3b08e050490715bac1a3c680a6ebd91a746a8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca56eaee492593abe680bc922bb622bfa81eede062bc384ffe6a61ee2fb7ff639781e09a75fc7b2df1edce701fe555933f28f23f918f7f9de65147a4500f8bc6
|
|
7
|
+
data.tar.gz: 4bc33b4e810054029a150ca0cc63f4f3ed1d8a02bd6c863266a2138a1035eaaa2e9938211d86ec717df802b479627350beedbc68d24a9eb471b00aa3637b8d60
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.0](https://github.com/seuros/star_gates/compare/v0.4.0...v0.5.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* code that relied on `require 'star_gates'` to load Rails must require Rails itself.
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* require 'star_gates' no longer loads Rails ([188ffee](https://github.com/seuros/star_gates/commit/188ffeef2bc8c06f9781c5161a63a958f2244e66))
|
|
13
|
+
|
|
14
|
+
## [0.4.0](https://github.com/seuros/star_gates/compare/v0.3.2...v0.4.0) (2026-11-16)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### ⚠ BREAKING CHANGES
|
|
18
|
+
|
|
19
|
+
* run on the released fleet cores in Integer nanoseconds
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* run on the released fleet cores in Integer nanoseconds ([1c51494](https://github.com/seuros/star_gates/commit/1c514944cfdfcfc58b82c2b77b54afb9a4a22473))
|
|
24
|
+
|
|
3
25
|
## [0.3.2](https://github.com/seuros/star_gates/compare/v0.3.1...v0.3.2) (2026-11-08)
|
|
4
26
|
|
|
5
27
|
|
data/README.md
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
# StarGates
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/rb/star_gates)
|
|
4
|
-
[](https://github.com/seuros/star_gates/actions/workflows/test.yml)
|
|
5
5
|
|
|
6
6
|
Feature flags, gradual rollouts, and circuit-protected execution for Ruby.
|
|
7
7
|
StarGates uses StateMachines for feature transitions, BreakerMachines for circuit
|
|
8
|
-
protection, and
|
|
8
|
+
protection, ChronoMachines for retries, and DeadlineMachines for time budgets.
|
|
9
|
+
|
|
10
|
+
Time is Integer nanoseconds throughout: timeouts, cooldowns, backoffs, execution
|
|
11
|
+
times, and every reported instant (`StarGates.monotonic_time`; event-sourced events
|
|
12
|
+
use wall-clock nanoseconds, `StarGates.wall_time`). Wrong types raise `TypeError`,
|
|
13
|
+
values out of range `RangeError`.
|
|
9
14
|
|
|
10
15
|
## Installation
|
|
11
16
|
|
|
@@ -34,7 +39,9 @@ StarGates.flags.reset!
|
|
|
34
39
|
|
|
35
40
|
Precedence: nonblank `FEATURE_<NAME>` environment variable, boolean configuration,
|
|
36
41
|
runtime override, then `false`. Names are case-insensitive; dashes become underscores.
|
|
37
|
-
Truthy strings are `true`, `1`, `yes`, and `on` (case-insensitive).
|
|
42
|
+
Truthy strings are `true`, `1`, `yes`, and `on` (case-insensitive). A flag name is a
|
|
43
|
+
String or Symbol of letters, digits, `_`, or `-`: another type raises `TypeError`, another
|
|
44
|
+
String `RangeError`.
|
|
38
45
|
|
|
39
46
|
For Rails, keep defaults explicit and resolve them lazily:
|
|
40
47
|
|
|
@@ -61,7 +68,9 @@ blindly; `prefix` (default `star_gates:flags`) namespaces them. A failed write r
|
|
|
61
68
|
`StarGates::Error`, and a value the store cannot return counts as no runtime override.
|
|
62
69
|
Listing (`all`, `enabled_flags`, `summary`) and `reset!` use a shared name index. Each new
|
|
63
70
|
name takes its own slot from an atomic `increment`, so workers adding flags at the same
|
|
64
|
-
moment never drop each other's names
|
|
71
|
+
moment never drop each other's names; an `increment` that answers no Integer
|
|
72
|
+
(`NullStore` answers nil) raises `StarGates::Error` before anything is written. Stores
|
|
73
|
+
without `increment` fall back to a
|
|
65
74
|
read-modify-write list, where a concurrent addition can drop a name from listings until
|
|
66
75
|
that flag next changes; lookups by name are unaffected either way. `reset!` and `remove`
|
|
67
76
|
clear runtime state and metadata, not environment/configuration. Switching `store` drops
|
|
@@ -71,10 +80,18 @@ process-local runtime state.
|
|
|
71
80
|
## Gates and rollouts
|
|
72
81
|
|
|
73
82
|
```ruby
|
|
83
|
+
NS_PER_SECOND = 1_000_000_000
|
|
84
|
+
|
|
85
|
+
# Open after 3 failures within a minute; probe again after 30 seconds.
|
|
86
|
+
PAYMENTS_POLICY = BreakerMachines::Policy.new(
|
|
87
|
+
BreakerMachines::Threshold.new(3, nil, 0), 60 * NS_PER_SECOND,
|
|
88
|
+
BreakerMachines::Recovery.new(30 * NS_PER_SECOND, 0.0, 1, 1), nil
|
|
89
|
+
)
|
|
90
|
+
|
|
74
91
|
StarGates.define do
|
|
75
92
|
gate :payments do
|
|
76
93
|
feature rollout: 25.percent
|
|
77
|
-
circuit
|
|
94
|
+
circuit PAYMENTS_POLICY
|
|
78
95
|
|
|
79
96
|
run { |context| PaymentAPI.charge(context[:amount]) }
|
|
80
97
|
fallback { |context| LegacyPaymentAPI.charge(context[:amount]) }
|
|
@@ -87,7 +104,11 @@ gate.call(key: 'account:42', amount: 100)
|
|
|
87
104
|
```
|
|
88
105
|
|
|
89
106
|
Features start disabled. Use `rollout_canary` for percentage/group targeting,
|
|
90
|
-
`enable` for full rollout, and `disable` to turn a feature off.
|
|
107
|
+
`enable` for full rollout, and `disable` to turn a feature off. An event
|
|
108
|
+
answers the performed transition, or `false` when refused, with the reason in
|
|
109
|
+
`<event>_refusal` (`gate.feature.disable_refusal # => :no_transition`); the
|
|
110
|
+
bang forms (`enable!`, ...) raise `StateMachines::InvalidTransition` carrying
|
|
111
|
+
it in `#reason`. Pass a stable
|
|
91
112
|
string or integer `key:` for percentage targeting, such as an account, resident,
|
|
92
113
|
or device identifier.
|
|
93
114
|
|
|
@@ -98,30 +119,65 @@ For group targeting, pass `groups: [:beta]` to match `feature group: :beta`.
|
|
|
98
119
|
A predicate such as `feature group: ->(context) { context[:region] == 'eu' }`
|
|
99
120
|
receives the full context passed to the gate.
|
|
100
121
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
122
|
+
A rollout is an Integer percentage, 0..100; a group is a Symbol or a Proc.
|
|
123
|
+
|
|
124
|
+
`circuit` takes a `BreakerMachines::Policy` and an optional `StarGates::RetryPolicy`,
|
|
125
|
+
positionally, as `StarGates::Circuit.new(policy, retry_policy, feature:, auto_rollback:)`
|
|
126
|
+
does; `feature:` and `auto_rollback:` are runtime-only. When a circuit opens, its
|
|
127
|
+
feature rolls back from enabled to canary, or from canary to disabled.
|
|
128
|
+
|
|
129
|
+
Gates use their fallback when the primary is unavailable. Without a fallback, `call`
|
|
130
|
+
raises `StarGates::UnavailableError`, whose `refusal` says why: `:ineligible` (the
|
|
131
|
+
feature is off for this context), `:failed` (the primary raised; it is the `cause`),
|
|
132
|
+
`:exceeded` (the time budget ran out), or the circuit's own refusal (`:open`, `:busy`,
|
|
133
|
+
`:full`).
|
|
105
134
|
|
|
106
135
|
### Retries
|
|
107
136
|
|
|
108
|
-
|
|
137
|
+
A `RetryPolicy` is a ChronoMachines strategy and an attempt budget that counts the
|
|
138
|
+
first try, in `ChronoMachines::Retry`'s order:
|
|
109
139
|
|
|
110
140
|
```ruby
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
retry_config: {
|
|
114
|
-
max_attempts: 3,
|
|
115
|
-
base_delay: 0.1,
|
|
116
|
-
multiplier: 2,
|
|
117
|
-
max_delay: 5
|
|
118
|
-
}
|
|
141
|
+
backoff = ChronoMachines::Exponential.new(100_000_000, 2.0, 5 * NS_PER_SECOND, 1.0)
|
|
142
|
+
circuit PAYMENTS_POLICY, StarGates::RetryPolicy.new(backoff, 3)
|
|
119
143
|
```
|
|
120
144
|
|
|
121
|
-
Retries are opt-in
|
|
145
|
+
Retries are opt-in. A call the circuit refuses is never retried: the refusal is the
|
|
146
|
+
answer. Exhausted retries re-raise the last exception.
|
|
147
|
+
|
|
148
|
+
### Timeouts
|
|
149
|
+
|
|
150
|
+
`ApplicationStarGate` features take `timeout:` in Integer nanoseconds, narrowed by
|
|
151
|
+
the ambient `DeadlineMachines` budget. A spent budget does not run the block.
|
|
152
|
+
|
|
153
|
+
### Event sourcing
|
|
154
|
+
|
|
155
|
+
`StarGates.configure_foundation` wires an event store, repository, and projection.
|
|
156
|
+
Commands answer the aggregate or a refusal symbol and change nothing:
|
|
157
|
+
`:not_found`, `:invalid_transition`, `:stale_state` (the feature is not in the
|
|
158
|
+
`from_state` given), `:version_conflict` (a concurrent writer saved first).
|
|
159
|
+
States and event names are Symbols; a String is a `TypeError` and changes nothing.
|
|
160
|
+
The projection reads the same through any adapter (Symbol keys and state names, even
|
|
161
|
+
from Redis's JSON). `StarGates.recent_transitions` answers
|
|
162
|
+
`{ transitions:, total_found:, limit:, generated_at: }` with event sourcing on or off,
|
|
163
|
+
and every transition is `{ feature:, from:, to:, event:, occurred_at: }` in both
|
|
164
|
+
modes: `feature` the String feature id (the gate name without event sourcing),
|
|
165
|
+
`from`, `to` and `event` Symbols, `occurred_at` wall-clock Integer nanoseconds.
|
|
166
|
+
|
|
167
|
+
With jsonrpc-rails, include `StarGates::JsonrpcRailsAdapter` in a controller and route
|
|
168
|
+
to its `handle_jsonrpc` action (`post '/jsonrpc', to: 'features#handle_jsonrpc'`). The
|
|
169
|
+
adapter defines no method Rails' controllers already have, so `process` and the rest of
|
|
170
|
+
the dispatch stay Rails'.
|
|
122
171
|
|
|
123
172
|
## Rails
|
|
124
173
|
|
|
174
|
+
`require 'star_gates'` never loads Rails. Inside an application that already loaded
|
|
175
|
+
it (Bundler.require runs after `require 'rails'`), `StarGates.configuration` takes its
|
|
176
|
+
environment from `Rails.env` and `StarGates.load_config!` reads
|
|
177
|
+
`Rails.root/config/star_gates.yml`; both are checked when first used, so there is no
|
|
178
|
+
Railtie to require. Outside Rails they fall back to `RACK_ENV`/`RAILS_ENV`/`RUBY_ENV`
|
|
179
|
+
and the working directory.
|
|
180
|
+
|
|
125
181
|
Define gates in an initializer and call them from controllers or services:
|
|
126
182
|
|
|
127
183
|
```ruby
|
|
@@ -147,7 +203,9 @@ end
|
|
|
147
203
|
```
|
|
148
204
|
|
|
149
205
|
Circuit events include `circuit.auto_rollback`, `circuit.half_open`,
|
|
150
|
-
`circuit.recovered`, and `circuit.retry
|
|
206
|
+
`circuit.recovered`, and `circuit.retry` (`next_delay` in nanoseconds), prefixed with
|
|
207
|
+
`star_gates.`. Telemetry adapters take durations in nanoseconds; the Prometheus
|
|
208
|
+
adapter reports seconds, its base unit.
|
|
151
209
|
|
|
152
210
|
## Examples
|
|
153
211
|
|
|
@@ -188,7 +246,6 @@ ship.engage_warp('Alpha Centauri', key: 'crew:1', groups: [:pilots])
|
|
|
188
246
|
- [Transition history](test/dummy/demos/transition_demo.rb): recorded feature transitions.
|
|
189
247
|
- [Introspection](test/dummy/demos/introspection_demo.rb): status, analytics, and context simulation.
|
|
190
248
|
- [Application payment feature](test/dummy/demos/application_payment_feature.rb): application-level features.
|
|
191
|
-
- [JSON-RPC controller](test/dummy/demos/jsonrpc_rails_controller.rb): optional JSON-RPC integration.
|
|
192
249
|
|
|
193
250
|
Run standalone examples from the repository root:
|
|
194
251
|
|
|
@@ -202,9 +259,15 @@ bundle exec ruby test/dummy/demos/payment_feature.rb
|
|
|
202
259
|
bin/setup
|
|
203
260
|
bundle exec rake test
|
|
204
261
|
bundle exec rake test:rails
|
|
262
|
+
bundle exec rake rbs
|
|
205
263
|
dictator lint .
|
|
206
264
|
```
|
|
207
265
|
|
|
266
|
+
The Gemfile resolves released sibling gems. To test against sibling checkouts, put
|
|
267
|
+
`gem 'breaker_machines', path: '../breaker_machines'` (and the others) in an
|
|
268
|
+
untracked `Gemfile.local`, which the Gemfile evaluates when present, and compile each
|
|
269
|
+
sibling's extension in its own tree first (`bundle exec rake compile`).
|
|
270
|
+
|
|
208
271
|
Tests run with padded_cell and Minitest 6. Gem tests and Rails integration tests
|
|
209
272
|
run in separate processes. `bundle exec rake` runs both suites. To select gem tests
|
|
210
273
|
directly or pass runner options:
|
|
@@ -216,6 +279,14 @@ bundle exec rake test TESTOPTS='-j 4 --seed 1234'
|
|
|
216
279
|
|
|
217
280
|
See [CHANGELOG.md](CHANGELOG.md) for release history.
|
|
218
281
|
|
|
282
|
+
## 📡 Credits
|
|
283
|
+
|
|
284
|
+
**AMS Fowler**, NCC-2010, *Fowler*-class gatekeeper.
|
|
285
|
+
|
|
286
|
+
Named for Martin Fowler's *FeatureToggle* (2010), the note that gave feature
|
|
287
|
+
flags a name and the warning that came with it: every toggle is debt until it
|
|
288
|
+
is removed.
|
|
289
|
+
|
|
219
290
|
## License
|
|
220
291
|
|
|
221
292
|
[BSD-3-Clause](LICENSE.txt).
|
data/Rakefile
CHANGED
|
@@ -4,12 +4,18 @@ require_relative 'base_adapter'
|
|
|
4
4
|
|
|
5
5
|
module StarGates
|
|
6
6
|
module Adapters
|
|
7
|
+
# TTLs (`default_ttl`, `expires_in:`) are Integer nanoseconds, rounded up
|
|
8
|
+
# to Redis's milliseconds at the boundary.
|
|
7
9
|
class RedisAdapter < BaseAdapter
|
|
10
|
+
NS_PER_MS = 1_000_000
|
|
11
|
+
DEFAULT_TTL = 3600 * 1_000_000_000
|
|
12
|
+
private_constant :NS_PER_MS, :DEFAULT_TTL
|
|
13
|
+
|
|
8
14
|
def initialize(config = {})
|
|
9
15
|
super
|
|
10
16
|
@redis = config[:redis] || redis_from_config
|
|
11
17
|
@key_prefix = config[:key_prefix] || 'star_gates'
|
|
12
|
-
@default_ttl = config[:default_ttl] ||
|
|
18
|
+
@default_ttl = config[:default_ttl] || DEFAULT_TTL
|
|
13
19
|
end
|
|
14
20
|
|
|
15
21
|
def store(key, value)
|
|
@@ -27,7 +33,7 @@ module StarGates
|
|
|
27
33
|
end
|
|
28
34
|
|
|
29
35
|
def exists?(key)
|
|
30
|
-
@redis.exists
|
|
36
|
+
@redis.exists(prefixed_key(key)).positive?
|
|
31
37
|
end
|
|
32
38
|
|
|
33
39
|
def cache_fetch(key, expires_in: nil, &block)
|
|
@@ -38,19 +44,13 @@ module StarGates
|
|
|
38
44
|
deserialize(cached)
|
|
39
45
|
elsif block_given?
|
|
40
46
|
value = block.call
|
|
41
|
-
if value
|
|
42
|
-
serialized = serialize(value)
|
|
43
|
-
ttl = expires_in || @default_ttl
|
|
44
|
-
@redis.setex(prefixed, ttl, serialized)
|
|
45
|
-
end
|
|
47
|
+
write_expiring(prefixed, value, expires_in) if value
|
|
46
48
|
value
|
|
47
49
|
end
|
|
48
50
|
end
|
|
49
51
|
|
|
50
52
|
def cache_write(key, value, expires_in: nil)
|
|
51
|
-
|
|
52
|
-
ttl = expires_in || @default_ttl
|
|
53
|
-
@redis.setex(prefixed_key(key), ttl, serialized)
|
|
53
|
+
write_expiring(prefixed_key(key), value, expires_in)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
def cache_delete(key)
|
|
@@ -79,6 +79,11 @@ module StarGates
|
|
|
79
79
|
|
|
80
80
|
private
|
|
81
81
|
|
|
82
|
+
def write_expiring(key, value, expires_in)
|
|
83
|
+
ttl = expires_in || @default_ttl
|
|
84
|
+
@redis.set(key, serialize(value), px: (ttl + NS_PER_MS - 1) / NS_PER_MS)
|
|
85
|
+
end
|
|
86
|
+
|
|
82
87
|
def prefixed_key(key)
|
|
83
88
|
"#{@key_prefix}:#{key}"
|
|
84
89
|
end
|