修改限流

This commit is contained in:
2025-12-29 14:44:32 +08:00
parent c80ecb19e4
commit cc67dd2485
4 changed files with 325 additions and 30 deletions

View File

@@ -1,13 +1,10 @@
package middleware
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gstr"
"golang.org/x/time/rate"
)
// Logger 中间件
@@ -24,17 +21,6 @@ func Logger(r *ghttp.Request) {
)
}
var rateLimit = g.Cfg().MustGet(context.TODO(), "rate.limit").Int()
var rateBurst = g.Cfg().MustGet(context.TODO(), "rate.burst").Int()
var limiter = rate.NewLimiter(rate.Limit(rateLimit), rateBurst)
func Limiter(r *ghttp.Request) {
if !limiter.Allow() {
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
r.ExitAll()
}
r.Middleware.Next()
}
func Auth(r *ghttp.Request) {
//utils.GetUserInfo(r.GetCtx())
token := r.Header.Get("Authorization")
@@ -51,7 +37,3 @@ func Auth(r *ghttp.Request) {
r.Middleware.Next()
}
func validateToken(token string) bool {
// 实现 token 验证逻辑
return token == "valid-token"
}