Skip to content

feat: add RateLimitLayer middleware#566

Closed
Linuxdazhao wants to merge 2 commits into
64bit:mainfrom
Linuxdazhao:feat/rate-limit-layer
Closed

feat: add RateLimitLayer middleware#566
Linuxdazhao wants to merge 2 commits into
64bit:mainfrom
Linuxdazhao:feat/rate-limit-layer

Conversation

@Linuxdazhao

Copy link
Copy Markdown
Contributor

接着 #320 的讨论,我把 RPM 限流层做了出来。

放在 retry 下面,governor 做 RPM 限流,同时从 x-ratelimit-remaining-requests 和 x-ratelimit-reset-requests 头读服务端的限流状态。remaining 到 0 的时候 poll_ready 会卡住等到 reset 时间再放行,避免发多余的请求。governor 桶和背压状态在 clone 之间共享,retry 的时候也走同一个桶。

native 上用 tokio::sleep 做等待,sleep future 存在 service 里防止 waker 丢掉;wasm 上只走 governor 本地计数,不做延迟。

TPM 限流需要从响应体提取 token 用量,后面单独做。

开了 rate-limit feature 才生效,不影响现有用户。13 个单测覆盖了 header 解析、背压状态、waker 注册、governor 桶共享、重试穿过限流层这些情况。

Linuxdazhao and others added 2 commits June 6, 2026 17:12
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Uses governor for RPM limiting, placed below the retry layer so retries
are also throttled. Reads x-ratelimit-remaining-requests and
x-ratelimit-reset-requests headers to apply backpressure when the server
quota is exhausted. Gated behind the rate-limit feature.
@Linuxdazhao

Copy link
Copy Markdown
Contributor Author

close, 有文件需要清理

@Linuxdazhao Linuxdazhao deleted the feat/rate-limit-layer branch June 30, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant