refactor: 移除 Ragflow 和 NATS 相关代码
This commit is contained in:
@@ -1,25 +1,48 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.com/red-future/common/redis"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// 限流 Redis Key 常量
|
||||
const (
|
||||
RateLimitKeyPrefix = "ragflow:ratelimit:" // 限流Key前缀
|
||||
RateLimitKeyIP = "ip:%s" // IP限流: ip:192.168.1.1
|
||||
RateLimitKeyUser = "user:%s" // 用户限流: user:123 或 user:anon:192.168.1.1
|
||||
RateLimitKeyService = "service:%s" // 服务限流: service:customerService
|
||||
RateLimitKeyGlobal = "global:requests" // 全局限流: global:requests
|
||||
)
|
||||
|
||||
func IncrRateLimit(ctx context.Context, key string, windowSeconds int64) (count int64, err error) {
|
||||
fullKey := RateLimitKeyPrefix + key
|
||||
count, err = g.Redis().Incr(ctx, fullKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 首次设置过期时间
|
||||
if count == 1 {
|
||||
g.Redis().Expire(ctx, fullKey, windowSeconds)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GlobalLimiter 全局限流中间件(使用Redis分布式控制)
|
||||
func GlobalLimiter(r *ghttp.Request) {
|
||||
// 从配置文件读取全局限流参数
|
||||
globalLimit := g.Cfg().MustGet(r.GetCtx(), "rate.limit", 800).Int64()
|
||||
|
||||
key := redis.RateLimitKeyGlobal
|
||||
key := RateLimitKeyGlobal
|
||||
|
||||
// 使用Redis计数器进行全局限流
|
||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
||||
count, err := IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
||||
if err != nil {
|
||||
g.Log().Errorf(r.GetCtx(), "全局限流Redis错误: %v", err)
|
||||
r.Middleware.Next()
|
||||
@@ -38,13 +61,13 @@ func GlobalLimiter(r *ghttp.Request) {
|
||||
// IPLimiter IP限流中间件(防DDoS)
|
||||
func IPLimiter(r *ghttp.Request) {
|
||||
ip := r.GetClientIp()
|
||||
key := fmt.Sprintf(redis.RateLimitKeyIP, ip)
|
||||
key := fmt.Sprintf(RateLimitKeyIP, ip)
|
||||
|
||||
// 从配置文件读取IP限流参数
|
||||
ipLimit := g.Cfg().MustGet(r.GetCtx(), "rate.ip.limit", 100).Int64()
|
||||
|
||||
// 使用Redis计数器
|
||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
||||
count, err := IncrRateLimit(r.GetCtx(), key, 1) // 1秒窗口
|
||||
if err != nil {
|
||||
g.Log().Errorf(r.GetCtx(), "IP限流Redis错误: %v", err)
|
||||
r.Middleware.Next()
|
||||
@@ -75,8 +98,8 @@ func UserLimiter(r *ghttp.Request) {
|
||||
userName = gconv.String(user.UserName)
|
||||
// 从配置文件读取用户限流参数
|
||||
userLimit := g.Cfg().MustGet(r.GetCtx(), "rate.user.limit", 50).Int64()
|
||||
key := fmt.Sprintf(redis.RateLimitKeyUser, userName)
|
||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
||||
key := fmt.Sprintf(RateLimitKeyUser, userName)
|
||||
count, err := IncrRateLimit(r.GetCtx(), key, 1)
|
||||
if err != nil {
|
||||
g.Log().Errorf(r.GetCtx(), "用户限流Redis错误: %v", err)
|
||||
return
|
||||
@@ -111,8 +134,8 @@ func ServiceLimiter(r *ghttp.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
key := fmt.Sprintf(redis.RateLimitKeyService, serverName)
|
||||
count, err := redis.IncrRateLimit(r.GetCtx(), key, 1)
|
||||
key := fmt.Sprintf(RateLimitKeyService, serverName)
|
||||
count, err := IncrRateLimit(r.GetCtx(), key, 1)
|
||||
if err != nil {
|
||||
g.Log().Errorf(r.GetCtx(), "服务限流Redis错误: %v", err)
|
||||
r.Middleware.Next()
|
||||
|
||||
Reference in New Issue
Block a user