The rate limit request path turns aDocumentation Index
Fetch the complete documentation index at: https://engineering.unkey.com/llms.txt
Use this file to discover all available pages before exploring further.
(workspace, namespace, identifier, duration) tuple into a sliding-window decision. The normal path is local-memory first and only reaches regional state when a counter is cold or strict mode is active.
Single request flow
Each request builds two counter keys: the current fixed window cell and the previous fixed window cell. The previous cell is needed because the public behavior is a sliding window. The compare-and-swap loop protects the local counter from concurrent accepted requests in the same process. If another goroutine changes the current counter between the read and the commit, the request recomputes the effective count before trying again.Sliding-window math
The implementation stores fixed window cells, but the decision behaves like a sliding window. For a request at timet, the current sequence is:
1 at the beginning of a new fixed window and moves toward 0 as the current fixed window advances.
Counter entries
counterEntry is the in-memory state for one window cell. It is intentionally small because it sits on the request path.
| State | Purpose |
|---|---|
| Regional count | This region’s count for the window cell |
| Speculative count | In-flight RatelimitMany increments that may roll back |
| Hydration state | Cold-start coordination for regional origin reads |
| Global count | Imported count from other regions |
| Global push threshold | Minimum regional count worth sharing globally |
| Last pushed count | Last regional count published for global convergence |
Batch requests
RatelimitMany evaluates multiple limits with all-or-nothing semantics. The method temporarily increments each requested counter, evaluates the full batch, then either keeps every increment or rolls every increment back.
Global publishing reads regional count minus speculative count. That prevents temporary batch state from leaking into cross-region convergence before the batch is committed.
