Commit d03d796
feat: support live reloading of individual rate limits (#2469)
Move API limiter setup out of internal/api and into a dedicated
internal/api/apilimiter package, then wire it into serve-time config
reloads so rate limit changes are picked up without restarting the
service.
This change replaces the old LimiterOptions type with an
apilimiter.Limiter instance passed through api.WithLimiter(...). API
construction now defaults to apilimiter.New(...) when no limiter is
injected, and tests are updated to use the new option-based wiring.
The new apilimiter package centralizes:
- construction of all ratelimit and tollbooth limiters
- mapping between config/env vars and limiter fields
- copy/update logic for reusing existing limiter state where possible
- structured logging for limiter changes during config reload
On config reload in serve():
- keep track of the previously active limiter set
- call previousLim.Update(...) against the latest config
- build the new API with the updated limiter set
- store the new API, reload apiworker config, and retain the latest
limiter for the next reload cycle
This fixes the prior behavior where hot config reload rebuilt the API
but kept stale limiter settings, meaning rate-limit changes were not
applied until process restart.
Additional ratelimit changes:
- persist the original parsed conf.Rate value in conf.Rate via val
- add GetRateValue() for logging/comparison purposes
- extend ratelimit.Limiter with Config() so limiters can expose their
backing configuration
- add ratelimit.Equal(...) helper to compare limiters, configs, and rate
strings consistently
- store conf.Rate on BurstLimiter and IntervalLimiter and expose
Config()
- add String() methods to identify limiter type in tests/debug output
- rename IntervalLimiter.limit to events for clarity
Behavioral note:
- BurstLimiter documentation now matches implementation for non-positive
event counts: burst size becomes 0, so no events are allowed
Tests:
- update API tests to inject limiters through api.WithLimiter
- update options tests to validate apilimiter.New
- expand ratelimit tests to cover type identification and equality
semantics
- add dedicated apilimiter tests that verify only the expected fields
change when each config/env-backed limiter value is modified
---------
Co-authored-by: Chris Stockton <chris.stockton@supabase.io>
Co-authored-by: fadymak <dev@fadymak.com>1 parent e49a3e5 commit d03d796
13 files changed
Lines changed: 826 additions & 154 deletions
File tree
- cmd
- internal
- api
- apilimiter
- conf
- ratelimit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
66 | | - | |
| 67 | + | |
67 | 68 | | |
68 | 69 | | |
69 | | - | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| 134 | + | |
133 | 135 | | |
134 | 136 | | |
135 | 137 | | |
136 | | - | |
137 | | - | |
| 138 | + | |
| 139 | + | |
138 | 140 | | |
139 | 141 | | |
140 | 142 | | |
| |||
146 | 148 | | |
147 | 149 | | |
148 | 150 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
| 151 | + | |
154 | 152 | | |
| 153 | + | |
| 154 | + | |
155 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
156 | 162 | | |
157 | 163 | | |
158 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
113 | | - | |
| 114 | + | |
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
63 | | - | |
64 | | - | |
| 64 | + | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
0 commit comments