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
46cd3f40
authored
Aug 10, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(middleware): redis atomic incr, show waiting time
parent
ff2e0dbc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
8 deletions
+18
-8
middleware/email-verification-rate-limit.go
+18
-8
No files found.
middleware/email-verification-rate-limit.go
View file @
46cd3f40
...
...
@@ -21,24 +21,34 @@ func redisEmailVerificationRateLimiter(c *gin.Context) {
rdb
:=
common
.
RDB
key
:=
"emailVerification:"
+
EmailVerificationRateLimitMark
+
":"
+
c
.
ClientIP
()
listLength
,
err
:=
rdb
.
LLen
(
ctx
,
key
)
.
Result
()
count
,
err
:=
rdb
.
Incr
(
ctx
,
key
)
.
Result
()
if
err
!=
nil
{
fmt
.
Println
(
"Redis限流检查失败:"
,
err
.
Error
())
c
.
Status
(
http
.
StatusInternalServerError
)
c
.
Abort
()
// fallback
memoryEmailVerificationRateLimiter
(
c
)
return
}
if
listLength
<
EmailVerificationMaxRequests
{
rdb
.
LPush
(
ctx
,
key
,
time
.
Now
()
.
Format
(
timeFormat
))
rdb
.
Expire
(
ctx
,
key
,
time
.
Duration
(
EmailVerificationDuration
)
*
time
.
Second
)
// 第一次设置键时设置过期时间
if
count
==
1
{
_
=
rdb
.
Expire
(
ctx
,
key
,
time
.
Duration
(
EmailVerificationDuration
)
*
time
.
Second
)
.
Err
()
}
// 检查是否超出限制
if
count
<=
int64
(
EmailVerificationMaxRequests
)
{
c
.
Next
()
return
}
// 获取剩余等待时间
ttl
,
err
:=
rdb
.
TTL
(
ctx
,
key
)
.
Result
()
waitSeconds
:=
int64
(
EmailVerificationDuration
)
if
err
==
nil
&&
ttl
>
0
{
waitSeconds
=
int64
(
ttl
.
Seconds
())
}
c
.
JSON
(
http
.
StatusTooManyRequests
,
gin
.
H
{
"success"
:
false
,
"message"
:
fmt
.
Sprintf
(
"发送过于频繁,请等待 %d 秒后再试"
,
EmailVerificationDuration
),
"message"
:
fmt
.
Sprintf
(
"发送过于频繁,请等待 %d 秒后再试"
,
waitSeconds
),
})
c
.
Abort
()
}
...
...
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