throttle_machines 0.4.2-aarch64-linux → 0.5.0-aarch64-linux
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/CHANGELOG.md +231 -0
- data/README.md +89 -14
- data/ext/throttle_machines_native/core/CHANGELOG.md +77 -0
- data/ext/throttle_machines_native/core/README.md +100 -0
- data/ext/throttle_machines_native/extconf.rb +2 -0
- data/ext/throttle_machines_native/ffi/CHANGELOG.md +59 -0
- data/lib/throttle_machines/async_breaker.rb +5 -0
- data/lib/throttle_machines/async_support.rb +16 -21
- data/lib/throttle_machines/breakers.rb +46 -0
- data/lib/throttle_machines/control.rb +25 -46
- data/lib/throttle_machines/controller_helpers.rb +11 -10
- data/lib/throttle_machines/hedged_breaker.rb +4 -6
- data/lib/throttle_machines/hedged_request.rb +29 -42
- data/lib/throttle_machines/instrumentation.rb +10 -75
- data/lib/throttle_machines/limiter.rb +71 -136
- data/lib/throttle_machines/middleware.rb +6 -6
- data/lib/throttle_machines/rack_middleware/allow2_ban.rb +1 -8
- data/lib/throttle_machines/rack_middleware/ban_filter.rb +3 -2
- data/lib/throttle_machines/rack_middleware/configuration.rb +4 -2
- data/lib/throttle_machines/rack_middleware/fail2_ban.rb +5 -21
- data/lib/throttle_machines/rack_middleware/throttle.rb +2 -1
- data/lib/throttle_machines/rack_middleware.rb +3 -1
- data/lib/throttle_machines/storage/base.rb +49 -48
- data/lib/throttle_machines/storage/cache.rb +12 -6
- data/lib/throttle_machines/storage/memory.rb +68 -96
- data/lib/throttle_machines/storage/null.rb +18 -28
- data/lib/throttle_machines/storage/redis/gcra.lua +21 -20
- data/lib/throttle_machines/storage/redis/increment_counter.lua +4 -4
- data/lib/throttle_machines/storage/redis/peek_gcra.lua +16 -16
- data/lib/throttle_machines/storage/redis/peek_token_bucket.lua +22 -18
- data/lib/throttle_machines/storage/redis/token_bucket.lua +31 -19
- data/lib/throttle_machines/storage/redis.rb +63 -69
- data/lib/throttle_machines/throttled_error.rb +4 -1
- data/lib/throttle_machines/version.rb +1 -1
- data/lib/throttle_machines.rb +130 -87
- data/lib/throttle_machines_native/throttle_machines_native.so +0 -0
- data/sig/throttle_machines.rbs +555 -0
- metadata +19 -57
- data/lib/throttle_machines/async_limiter.rb +0 -24
- data/lib/throttle_machines/dependency_error.rb +0 -6
- data/lib/throttle_machines/native_speedup.rb +0 -110
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4bebed33143bfb718a98f4b84152c3a1a08335f29ab5ee10e4fb25b4c97ce7b9
|
|
4
|
+
data.tar.gz: b377e3f94c98f146f96ccc7c8be3b0f7b49d5bfc92e7342d7d7ffac20a1a0435
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af83e692a3def486e24006207f61822fec96e5247f31e2b985df579504fb28bea2509da9c9f340206f072391763ea738966af14b77c4d6bb7b4b4912e596393f
|
|
7
|
+
data.tar.gz: 3a639519422d3c7b5a3c1de392f2204b6ca3fd46505c1e5bda0ce2d5ffe07b98007431e375d48e768ddd88d423b941ff90c80643023e9cebaa8ab3fb6d392dd4
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.5.0](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.4.3...throttle_machines/v0.5.0) (2026-11-16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### ⚠ BREAKING CHANGES
|
|
12
|
+
|
|
13
|
+
* rebuild throttle on the fleet conventions with exact gate counts
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* rebuild throttle on the fleet conventions with exact gate counts ([94d19f3](https://github.com/seuros/throttle_machines/commit/94d19f3abbcbe8c8b05563561e9a68d06ca8d079))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* keep test modules out of the packaged crate ([7f28c6e](https://github.com/seuros/throttle_machines/commit/7f28c6e2a72a9d4a30ec809be8f8a9c9462fa20e))
|
|
23
|
+
|
|
24
|
+
## [0.4.3](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.4.2...throttle_machines/v0.4.3) (2026-11-02)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
* refund a keyed admission ([d3c1aa0](https://github.com/seuros/throttle_machines/commit/d3c1aa0b95797e0574253b767e43c229ddb5de40))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
### Bug Fixes
|
|
33
|
+
|
|
34
|
+
* sweep a keyed shard once an eighth of it could have recovered ([48b6ad9](https://github.com/seuros/throttle_machines/commit/48b6ad996b6ecdcf4b9b7c543d7970d0ffd8291a))
|
|
35
|
+
|
|
36
|
+
## [0.4.2](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.4.1...throttle_machines/v0.4.2) (2026-11-01)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Bug Fixes
|
|
40
|
+
|
|
41
|
+
* give the GC time instead of assuming it ran ([2ab097f](https://github.com/seuros/throttle_machines/commit/2ab097f4f5160da18d760cd0ba6a807071cd866e))
|
|
42
|
+
|
|
43
|
+
## [0.4.1](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.4.0...throttle_machines/v0.4.1) (2026-11-01)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
### Features
|
|
47
|
+
|
|
48
|
+
* gems:push_all fetches the release itself ([0d161ce](https://github.com/seuros/throttle_machines/commit/0d161cedd7983e6ac10b787d072ee9a82b858333))
|
|
49
|
+
* pass a rubygems OTP through GEM_OTP ([6ee50b0](https://github.com/seuros/throttle_machines/commit/6ee50b07703bd123fb902780a2aeae30601c74a5))
|
|
50
|
+
* verify release gem checksums before pushing ([409258c](https://github.com/seuros/throttle_machines/commit/409258cedd8844272eb4fb16f301b141b4c087c7))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
### Bug Fixes
|
|
54
|
+
|
|
55
|
+
* enable the rb-sys compiled fallback so new Rubies can build ([9f1f1b6](https://github.com/seuros/throttle_machines/commit/9f1f1b6289d697ff2f3894820e7826a9ddbd1244))
|
|
56
|
+
* raise the breaker_machines floor to 0.24.0 ([897ff41](https://github.com/seuros/throttle_machines/commit/897ff41e49b0b15fc96b067ceb38ab5f254a514d))
|
|
57
|
+
* raise the chrono_machines floor to 0.12.1 ([e6bfcbe](https://github.com/seuros/throttle_machines/commit/e6bfcbebf222e0b553ef445e4b8bf80de7d7be74))
|
|
58
|
+
|
|
59
|
+
## [0.4.0](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.3.4...throttle_machines/v0.4.0) (2026-11-01)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
### ⚠ BREAKING CHANGES
|
|
63
|
+
|
|
64
|
+
* a Storage::Base subclass implementing gcra or token_bucket must also implement reset_gcra and reset_token_bucket, or supports? reports the algorithm unavailable.
|
|
65
|
+
|
|
66
|
+
### Features
|
|
67
|
+
|
|
68
|
+
* bound the throttle wait by the caller's deadline ([37a7784](https://github.com/seuros/throttle_machines/commit/37a7784ca6b5eed976d5b61ca4b6d0a51502a407))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
### Bug Fixes
|
|
72
|
+
|
|
73
|
+
* a failed gem push fails the release ([6c51c25](https://github.com/seuros/throttle_machines/commit/6c51c25a5f2a424c8d165df9fa04d69960808ef3))
|
|
74
|
+
* a lone checkout can bundle ([5cc6824](https://github.com/seuros/throttle_machines/commit/5cc68244e7f7f3ce3a3dfc665a7e6c907648d667))
|
|
75
|
+
* a reset deletes its own limiter's keys, not a pattern ([b9f93af](https://github.com/seuros/throttle_machines/commit/b9f93af050b3790f881dd7fe15882ec7f6a53f4a))
|
|
76
|
+
* a spent budget raised nothing and ran unbounded ([d58240e](https://github.com/seuros/throttle_machines/commit/d58240ebb675e7ec55b4eac0886d223807eabc51))
|
|
77
|
+
* backoff: selected jitter, never the backoff strategy ([f2f6a2d](https://github.com/seuros/throttle_machines/commit/f2f6a2dcdf776839bc6c1fbbeed18a4384e2fc4a))
|
|
78
|
+
* both backends report a wait as a Float ([525c9a6](https://github.com/seuros/throttle_machines/commit/525c9a6e3791fbe3e93b5c35ac09f77d470c65a5))
|
|
79
|
+
* bound the storage timeout by the caller's budget ([1bef8b1](https://github.com/seuros/throttle_machines/commit/1bef8b1d191a421a0f0bdcb9d4d818851e1360b8))
|
|
80
|
+
* break_circuit shares one circuit, limiters reject a zero rate ([acf5bd8](https://github.com/seuros/throttle_machines/commit/acf5bd8d46b2a655af9b970889e8e47fb5bbf188))
|
|
81
|
+
* **storage:** let a memory store be garbage collected ([e173f89](https://github.com/seuros/throttle_machines/commit/e173f89bf056dbf75a0d514fd7a857bdeddcc50a))
|
|
82
|
+
|
|
83
|
+
## [0.3.4](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.3.3...throttle_machines/v0.3.4) (2026-10-30)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### Features
|
|
87
|
+
|
|
88
|
+
* **storage:** add ActiveSupport::Cache storage ([176c366](https://github.com/seuros/throttle_machines/commit/176c366dc159ff4493482913ddc4110462590344))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
### Bug Fixes
|
|
92
|
+
|
|
93
|
+
* **deps:** require breaker_machines 0.22.1 ([8bbcdda](https://github.com/seuros/throttle_machines/commit/8bbcddaf6459d6905e47401af6b5de73001f6537))
|
|
94
|
+
* **storage:** reject algorithms a storage cannot run when the limiter is built ([a024d23](https://github.com/seuros/throttle_machines/commit/a024d23ab9b24fb93d4f9fa5aa16bf7b56bee253))
|
|
95
|
+
|
|
96
|
+
## [0.3.3](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.3.2...throttle_machines/v0.3.3) (2026-10-27)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
### Bug Fixes
|
|
100
|
+
|
|
101
|
+
* Zeitwerk ignores native_speedup.rb, so apps that eager load (production, CI) boot instead of raising NameError for ThrottleMachines::NativeSpeedup ([#20](https://github.com/seuros/throttle_machines/issues/20)) ([a333434](https://github.com/seuros/throttle_machines/commit/a3334348b9f328d60767bccf317842fde1c27b6e))
|
|
102
|
+
|
|
103
|
+
## [0.3.2](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.3.1...throttle_machines/v0.3.2) (2026-10-20)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
### Bug Fixes
|
|
107
|
+
|
|
108
|
+
* cap in-memory storage across Ruby and native backends ([284387a](https://github.com/seuros/throttle_machines/commit/284387a091383fbbe5e9b02536084fbb7b81838d))
|
|
109
|
+
* **core:** cap keyed GCRA storage under identity sprays ([fb8030a](https://github.com/seuros/throttle_machines/commit/fb8030a73b4dfe03497fd7550f8ee391cca41538))
|
|
110
|
+
|
|
111
|
+
## [0.3.1](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.3.0...throttle_machines/v0.3.1) (2026-10-14)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
### Features
|
|
115
|
+
|
|
116
|
+
* **core:** add KeyedGcra per-key store and GcraParams::from_rate ([b7fa7a5](https://github.com/seuros/throttle_machines/commit/b7fa7a59bcc3dfb276e4fb9be3a95e38c08fe84c))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
### Bug Fixes
|
|
120
|
+
|
|
121
|
+
* admit on-schedule GCRA requests in Ruby and Redis storage ([110b5e3](https://github.com/seuros/throttle_machines/commit/110b5e3d6103180a324e7edeca585958970208f0))
|
|
122
|
+
* **core:** admit a token due now despite float rounding ([c46bd54](https://github.com/seuros/throttle_machines/commit/c46bd54e2d913ecc25c3a9fe8098a8116c1747d6))
|
|
123
|
+
* **core:** admit on-schedule GCRA requests despite float rounding ([5aa090e](https://github.com/seuros/throttle_machines/commit/5aa090e1562325e5d9474f3ec35cf1d22d5ccafb))
|
|
124
|
+
* token bucket rounding and truncated Redis replies ([eae0a9e](https://github.com/seuros/throttle_machines/commit/eae0a9e4a5a42b4f6fc2cc61810471d596df08b5))
|
|
125
|
+
|
|
126
|
+
## [0.3.0](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.2.4...throttle_machines/v0.3.0) (2026-10-12)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
### ⚠ BREAKING CHANGES
|
|
130
|
+
|
|
131
|
+
* Rails 8.0 is no longer supported; activesupport >= 8.1 is required.
|
|
132
|
+
|
|
133
|
+
### Bug Fixes
|
|
134
|
+
|
|
135
|
+
* retain loaded Redis script SHAs ([#16](https://github.com/seuros/throttle_machines/issues/16)) ([75f2ef1](https://github.com/seuros/throttle_machines/commit/75f2ef1a48f8c5e20fec6a96bb61105b96e23d4f))
|
|
136
|
+
* run Redis scripts by SHA with a single EVAL fallback ([6e586e6](https://github.com/seuros/throttle_machines/commit/6e586e6d32782eb95a6b136763b870f36f8da309))
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
### Build System
|
|
140
|
+
|
|
141
|
+
* require Rails 8.1 and run tests with padded_cell ([f131d15](https://github.com/seuros/throttle_machines/commit/f131d15e0445db0758b8cdb96fb3a23573eafa7f))
|
|
142
|
+
|
|
143
|
+
## [0.2.4](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.2.3...throttle_machines/v0.2.4) (2026-10-10)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
### Bug Fixes
|
|
147
|
+
|
|
148
|
+
* **ci:** attach release gems with gh release upload ([dd3ece4](https://github.com/seuros/throttle_machines/commit/dd3ece4a402e8b088b25648c6c786cce03832593))
|
|
149
|
+
* **ci:** use upload-artifact@v7; v8 does not exist ([e535e25](https://github.com/seuros/throttle_machines/commit/e535e255424179a13b7639db82cc691afdf9faf1))
|
|
150
|
+
* **deps:** pin breaker_machines ~> 0.19 ([7621b67](https://github.com/seuros/throttle_machines/commit/7621b6718d9a93696a748464255e3f44d9a50738))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
### Performance Improvements
|
|
154
|
+
|
|
155
|
+
* **ffi:** build Decision/Outcome without a kwargs hash; adopt clippy pedantic and nursery ([23c263d](https://github.com/seuros/throttle_machines/commit/23c263da35829a9375397abaae807659ca96a1ac))
|
|
156
|
+
|
|
157
|
+
## [0.2.3](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.2.2...throttle_machines/v0.2.3) (2026-10-06)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
### Features
|
|
161
|
+
|
|
162
|
+
* require Ruby 4.0, magnus 0.9, Ractor-safe ext with Data results ([448ecd6](https://github.com/seuros/throttle_machines/commit/448ecd6674c5be744cc92b44d368a46552d48161))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
### Bug Fixes
|
|
166
|
+
|
|
167
|
+
* **build:** skip host compile when packaging a cross gem ([2becc31](https://github.com/seuros/throttle_machines/commit/2becc317c51e2f047ee3053a0a179db78c93225e))
|
|
168
|
+
* **ci:** build Rust crates against Ruby 4.0 ([3a69957](https://github.com/seuros/throttle_machines/commit/3a699577ec16ef6655bf6e382012b91bd22a914a))
|
|
169
|
+
* **deps:** pin breaker_machines ~> 0.18, chrono_machines ~> 0.9 ([17e1d60](https://github.com/seuros/throttle_machines/commit/17e1d60a4b19cdf5622093accf2290615f2aaed7))
|
|
170
|
+
|
|
171
|
+
## [0.2.2](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.2.1...throttle_machines/v0.2.2) (2026-06-24)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
### Features
|
|
175
|
+
|
|
176
|
+
* add native circuit breaker on shared Gate contract ([dee0b57](https://github.com/seuros/throttle_machines/commit/dee0b57b20c7c887c2e7f7e7d7d2e6a5696be118))
|
|
177
|
+
|
|
178
|
+
## [0.2.1](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.2.0...throttle_machines/v0.2.1) (2026-06-24)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
### Bug Fixes
|
|
182
|
+
|
|
183
|
+
* add storage-backed async throttling ([09c8125](https://github.com/seuros/throttle_machines/commit/09c8125312dd21fb7a61c469b99faf09764cd75d))
|
|
184
|
+
* add storage-backed async throttling ([12fe6b5](https://github.com/seuros/throttle_machines/commit/12fe6b51631d149f849aeb818f8d6252b5f8eb04))
|
|
185
|
+
|
|
186
|
+
## [0.2.0](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.1.3...throttle_machines/v0.2.0) (2026-05-25)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
### Bug Fixes
|
|
190
|
+
|
|
191
|
+
* build native extension with current rb_sys ([0960f00](https://github.com/seuros/throttle_machines/commit/0960f00ecbb3f55fd888984f7474c591fc0459ed))
|
|
192
|
+
* ship pure ruby fallback gem ([3a54ac3](https://github.com/seuros/throttle_machines/commit/3a54ac3dffb073ece85a10c4c35ed3c426d658f6))
|
|
193
|
+
|
|
194
|
+
## [0.1.3](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.1.2...throttle_machines/v0.1.3) (2026-03-22)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
### Features
|
|
198
|
+
|
|
199
|
+
* add matryoshka pattern with Rust native extension ([97d41a6](https://github.com/seuros/throttle_machines/commit/97d41a6ddf71e042f437c44315c40e3d30c9efe1))
|
|
200
|
+
* add matryoshka pattern with Rust native extension ([3f614cb](https://github.com/seuros/throttle_machines/commit/3f614cbefe8b489f6627b35b635ed954e9e84d4d))
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
### Bug Fixes
|
|
204
|
+
|
|
205
|
+
* bump breaker_machines to ~> 0.10 and upgrade magnus to 0.8.2 ([62b1c20](https://github.com/seuros/throttle_machines/commit/62b1c2007879775eed1d8412caba641c7ef345bd))
|
|
206
|
+
* lower Ruby requirement to 3.3.0 for TruffleRuby compatibility ([2067cc7](https://github.com/seuros/throttle_machines/commit/2067cc775362cb268d2e601aeb8c396449ca1be2))
|
|
207
|
+
* upgrade magnus to 0.8.2 and refresh lock file ([01c6924](https://github.com/seuros/throttle_machines/commit/01c692477c05bd927501b595a4cfb162de968087))
|
|
208
|
+
|
|
209
|
+
## [0.1.2](https://github.com/seuros/throttle_machines/compare/throttle_machines/v0.1.1...throttle_machines/v0.1.2) (2025-11-11)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
### Features
|
|
213
|
+
|
|
214
|
+
* Add Rails 8.1 support and remove ActiveSupport::Configurable deprecation ([#4](https://github.com/seuros/throttle_machines/issues/4)) ([684fe6f](https://github.com/seuros/throttle_machines/commit/684fe6fc0b5a0dd150cc4b08d872c37f46bffdff))
|
|
215
|
+
|
|
216
|
+
## [0.1.1](https://github.com/seuros/throttle_machines/compare/throttle_machines-v0.1.0...throttle_machines/v0.1.1) (2025-08-08)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
### Bug Fixes
|
|
220
|
+
|
|
221
|
+
* extract lua code ([d4821aa](https://github.com/seuros/throttle_machines/commit/d4821aaa411919ef63a6fdc681e977e9b4eec8e4))
|
|
222
|
+
* extract lua code ([aa87d14](https://github.com/seuros/throttle_machines/commit/aa87d146911b760cfef1b76f6d2dd87ad6870458))
|
|
223
|
+
* versioning ([df0a3f8](https://github.com/seuros/throttle_machines/commit/df0a3f89ba027639f99fa6a49cdc10b9ba9dcdea))
|
|
224
|
+
|
|
225
|
+
## [Unreleased]
|
|
226
|
+
|
|
227
|
+
### Added
|
|
228
|
+
- Initial implementation of ThrottleMachines
|
|
229
|
+
- Core rate limiting engine with temporal precision
|
|
230
|
+
- Support for multiple storage backends
|
|
231
|
+
- Elegant API design for Rails integration
|
data/README.md
CHANGED
|
@@ -45,6 +45,20 @@ bundle install
|
|
|
45
45
|
|
|
46
46
|
---
|
|
47
47
|
|
|
48
|
+
## 🕰️ Ship's Clock
|
|
49
|
+
|
|
50
|
+
Every instant and span is **Integer nanoseconds**: periods, windows, TTLs,
|
|
51
|
+
`retry_after`, deadlines. `ThrottleMachines.monotonic_time` reads
|
|
52
|
+
`Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)`. No floats, no
|
|
53
|
+
seconds; HTTP `Retry-After` headers are the one place a wait becomes whole
|
|
54
|
+
seconds, rounded up.
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
NS = ThrottleMachines::NS_PER_SECOND # 1_000_000_000
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
48
62
|
## 🎯 Quick Mission Brief
|
|
49
63
|
|
|
50
64
|
### Basic Throttling - The Photon Torpedo Approach
|
|
@@ -52,8 +66,8 @@ bundle install
|
|
|
52
66
|
```ruby
|
|
53
67
|
# Simple rate limiting - like controlling photon torpedo launches
|
|
54
68
|
torpedo_limiter = ThrottleMachines.limiter("photon_launcher",
|
|
55
|
-
limit: 10,
|
|
56
|
-
period: 60
|
|
69
|
+
limit: 10, # 10 torpedoes
|
|
70
|
+
period: 60 * NS # per minute
|
|
57
71
|
)
|
|
58
72
|
|
|
59
73
|
# Check and consume approach
|
|
@@ -69,7 +83,7 @@ begin
|
|
|
69
83
|
torpedo_limiter.throttle!
|
|
70
84
|
launch_torpedo!
|
|
71
85
|
rescue ThrottleMachines::ThrottledError => e
|
|
72
|
-
puts "Torpedo bay recharging... Retry after #{e.
|
|
86
|
+
puts "Torpedo bay recharging... Retry after #{e.retry_after} ns"
|
|
73
87
|
end
|
|
74
88
|
```
|
|
75
89
|
|
|
@@ -80,7 +94,7 @@ end
|
|
|
80
94
|
# Instead of sudden stops, it gracefully manages flow
|
|
81
95
|
diplomatic_limiter = ThrottleMachines.limiter("federation_embassy",
|
|
82
96
|
limit: 100,
|
|
83
|
-
period: 60,
|
|
97
|
+
period: 60 * NS,
|
|
84
98
|
algorithm: :gcra # Generic Cell Rate Algorithm
|
|
85
99
|
)
|
|
86
100
|
|
|
@@ -92,37 +106,87 @@ diplomatic_limiter = ThrottleMachines.limiter("federation_embassy",
|
|
|
92
106
|
```ruby
|
|
93
107
|
# Use the block form for automatic throttling
|
|
94
108
|
# This handles the throttle! and error handling for you
|
|
95
|
-
ThrottleMachines.limit("warp_drive", limit: 5, period: 60) do
|
|
109
|
+
ThrottleMachines.limit("warp_drive", limit: 5, period: 60 * NS) do
|
|
96
110
|
engage_warp_drive! # This will be throttled automatically
|
|
97
111
|
end
|
|
98
112
|
# Raises ThrottledError if limit exceeded
|
|
99
113
|
```
|
|
100
114
|
|
|
115
|
+
### The Reactor Core - Native Gates 🔩
|
|
116
|
+
|
|
117
|
+
Under every limiter sits a `no_std` Rust core. Its gates are yours to drive
|
|
118
|
+
directly, on a clock you supply. Policies are frozen `Data` values validated
|
|
119
|
+
by the core (`TypeError` for the wrong type, `RangeError` for a value it
|
|
120
|
+
refuses); a gate answers exactly how many more calls it would admit at that
|
|
121
|
+
instant, or a refusal Symbol, which changes nothing: `:throttled`, or for
|
|
122
|
+
GCRA `:overflow` when an admission would push its TAT past the largest
|
|
123
|
+
instant (`(2**64 * 10**9) - 1` nanoseconds; `retry_after` then answers that
|
|
124
|
+
instant: never). A `GcraPolicy` whose burst ceiling holds more than
|
|
125
|
+
`2**64 - 1` emission intervals is a `RangeError`. `now` is read over that
|
|
126
|
+
whole range, so every instant a gate returns (a GCRA `tat` can pass
|
|
127
|
+
`2**64 - 1`) reads back.
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
policy = ThrottleMachines::GcraPolicy.per(1 * NS, 10, 3) # 10/s, bursts of 3
|
|
131
|
+
gate = ThrottleMachines::Gcra.new(policy)
|
|
132
|
+
now = ThrottleMachines.monotonic_time
|
|
133
|
+
|
|
134
|
+
gate.check(now) # => 2, then 1, then 0
|
|
135
|
+
gate.check(now) # => :throttled (after the burst)
|
|
136
|
+
gate.retry_after(now) # => 100_000_000
|
|
137
|
+
gate.peek(now) # non-consuming; gate.refund gives one back
|
|
138
|
+
|
|
139
|
+
bucket = ThrottleMachines::TokenBucket.new(ThrottleMachines::TokenBucketPolicy.new(5, 200_000_000))
|
|
140
|
+
window = ThrottleMachines::FixedWindow.new(ThrottleMachines::FixedWindowPolicy.new(60 * NS, 100))
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
State kept elsewhere resumes with `Gcra.restored(policy, tat)`,
|
|
144
|
+
`TokenBucket.restored(policy, tokens, refilled_at)` and
|
|
145
|
+
`FixedWindow.restored(policy, count, window_start)`.
|
|
146
|
+
|
|
147
|
+
### Retries and Breakers 🛡️
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
backoff = ChronoMachines::Exponential.new(100_000_000, 2.0, 5 * NS, 1.0)
|
|
151
|
+
|
|
152
|
+
ThrottleMachines.control("hyperspace_relay") do
|
|
153
|
+
limit rate: 10, per: 1 * NS
|
|
154
|
+
break_on failures: 5, within: 60 * NS
|
|
155
|
+
retry_on_failure backoff, 3
|
|
156
|
+
end.call { relay.transmit! }
|
|
157
|
+
```
|
|
158
|
+
|
|
101
159
|
---
|
|
102
160
|
|
|
103
161
|
## Bounded in-memory storage
|
|
104
162
|
|
|
105
163
|
The memory backend tracks at most **65536 entries**, shared across fixed-window
|
|
106
|
-
counters, GCRA states, and token buckets
|
|
164
|
+
counters, GCRA states, and token buckets.
|
|
107
165
|
|
|
108
166
|
```ruby
|
|
109
167
|
ThrottleMachines.storage = ThrottleMachines::Storage::Memory.new(max_keys: 10_000)
|
|
110
168
|
```
|
|
111
169
|
|
|
112
170
|
When full, expired entries are reclaimed before admission. If no slot is
|
|
113
|
-
available, new identities are
|
|
171
|
+
available, new identities are refused with `:full`; active identities are never evicted
|
|
114
172
|
to make room and can continue under their existing limits. Peeks also fail
|
|
115
173
|
closed for untrackable identities. Capacity refusals use a one-second retry
|
|
116
174
|
hint, not a reservation. At saturation, on-demand expiry scans run at most
|
|
117
175
|
once per second, even with `auto_cleanup: false`.
|
|
118
176
|
|
|
119
|
-
`max_keys` must be a non-negative
|
|
177
|
+
`max_keys` must be a non-negative Integer; zero denies all identities. For
|
|
120
178
|
direct storage callers, an untrackable fixed-window counter reads/increments
|
|
121
|
-
as `
|
|
179
|
+
as `Storage::Base::UNTRACKED` (2**64) so it cannot pass a rate limit.
|
|
122
180
|
|
|
123
181
|
This bounds entry count, not bytes: also bound untrusted identity lengths.
|
|
124
182
|
Redis storage uses TTLs but has no application-level key cap; configure Redis
|
|
125
183
|
memory limits separately. Evicting active Redis keys can reset their limits.
|
|
184
|
+
Redis scripts read `now` from the server (`TIME`, whole microseconds, turned
|
|
185
|
+
into nanoseconds), so processes on any number of hosts can share one Redis
|
|
186
|
+
store; a step in the Redis host's clock is a step for every limit. Stored
|
|
187
|
+
instants count from a `TIME` second kept beside them and are rebased on each
|
|
188
|
+
write, which keeps the scripts' double arithmetic exact. GCRA state is a hash:
|
|
189
|
+
clear throttle keys written by a release that passed the caller's clock.
|
|
126
190
|
|
|
127
191
|
---
|
|
128
192
|
|
|
@@ -144,7 +208,7 @@ first requests.
|
|
|
144
208
|
|
|
145
209
|
It supports `:fixed_window` only; the cache API has no atomic read-modify-write
|
|
146
210
|
for GCRA or token buckets. A limiter built with an algorithm its storage lacks
|
|
147
|
-
raises `
|
|
211
|
+
raises `RangeError` on construction, for custom backends too.
|
|
148
212
|
|
|
149
213
|
When the store fails to count (an outage, `NullStore`), requests are admitted.
|
|
150
214
|
Pass `fail_closed: true` to throttle them instead. `clear` deletes only this
|
|
@@ -159,19 +223,19 @@ with `Limiter#reset!`.
|
|
|
159
223
|
Like the best spacecraft, ThrottleMachines follows the principle of **ultra-thin design**:
|
|
160
224
|
|
|
161
225
|
- **No bloat** - Every component serves a critical function
|
|
162
|
-
- **
|
|
163
|
-
- **
|
|
226
|
+
- **Rust reactor core** - `no_std`, allocation-free, no clock of its own
|
|
227
|
+
- **Exact time** - Integer nanoseconds end to end; no float drift at the boundaries
|
|
164
228
|
- **Modular systems** - Swap components like ship modules
|
|
165
229
|
|
|
166
230
|
---
|
|
167
231
|
|
|
168
232
|
## ⚡ Warp Factor Features
|
|
169
233
|
|
|
170
|
-
- **🚀 Multiple Algorithms** - GCRA, Token Bucket, Fixed Window
|
|
234
|
+
- **🚀 Multiple Algorithms** - GCRA, Token Bucket, Fixed Window
|
|
171
235
|
- **💫 Distributed Ready** - Redis backend for fleet coordination
|
|
172
236
|
- **🔄 Async Support** - Fiber-safe operations for quantum communications
|
|
173
237
|
- **🏃 Hedged Requests** - Multi-path navigation for reduced latency
|
|
174
|
-
- **🎯
|
|
238
|
+
- **🎯 Nanosecond Precision** - Navigate the cosmos with temporal accuracy
|
|
175
239
|
- **🔌 Pluggable Storage** - Memory crystals or Redis quantum storage
|
|
176
240
|
- **🌍 Rails Integration** - Seamless planetary docking procedures
|
|
177
241
|
- **📡 Rack Middleware** - Universal translator for all spacecraft
|
|
@@ -217,6 +281,17 @@ Found a breach in the hull? Encountered an unknown anomaly?
|
|
|
217
281
|
|
|
218
282
|
---
|
|
219
283
|
|
|
284
|
+
## 📡 Credits
|
|
285
|
+
|
|
286
|
+
**AMS Turner**, NCC-1986, *Turner*-class escort.
|
|
287
|
+
|
|
288
|
+
Named for Jonathan Turner's *New Directions in Communications* (1986), which
|
|
289
|
+
proposed the leaky bucket: let traffic drain at a fixed rate with a bounded
|
|
290
|
+
allowance for bursts. GCRA, the algorithm this gem's middleware and controller
|
|
291
|
+
helpers default to, is that leaky bucket kept as one theoretical arrival time.
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
220
295
|
## 🎖️ Mission Credentials
|
|
221
296
|
|
|
222
297
|
MIT License - See [LICENSE](LICENSE) for full transmission.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.6.0](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.5.0...throttle-machines-v0.6.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* rebuild throttle on the fleet conventions with exact gate counts
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* rebuild throttle on the fleet conventions with exact gate counts ([94d19f3](https://github.com/seuros/throttle_machines/commit/94d19f3abbcbe8c8b05563561e9a68d06ca8d079))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* keep test modules out of the packaged crate ([7f28c6e](https://github.com/seuros/throttle_machines/commit/7f28c6e2a72a9d4a30ec809be8f8a9c9462fa20e))
|
|
18
|
+
|
|
19
|
+
## [0.5.0](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.4.0...throttle-machines-v0.5.0) (2026-11-02)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* refund a keyed admission ([d3c1aa0](https://github.com/seuros/throttle_machines/commit/d3c1aa0b95797e0574253b767e43c229ddb5de40))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* sweep a keyed shard once an eighth of it could have recovered ([48b6ad9](https://github.com/seuros/throttle_machines/commit/48b6ad996b6ecdcf4b9b7c543d7970d0ffd8291a))
|
|
30
|
+
|
|
31
|
+
## [0.4.0](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.3.1...throttle-machines-v0.4.0) (2026-11-01)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
* bound the throttle wait by the caller's deadline ([37a7784](https://github.com/seuros/throttle_machines/commit/37a7784ca6b5eed976d5b61ca4b6d0a51502a407))
|
|
37
|
+
|
|
38
|
+
## [0.3.1](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.3.0...throttle-machines-v0.3.1) (2026-10-20)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Bug Fixes
|
|
42
|
+
|
|
43
|
+
* **core:** cap keyed GCRA storage under identity sprays ([fb8030a](https://github.com/seuros/throttle_machines/commit/fb8030a73b4dfe03497fd7550f8ee391cca41538))
|
|
44
|
+
|
|
45
|
+
## [0.3.0](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.2.1...throttle-machines-v0.3.0) (2026-10-14)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Features
|
|
49
|
+
|
|
50
|
+
* **core:** add KeyedGcra per-key store and GcraParams::from_rate ([b7fa7a5](https://github.com/seuros/throttle_machines/commit/b7fa7a59bcc3dfb276e4fb9be3a95e38c08fe84c))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
### Bug Fixes
|
|
54
|
+
|
|
55
|
+
* **core:** admit a token due now despite float rounding ([c46bd54](https://github.com/seuros/throttle_machines/commit/c46bd54e2d913ecc25c3a9fe8098a8116c1747d6))
|
|
56
|
+
* **core:** admit on-schedule GCRA requests despite float rounding ([5aa090e](https://github.com/seuros/throttle_machines/commit/5aa090e1562325e5d9474f3ec35cf1d22d5ccafb))
|
|
57
|
+
|
|
58
|
+
## [0.2.1](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.2.0...throttle-machines-v0.2.1) (2026-10-10)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
### Performance Improvements
|
|
62
|
+
|
|
63
|
+
* **ffi:** build Decision/Outcome without a kwargs hash; adopt clippy pedantic and nursery ([23c263d](https://github.com/seuros/throttle_machines/commit/23c263da35829a9375397abaae807659ca96a1ac))
|
|
64
|
+
|
|
65
|
+
## [0.2.0](https://github.com/seuros/throttle_machines/compare/throttle-machines-v0.2.0...throttle-machines-v0.2.0) (2026-06-24)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Features
|
|
69
|
+
|
|
70
|
+
* add matryoshka pattern with Rust native extension ([97d41a6](https://github.com/seuros/throttle_machines/commit/97d41a6ddf71e042f437c44315c40e3d30c9efe1))
|
|
71
|
+
* add matryoshka pattern with Rust native extension ([3f614cb](https://github.com/seuros/throttle_machines/commit/3f614cbefe8b489f6627b35b635ed954e9e84d4d))
|
|
72
|
+
* add native circuit breaker on shared Gate contract ([dee0b57](https://github.com/seuros/throttle_machines/commit/dee0b57b20c7c887c2e7f7e7d7d2e6a5696be118))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
### Bug Fixes
|
|
76
|
+
|
|
77
|
+
* ship pure ruby fallback gem ([3a54ac3](https://github.com/seuros/throttle_machines/commit/3a54ac3dffb073ece85a10c4c35ed3c426d658f6))
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# throttle-machines
|
|
2
|
+
|
|
3
|
+
Rate limiting on a clock you supply. `no_std`, allocation-free, no `unsafe`;
|
|
4
|
+
the crate reads no clock and does no I/O. `now` is a `core::time::Duration`
|
|
5
|
+
since an epoch you pick, and so is every span.
|
|
6
|
+
|
|
7
|
+
## Algorithms
|
|
8
|
+
|
|
9
|
+
Each limiter is a small `Copy` value, a validated policy plus the state it
|
|
10
|
+
has reached, behind the shared [`Gate`] contract:
|
|
11
|
+
|
|
12
|
+
- **GCRA** (`Gcra`, `GcraPolicy`): one call per emission interval, with a
|
|
13
|
+
delay tolerance for bursts. Smooth and exact.
|
|
14
|
+
- **Token bucket** (`TokenBucket`, `TokenBucketPolicy`): a burst of
|
|
15
|
+
`capacity`, one token earned every `refill_every`.
|
|
16
|
+
- **Fixed window** (`FixedWindow`, `FixedWindowPolicy`): `limit` calls per
|
|
17
|
+
window.
|
|
18
|
+
|
|
19
|
+
`check(now)` admits one call and answers exactly how many more the gate
|
|
20
|
+
would admit at the same instant, or a `Refusal`, which changes nothing:
|
|
21
|
+
`Throttled`, or for GCRA `Overflow` when the admission would push the TAT
|
|
22
|
+
past `Duration::MAX`. `peek(now)` answers the same without consuming,
|
|
23
|
+
`retry_after(now)` how long until the next admission (`Duration::MAX` when
|
|
24
|
+
no instant a `Duration` holds admits one), and `refund()` gives one
|
|
25
|
+
admission back. Nothing saturates into a count: a window or refill due past
|
|
26
|
+
`Duration::MAX` is never due.
|
|
27
|
+
|
|
28
|
+
Policies are built with `new`, which answers `None` for a value the limiter
|
|
29
|
+
cannot honour (a zero interval, refill or window; a GCRA burst ceiling,
|
|
30
|
+
`delay_tolerance + emission_interval`, past `Duration::MAX` or holding more
|
|
31
|
+
than `u64::MAX` intervals, which no `u64` count could answer), or with `per(period,
|
|
32
|
+
limit, ..)`, which spreads `limit` calls over `period`, rounding the interval
|
|
33
|
+
up to the nanosecond so the rate never exceeds the one asked for.
|
|
34
|
+
|
|
35
|
+
```rust
|
|
36
|
+
use core::num::NonZeroU64;
|
|
37
|
+
use core::time::Duration;
|
|
38
|
+
use throttle_machines::{Gate, Gcra, GcraPolicy, Refusal};
|
|
39
|
+
|
|
40
|
+
// 10 calls per second, bursts of 3.
|
|
41
|
+
let ten = NonZeroU64::new(10).unwrap();
|
|
42
|
+
let policy = GcraPolicy::per(Duration::from_secs(1), ten, NonZeroU64::new(3).unwrap()).unwrap();
|
|
43
|
+
let mut gcra = Gcra::new(policy);
|
|
44
|
+
|
|
45
|
+
let now = Duration::from_secs(60);
|
|
46
|
+
assert_eq!(gcra.check(now), Ok(2));
|
|
47
|
+
assert_eq!(gcra.check(now), Ok(1));
|
|
48
|
+
assert_eq!(gcra.check(now), Ok(0));
|
|
49
|
+
assert_eq!(gcra.check(now), Err(Refusal::Throttled));
|
|
50
|
+
assert_eq!(gcra.retry_after(now), Duration::from_millis(100));
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
A caller that keeps state elsewhere between calls (a database row, a cache
|
|
54
|
+
entry) reads it back with `tat()`, `tokens()`/`refilled_at()` or
|
|
55
|
+
`count()`/`window_start()` and resumes with `restored`.
|
|
56
|
+
|
|
57
|
+
## Per-key limiting
|
|
58
|
+
|
|
59
|
+
The `std` feature adds `KeyedGcra`, which holds a TAT per key in a sharded,
|
|
60
|
+
mutex-guarded map and evicts keys once they have fully recovered.
|
|
61
|
+
|
|
62
|
+
Each shard holds at most `max_keys_per_shard` keys (4096 by default, 65536
|
|
63
|
+
with the default 16 shards). A full shard first reclaims recovered keys, in
|
|
64
|
+
batches: it sweeps once an eighth of it could have recovered, and refuses
|
|
65
|
+
untracked keys with `Refusal::Full` until then without allocating. Tracked
|
|
66
|
+
keys keep their limits; free slots in other shards are not borrowed.
|
|
67
|
+
`evict_at` is a cleanup trigger after admissions, not the cap; `evict(now)`
|
|
68
|
+
reclaims every recovered key whenever it is called. `peek` never reclaims.
|
|
69
|
+
|
|
70
|
+
`refund(key, now)` gives back one admission `check` granted, for calls that
|
|
71
|
+
spend several limiters and must be admitted by all of them or none.
|
|
72
|
+
|
|
73
|
+
The cap bounds entry count, not key length: bound identities at ingress when
|
|
74
|
+
they are attacker-controlled.
|
|
75
|
+
|
|
76
|
+
```rust
|
|
77
|
+
# #[cfg(feature = "std")] {
|
|
78
|
+
use core::num::NonZeroU64;
|
|
79
|
+
use std::time::{Duration, Instant};
|
|
80
|
+
use throttle_machines::{GcraPolicy, KeyedGcra};
|
|
81
|
+
|
|
82
|
+
let ten = NonZeroU64::new(10).unwrap();
|
|
83
|
+
let policy = GcraPolicy::per(Duration::from_secs(1), ten, NonZeroU64::new(3).unwrap()).unwrap();
|
|
84
|
+
let limiter = KeyedGcra::<String>::new(policy);
|
|
85
|
+
|
|
86
|
+
let epoch = Instant::now();
|
|
87
|
+
assert_eq!(limiter.check("203.0.113.7", epoch.elapsed()), Ok(2));
|
|
88
|
+
# }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```toml
|
|
92
|
+
[dependencies]
|
|
93
|
+
throttle-machines = { version = ">=0.6", features = ["std"] }
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT
|
|
99
|
+
|
|
100
|
+
[`Gate`]: https://docs.rs/throttle-machines/latest/throttle_machines/trait.Gate.html
|