feat(login): 添加限流功能并更新依赖 - 引入 github.com/yudeguang/ratelimit 库替代原有的 golang.org/x/time/rate - 实现基于IP地址的访问频率限制 - 添加每秒20次请求的限流规则 - 更新 go.mod 和 go.sum 文件以包含新依赖项 ```
This commit is contained in:
@@ -40,6 +40,8 @@ require (
|
||||
github.com/tidwall/gjson v1.9.3 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/yudeguang/iox v0.0.0-20220912082704-297952f8e912 // indirect
|
||||
github.com/yudeguang/ratelimit v0.0.0-20240108053714-dcabc0e41abd // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
|
||||
@@ -140,6 +140,10 @@ github.com/tencent-connect/botgo v0.2.1/go.mod h1:oO1sG9ybhXNickvt+CVym5khwQ+uKh
|
||||
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/yudeguang/iox v0.0.0-20220912082704-297952f8e912 h1:38jG5Ar6KtDhahRr1eI0lHI2BzQSk/izqE2Aqn9EXmI=
|
||||
github.com/yudeguang/iox v0.0.0-20220912082704-297952f8e912/go.mod h1:/yeZ8yPyE9g4jM7Z8LPKwi1L9lDGmLGQ0ywR4rtdNdY=
|
||||
github.com/yudeguang/ratelimit v0.0.0-20240108053714-dcabc0e41abd h1:wHlR3yP30WN+uQhGc709SXlI1JuSNxQow+AMSGNYltg=
|
||||
github.com/yudeguang/ratelimit v0.0.0-20240108053714-dcabc0e41abd/go.mod h1:NcFk/p88iJxUWYrlDIat7mJLufpsHExnYvxUkApkhJc=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/yudeguang/ratelimit"
|
||||
|
||||
i18n "blazing/modules/base/middleware"
|
||||
|
||||
@@ -12,7 +14,6 @@ import (
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/xiaoqidun/qqwry"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -62,7 +63,17 @@ func beforeServeHook(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
}
|
||||
|
||||
var limiter = rate.NewLimiter(rate.Limit(150), 50)
|
||||
// var limiter = rate.NewLimiter(rate.Limit(150), 50)
|
||||
var limiter *ratelimit.Rule = ratelimit.NewRule()
|
||||
|
||||
// 简单规则案例
|
||||
func init() {
|
||||
|
||||
//步骤二:增加一条或者多条规则组成复合规则,此复合规则必须至少包含一条规则
|
||||
limiter.AddRule(time.Second*1, 20)
|
||||
//步骤三:调用函数判断某用户是否允许访问 allow:= r.AllowVisit(user)
|
||||
|
||||
}
|
||||
|
||||
// Limiter is a middleware that implements rate limiting for all HTTP requests.
|
||||
// It returns HTTP 429 (Too Many Requests) when the rate limit is exceeded.
|
||||
@@ -71,7 +82,7 @@ func Limiter(r *ghttp.Request) {
|
||||
// - rate.Limit(2): 表示速率为 "每秒2个请求"
|
||||
// - 2: 表示桶的容量 (Burst),允许瞬时处理2个请求
|
||||
|
||||
if !limiter.Allow() {
|
||||
if !limiter.AllowVisitByIP4(r.GetClientIp()) {
|
||||
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
||||
r.ExitAll()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user