Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2a0e1ab9
authored
May 05, 2025
by
tbphp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: Redis limit ignoring max eq 0
parent
52d5d495
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
19 deletions
+21
-19
middleware/model-rate-limit.go
+21
-19
No files found.
middleware/model-rate-limit.go
View file @
2a0e1ab9
...
...
@@ -93,25 +93,27 @@ func redisRateLimitHandler(duration int64, totalMaxCount, successMaxCount int) g
}
//2.检查总请求数限制并记录总请求(当totalMaxCount为0时会自动跳过,使用令牌桶限流器
totalKey
:=
fmt
.
Sprintf
(
"rateLimit:%s"
,
userId
)
// 初始化
tb
:=
limiter
.
New
(
ctx
,
rdb
)
allowed
,
err
=
tb
.
Allow
(
ctx
,
totalKey
,
limiter
.
WithCapacity
(
int64
(
totalMaxCount
)
*
duration
),
limiter
.
WithRate
(
int64
(
totalMaxCount
)),
limiter
.
WithRequested
(
duration
),
)
if
err
!=
nil
{
fmt
.
Println
(
"检查总请求数限制失败:"
,
err
.
Error
())
abortWithOpenAiMessage
(
c
,
http
.
StatusInternalServerError
,
"rate_limit_check_failed"
)
return
}
if
!
allowed
{
abortWithOpenAiMessage
(
c
,
http
.
StatusTooManyRequests
,
fmt
.
Sprintf
(
"您已达到总请求数限制:%d分钟内最多请求%d次,包括失败次数,请检查您的请求是否正确"
,
setting
.
ModelRequestRateLimitDurationMinutes
,
totalMaxCount
))
if
totalMaxCount
>
0
{
totalKey
:=
fmt
.
Sprintf
(
"rateLimit:%s"
,
userId
)
// 初始化
tb
:=
limiter
.
New
(
ctx
,
rdb
)
allowed
,
err
=
tb
.
Allow
(
ctx
,
totalKey
,
limiter
.
WithCapacity
(
int64
(
totalMaxCount
)
*
duration
),
limiter
.
WithRate
(
int64
(
totalMaxCount
)),
limiter
.
WithRequested
(
duration
),
)
if
err
!=
nil
{
fmt
.
Println
(
"检查总请求数限制失败:"
,
err
.
Error
())
abortWithOpenAiMessage
(
c
,
http
.
StatusInternalServerError
,
"rate_limit_check_failed"
)
return
}
if
!
allowed
{
abortWithOpenAiMessage
(
c
,
http
.
StatusTooManyRequests
,
fmt
.
Sprintf
(
"您已达到总请求数限制:%d分钟内最多请求%d次,包括失败次数,请检查您的请求是否正确"
,
setting
.
ModelRequestRateLimitDurationMinutes
,
totalMaxCount
))
}
}
// 4. 处理请求
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment