修改限流

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

@@ -8,6 +8,7 @@ import (
"sync"
"time"
"gitee.com/red-future---jilin-g/common/consts"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
@@ -478,15 +479,11 @@ func IsUserActive(ctx context.Context, userId string, seconds int64) (bool, erro
// ============== 限流相关 ==============
const (
// RateLimitKeyPrefix 限流计数器 Key 前缀
RateLimitKeyPrefix = "ragflow:ratelimit:"
)
// IncrRateLimit 增加限流计数器,返回当前计数
// key: 限流key需要包含完整路径如 "ip:192.168.1.1"
// windowSeconds: 时间窗口(秒)
func IncrRateLimit(ctx context.Context, key string, windowSeconds int64) (count int64, err error) {
fullKey := RateLimitKeyPrefix + key
fullKey := consts.RateLimitKeyPrefix + key
result, err := redisClient.Do(ctx, "INCR", fullKey)
if err != nil {
return
@@ -502,7 +499,7 @@ func IncrRateLimit(ctx context.Context, key string, windowSeconds int64) (count
// GetRateLimit 获取当前限流计数
func GetRateLimit(ctx context.Context, key string) (count int64, err error) {
fullKey := RateLimitKeyPrefix + key
fullKey := consts.RateLimitKeyPrefix + key
result, err := redisClient.Get(ctx, fullKey)
if err != nil {
return