Repository navigation
Description
Read This First (Do Not Remove This Section)
- Docs: https://docs.newapi.ai/
- Usage questions first: https://deepwiki.com/QuantumNous/new-api
- Issues about forwarding behavior after enabling pass-through mode are not accepted; pass-through mode forwards requests directly, so please verify
upstream behavior yourself. - Technical support requests such as coding plans or reverse-engineering channels are not accepted as issues.
- Warning: issues with this template removed, section headings deleted, or content cleared may be closed directly. Repeated abusive submissions may result
in a block.
Your current newapi version
v1.0.0-rc.21
Submission Checks
- Non-duplicate issue: I have searched existing Issues and confirmed there are no
similar issues. - Read this first: I have fully read the section above, reviewed the docs at https://docs.newapi.ai/ and the project README, and asked AI first,
confirming this is not a usage, configuration, or integration question. - Template intact: I have not removed any guidance or section headings from this template and will complete it as requested.
- Maintainer time: I understand that maintainers have limited time and issues that do not follow this template may be ignored or closed directly.
查重说明:搜索后发现 #4782 报告了相同问题,但该 issue 因未使用规定模板、缺少版本和 Submission Checks 而被机器人关闭,并被告知可以使用正确的 Bug Report 模板
重新提交。本 issue 是按该要求进行的规范重提。另有相关 PR #2856,但它已关闭且未合并。
Issue Description
启用 Redis 和模型请求限流后,如果 Redis 在 new-api 进程仍然运行期间发生重启、替换、故障转移或脚本缓存被清空,所有经过模型请求限流的 API 请求会持续返回 HTTP
500:
rate_limit_check_failed
rate limit failed: NOSCRIPT No matching script. Please use EVAL.
客户端可能把该 500 显示为通用的 “We're currently experiencing high demand”,但实际并不是上游高负载,也不是 pass-through、渠道配置或使用问题。
实际生产环境中观察到:
- new-api、Redis、PostgreSQL、nginx 和宿主机资源均健康;
- Redis 的启动时间晚于 new-api,Redis 重启后其 Lua script cache 被清空;
- new-api 日志在检查窗口内连续出现 496 次
rate_limit_check_failed/NOSCRIPT; - 受影响接口包括
POST /v1/responses和POST /v1/messages; - 这些请求由 new-api 很快返回 500,并未正常进入上游处理;
- 仅重启 new-api 后立即恢复:后续实际 relay 请求全部成功,未再出现
NOSCRIPT或限流相关 500。
代码层面的原因位于 common/limiter/limiter.go:
New()通过全局sync.Once仅执行一次ScriptLoad,并把 SHA 保存在进程内;Allow()后续始终使用EvalSha;- Redis 重启后脚本缓存丢失,但 new-api 仍持有旧 SHA;
- 当前代码没有在 Redis 返回
NOSCRIPT时重新加载脚本或回退到EVAL,而是把错误直接返回给中间件; middleware/model-rate-limit.go将该错误转换为 HTTP 500rate_limit_check_failed,导致启用该限流路径的用户持续失败。
相关实现的核心逻辑如下:
limitSHA, err := r.ScriptLoad(ctx, rateLimitScript).Result()
// ...
result, err := rl.client.EvalSha(
ctx,
rl.limitScriptSHA,
[]string{key},
config.Requested,
config.Rate,
config.Capacity,
).Int()这是应用层缺少 Redis NOSCRIPT 恢复逻辑导致的问题,不是上游行为或使用配置问题。
建议修复方向:
EvalSha返回NOSCRIPT时,线程安全地重新加载内嵌 Lua 脚本、更新 SHA,并且只重试一次;- 或使用 go-redis 提供的
Script.Run模式,让客户端在EVALSHA遇到NOSCRIPT时自动回退到EVAL; - 不应绕过或关闭限流;
- 增加测试:首次请求成功后,在测试 Redis 中执行
SCRIPT FLUSH模拟脚本缓存丢失,再次请求应自动恢复且不能返回 500;
Steps to Reproduce
以下步骤只能在隔离测试环境执行,不要在生产 Redis 上执行
SCRIPT FLUSH。
-
使用
v1.0.0-rc.21,配置并连接 Redis。 -
启用
ModelRequestRateLimitEnabled,并设置非零的请求次数和持续时间限制。 -
启动 Redis 和 new-api。
-
发送一次经过模型请求限流的请求,确认请求正常,并让 new-api 加载 Lua 脚本和缓存 SHA。
-
保持 new-api 进程运行,在测试环境重启 Redis;或者在专用测试 Redis 中执行
SCRIPT FLUSH。 -
再次发送相同类型的 API 请求。
-
观察 new-api 返回 HTTP 500,并记录:
rate limit failed: NOSCRIPT No matching script. Please use EVAL. -
重启 new-api 后再次请求,服务恢复,证明问题来自进程内旧 SHA 与 Redis 新脚本缓存不一致。
Expected Result
Redis 重启、故障转移或脚本缓存丢失后,new-api 应当自动识别 NOSCRIPT,安全地重新加载 Lua 脚本并重试当前限流操作一次。
用户请求不应持续返回 HTTP 500,也不应要求运维人员手工重启 new-api。自动恢复过程必须保持现有限流语义,不能临时绕过限流。
Related Screenshots
暂无截图。关键脱敏日志如下:
检查总请求数限制失败: rate limit failed: NOSCRIPT No matching script. Please use EVAL.
[ERR] ... | rate_limit_check_failed
[GIN] ... | relay | 500 | ... | POST /v1/responses
[GIN] ... | relay | 500 | ... | POST /v1/messages
相关记录:
- [Bug] Redis NOSCRIPT error causes 500 rate_limit_check_failed — EVALSHA has no EVAL fallback #4782:相同问题,但因未使用正确模板而被关闭;
- Fix/rate limiting failure #2856:包含
NOSCRIPT自动恢复和SCRIPT FLUSH测试的相关实现,但已关闭且未合并。
Activity
new-api-reviewer-bot commentedon Jul 23, 2026
[Bot Review]
感谢提交,格式是完整的,版本、提交确认、问题描述和复现步骤都已提供。
我也看到你说明了相关的 #4782 和 #2856:#4782 目前不是开放 issue,#2856 是已关闭 PR,所以这里不按重复 issue 处理。
yujianwudi commentedon Sep 2, 2026
Follow-up evidence from a fresh
v1.0.0-rc.30incidentThis is additional evidence for the existing open issue #6412. The same failure was reproduced on an official NewAPI release / official Docker image in a self-hosted deployment. No third-party upstream error was involved.
Environment
v1.0.0-rc.30, OCI revision27ff6a8767e728f879d52770c273d4f73214a430POST /v1/chat/completions,POST /v1/responses, andPOST /v1/messages.Actual behavior
After Redis restarted while the NewAPI process remained running, requests that entered the model request rate-limit middleware repeatedly returned HTTP 500:
The error occurred before the request reached an upstream provider. A representative request returned in approximately 0.5 ms and had no corresponding consume/billing log. Requests that did not enter this rate-limit path, such as ordinary status or model-list requests, were not evidence of an upstream outage.
Timeline and impact
Redis restarted at
2026-09-02 23:39:28 +08:00and loaded the RDB successfully.NewAPI was not restarted and kept running with the previously loaded script SHA.
Representative failure at
2026-09-02 23:51:26 +08:00:From the Redis restart until
2026-09-03 00:01:04 +08:00, NewAPI recorded 5,873 rate-limit failures affecting 80 distinct authenticated users. New failures were still occurring immediately before the workaround.Restarting only the NewAPI container at
2026-09-03 00:06:03 +08:00immediately restored the service. Subsequentchat/completions,responses, andmessagesrequests returned HTTP 200, and no furtherNOSCRIPTorrate_limit_check_failedentries were observed.Code-level cause
In
common/limiter/limiter.go:New()uses a globalsync.Onceand callsScriptLoadonly once per NewAPI process.Allow()always callsEvalShausing the SHA retained in process memory.SCRIPT FLUSHremoves the server-side Lua script cache while the NewAPI process keeps the old SHA.EVALafterNOSCRIPT.middleware/model-rate-limit.goconverts that internal error into HTTP 500rate_limit_check_failedbefore callingc.Next().The relevant
v1.0.0-rc.30source is available at:Reproduction
Use an isolated test Redis only; do not run
SCRIPT FLUSHon production:v1.0.0-rc.30with Redis and enable model request rate limiting with a non-zero total request limit.SCRIPT FLUSHin the isolated Redis instance.NOSCRIPT No matching script. Please use EVAL.Expected behavior
When Redis loses the cached script, NewAPI should automatically reload the embedded Lua script or use a safe
EVALfallback, retry the current limiter operation once, and preserve the existing rate-limit semantics. A Redis restart or failover should not require a manual NewAPI restart and should not return an internal HTTP 500.This is an application-level recovery defect, not an upstream provider failure. Please consider adding a regression test that flushes the test Redis script cache after the first successful request and verifies automatic recovery.
Hades-ming commentedon Oct 19, 2026
最新版本我也遇到了这个问题 看到了一个 pr 但似乎还没有合并到主分支
PR #6486:自动恢复 Redis Lua 脚本