Skip to content

Commit

Permalink
调整检测剩余时间代码
Browse files Browse the repository at this point in the history
  • Loading branch information
gcslaoli committed Oct 12, 2024
1 parent 1f64980 commit 44570a6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/audit_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ func AuditLimit(r *ghttp.Request) {
g.Log().Debug(ctx, token, model, "remain", remain, "limit", limit, "per", per)
if remain < 1 {
r.Response.Status = 429
delayFrom := limiter.Reserve().DelayFrom(time.Now())
reservation := limiter.ReserveN(time.Now(), 1)
if !reservation.OK() {
// 处理预留失败的情况,例如返回错误
r.Response.WriteJson(g.Map{
"detail": "You have triggered the usage frequency limit of " + model + ", the current limit is " + gconv.String(limit) + " times/" + gconv.String(per) + ", please wait a moment before trying again.\n" + "您已经触发 " + model + " 使用频率限制,当前限制为 " + gconv.String(limit) + " 次/" + gconv.String(per) + ",请稍后再试.",
})
return
}
delayFrom := reservation.Delay()
g.Log().Debug(ctx, "delayFrom", delayFrom)
r.Response.WriteJson(g.Map{
"detail": "You have triggered the usage frequency limit of " + model + ", the current limit is " + gconv.String(limit) + " times/" + gconv.String(per) + ", please wait " + gconv.String(int(delayFrom.Seconds())) + " seconds before trying again.\n" + "您已经触发 " + model + " 使用频率限制,当前限制为 " + gconv.String(limit) + " 次/" + gconv.String(per) + ",请等待 " + gconv.String(int(delayFrom.Seconds())) + " 秒后再试.",
Expand Down

0 comments on commit 44570a6

Please sign in to comment.