refactor(login): 提取客户端IP到变量以优化速率限制逻辑 - 将 r.GetClientIp() 调用结果存储到 ip 变量中 - 在 AllowVisitByIP4 方法调用中使用该变量 - 保持相同的速率限制功能,但提高代码可读性 ```
This commit is contained in:
@@ -81,9 +81,10 @@ func Limiter(r *ghttp.Request) {
|
||||
// 3. 为任意键 "some-key" 获取一个速率限制器
|
||||
// - rate.Limit(2): 表示速率为 "每秒2个请求"
|
||||
// - 2: 表示桶的容量 (Burst),允许瞬时处理2个请求
|
||||
|
||||
if !limiter.AllowVisitByIP4(r.GetClientIp()) {
|
||||
ip := r.GetClientIp()
|
||||
if !limiter.AllowVisitByIP4(ip) {
|
||||
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
||||
|
||||
r.ExitAll()
|
||||
}
|
||||
r.Middleware.Next()
|
||||
|
||||
Reference in New Issue
Block a user