修复rate_limiter.go中残留的consts引用
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitee.com/red-future---jilin-g/common/consts"
|
|
||||||
"gitee.com/red-future---jilin-g/common/redis"
|
"gitee.com/red-future---jilin-g/common/redis"
|
||||||
"gitee.com/red-future---jilin-g/common/utils"
|
"gitee.com/red-future---jilin-g/common/utils"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
@@ -18,7 +17,7 @@ func GlobalLimiter(r *ghttp.Request) {
|
|||||||
// 从配置文件读取全局限流参数
|
// 从配置文件读取全局限流参数
|
||||||
globalLimit := g.Cfg().MustGet(r.GetCtx(), "rate.limit", 800).Int64()
|
globalLimit := g.Cfg().MustGet(r.GetCtx(), "rate.limit", 800).Int64()
|
||||||
|
|
||||||
key := consts.RateLimitKeyGlobal
|
key := redis.RateLimitKeyGlobal
|
||||||
|
|
||||||
// 使用Redis计数器进行全局限流
|
// 使用Redis计数器进行全局限流
|
||||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
||||||
@@ -40,7 +39,7 @@ func GlobalLimiter(r *ghttp.Request) {
|
|||||||
// IPLimiter IP限流中间件(防DDoS)
|
// IPLimiter IP限流中间件(防DDoS)
|
||||||
func IPLimiter(r *ghttp.Request) {
|
func IPLimiter(r *ghttp.Request) {
|
||||||
ip := r.GetClientIp()
|
ip := r.GetClientIp()
|
||||||
key := fmt.Sprintf(consts.RateLimitKeyIP, ip)
|
key := fmt.Sprintf(redis.RateLimitKeyIP, ip)
|
||||||
|
|
||||||
// 从配置文件读取IP限流参数
|
// 从配置文件读取IP限流参数
|
||||||
ipLimit := g.Cfg().MustGet(r.GetCtx(), "rate.ip.limit", 100).Int64()
|
ipLimit := g.Cfg().MustGet(r.GetCtx(), "rate.ip.limit", 100).Int64()
|
||||||
@@ -90,7 +89,7 @@ func UserLimiter(r *ghttp.Request) {
|
|||||||
userLimit = g.Cfg().MustGet(r.GetCtx(), "rate.user.anonymous.limit", 20).Int64()
|
userLimit = g.Cfg().MustGet(r.GetCtx(), "rate.user.anonymous.limit", 20).Int64()
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf(consts.RateLimitKeyUser, userId)
|
key := fmt.Sprintf(redis.RateLimitKeyUser, userId)
|
||||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Errorf(r.GetCtx(), "用户限流Redis错误: %v", err)
|
g.Log().Errorf(r.GetCtx(), "用户限流Redis错误: %v", err)
|
||||||
@@ -132,7 +131,7 @@ func ServiceLimiter(r *ghttp.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf(consts.RateLimitKeyService, serverName)
|
key := fmt.Sprintf(redis.RateLimitKeyService, serverName)
|
||||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Errorf(r.GetCtx(), "服务限流Redis错误: %v", err)
|
g.Log().Errorf(r.GetCtx(), "服务限流Redis错误: %v", err)
|
||||||
@@ -159,7 +158,7 @@ func OrderCreateLimiter(r *ghttp.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf(consts.RateLimitKeyOrder, userId)
|
key := fmt.Sprintf(redis.RateLimitKeyOrder, userId)
|
||||||
|
|
||||||
// 限制: 每个用户每分钟最多创建10个订单
|
// 限制: 每个用户每分钟最多创建10个订单
|
||||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 60) // 60秒窗口
|
count, err := redis.IncrRateLimit(r.GetCtx(), key, 60) // 60秒窗口
|
||||||
@@ -221,7 +220,7 @@ func CSMessageLimiter(r *ghttp.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf(consts.RateLimitKeyMessage, userId)
|
key := fmt.Sprintf(redis.RateLimitKeyMessage, userId)
|
||||||
|
|
||||||
// 限制: 每个用户每分钟最多发送30条消息
|
// 限制: 每个用户每分钟最多发送30条消息
|
||||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 60) // 60秒窗口
|
count, err := redis.IncrRateLimit(r.GetCtx(), key, 60) // 60秒窗口
|
||||||
|
|||||||
Reference in New Issue
Block a user