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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 399927934b2d54eff9c1845dcb90d09985041fadcbde167c05664fccad0fd1c0
4
- data.tar.gz: de20f67881038b0b15db64ceba479793b4bad7ca0863a6dfe8d009d20ed38597
3
+ metadata.gz: 9a3fe73641b898c85b2bcd78bafd6e5c6d4d455ca744101e6fe725e4fecc0d8a
4
+ data.tar.gz: f1bdb83dfa873a2965193b2cc171785fd8dcf78ce0b3cddf4198d7a407661d14
5
5
  SHA512:
6
- metadata.gz: bbd014d2f8fb483facd66a5a03433ba3289321ae370c780aff674b8c67a9533bae093bba8d273606616a8d5f39d859e8ba33cef684c74197d31de4348fda2089
7
- data.tar.gz: e2a6b7ee9c7ce2183cc062dac45451539ea1a0e0446f3248cb69c721480c92cc876b8c8c558cc3d9e98cc5f683e9a4da63cd0b3b4aaccdfdbec19e22660b32b1
6
+ metadata.gz: d88faf515d89d1cbb79d29ec640e96c94135980245219beafb0c2e8557e8584cb8df43330a2c03747bd290c97420bcf404f642ccf18007320298ad113a13b47e
7
+ data.tar.gz: c1d54c6f0e1a26727a3be804c8ef432908f8a5c3faf137b8168964eb04b5aaba67c26e3c0a0db564ce4ece9c2f19373c61439d10be46b11222784a81eb423ce8
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.3.2"
2
+ ".": "0.4.0"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/seuros/star_gates/compare/v0.3.2...v0.4.0) (2026-11-16)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * run on the released fleet cores in Integer nanoseconds
9
+
10
+ ### Features
11
+
12
+ * run on the released fleet cores in Integer nanoseconds ([1c51494](https://github.com/seuros/star_gates/commit/1c514944cfdfcfc58b82c2b77b54afb9a4a22473))
13
+
3
14
  ## [0.3.2](https://github.com/seuros/star_gates/compare/v0.3.1...v0.3.2) (2026-11-08)
4
15
 
5
16
 
data/README.md CHANGED
@@ -1,11 +1,16 @@
1
1
  # StarGates
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/star_gates.svg)](https://badge.fury.io/rb/star_gates)
4
- [![CI](https://github.com/seuros/star_gates/actions/workflows/ci.yml/badge.svg)](https://github.com/seuros/star_gates/actions/workflows/ci.yml)
4
+ [![Test](https://github.com/seuros/star_gates/actions/workflows/test.yml/badge.svg)](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 ChronoMachines for retries.
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. Stores without `increment` fall back to a
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 threshold: 3, within: 1.minute, reset_after: 30.seconds
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. Pass a stable
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,27 +119,55 @@ 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
- When a circuit opens, its feature rolls back from enabled to canary, or from canary
102
- to disabled. Gates use their fallback when the feature is unavailable or execution
103
- fails. Rails duration helpers retain their normal behavior, including fractional
104
- seconds such as `0.5.seconds`.
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
- Pass `retry_config` to a gate's `circuit` declaration:
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
- circuit threshold: 5,
112
- within: 1.minute,
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 and use ChronoMachines.
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
 
@@ -147,7 +196,9 @@ end
147
196
  ```
148
197
 
149
198
  Circuit events include `circuit.auto_rollback`, `circuit.half_open`,
150
- `circuit.recovered`, and `circuit.retry`, prefixed with `star_gates.`.
199
+ `circuit.recovered`, and `circuit.retry` (`next_delay` in nanoseconds), prefixed with
200
+ `star_gates.`. Telemetry adapters take durations in nanoseconds; the Prometheus
201
+ adapter reports seconds, its base unit.
151
202
 
152
203
  ## Examples
153
204
 
@@ -188,7 +239,6 @@ ship.engage_warp('Alpha Centauri', key: 'crew:1', groups: [:pilots])
188
239
  - [Transition history](test/dummy/demos/transition_demo.rb): recorded feature transitions.
189
240
  - [Introspection](test/dummy/demos/introspection_demo.rb): status, analytics, and context simulation.
190
241
  - [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
242
 
193
243
  Run standalone examples from the repository root:
194
244
 
@@ -202,9 +252,15 @@ bundle exec ruby test/dummy/demos/payment_feature.rb
202
252
  bin/setup
203
253
  bundle exec rake test
204
254
  bundle exec rake test:rails
255
+ bundle exec rake rbs
205
256
  dictator lint .
206
257
  ```
207
258
 
259
+ The Gemfile resolves released sibling gems. To test against sibling checkouts, put
260
+ `gem 'breaker_machines', path: '../breaker_machines'` (and the others) in an
261
+ untracked `Gemfile.local`, which the Gemfile evaluates when present, and compile each
262
+ sibling's extension in its own tree first (`bundle exec rake compile`).
263
+
208
264
  Tests run with padded_cell and Minitest 6. Gem tests and Rails integration tests
209
265
  run in separate processes. `bundle exec rake` runs both suites. To select gem tests
210
266
  directly or pass runner options:
@@ -216,6 +272,14 @@ bundle exec rake test TESTOPTS='-j 4 --seed 1234'
216
272
 
217
273
  See [CHANGELOG.md](CHANGELOG.md) for release history.
218
274
 
275
+ ## 📡 Credits
276
+
277
+ **AMS Fowler**, NCC-2010, *Fowler*-class gatekeeper.
278
+
279
+ Named for Martin Fowler's *FeatureToggle* (2010), the note that gave feature
280
+ flags a name and the warning that came with it: every toggle is debt until it
281
+ is removed.
282
+
219
283
  ## License
220
284
 
221
285
  [BSD-3-Clause](LICENSE.txt).
data/Rakefile CHANGED
@@ -15,4 +15,9 @@ namespace :test do
15
15
  end
16
16
  end
17
17
 
18
- task default: [:test, 'test:rails']
18
+ desc 'Validate RBS signatures'
19
+ task :rbs do
20
+ sh 'rbs', '-I', 'sig', 'validate'
21
+ end
22
+
23
+ task default: [:test, 'test:rails', :rbs]
@@ -2,6 +2,8 @@
2
2
 
3
3
  module StarGates
4
4
  module Adapters
5
+ # Storage, caching and messaging port. `expires_in:` is Integer
6
+ # nanoseconds; an adapter without expiry ignores it.
5
7
  class BaseAdapter
6
8
  def initialize(config = {})
7
9
  @config = config
@@ -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] || 3600 # 1 hour
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?(prefixed_key(key)).positive?
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
- serialized = serialize(value)
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