Skip to content

Commit

Permalink
fix: aiproxy ignore redis qpm error (#5234)
Browse files Browse the repository at this point in the history
* fix: aiproxy ignore redis qpm error

* fix: aiproxy lint
  • Loading branch information
zijiren233 authored Nov 25, 2024
1 parent d0c6dd5 commit 097bdd3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
8 changes: 4 additions & 4 deletions service/aiproxy/common/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var (
// InitRedisClient This function is called after init()
func InitRedisClient() (err error) {
if os.Getenv("REDIS_CONN_STRING") == "" {
logger.SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
logger.SysLog("REDIS_CONN_STRING not set, redis is not enabled")
return nil
}
RedisEnabled = true
logger.SysLog("Redis is enabled")
logger.SysLog("redis is enabled")
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil {
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
logger.FatalLog("failed to parse redis connection string: " + err.Error())
}
RDB = redis.NewClient(opt)

Expand All @@ -33,7 +33,7 @@ func InitRedisClient() (err error) {

_, err = RDB.Ping(ctx).Result()
if err != nil {
logger.FatalLog("Redis ping test failed: " + err.Error())
logger.FatalLog("redis ping test failed: " + err.Error())
}
return err
}
Expand Down
6 changes: 1 addition & 5 deletions service/aiproxy/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ func TokenAuth(c *gin.Context) {
}

if group.QPM > 0 {
ok, err := RateLimit(ctx, "group_qpm:"+group.ID, int(group.QPM), time.Minute)
if err != nil {
abortWithMessage(c, http.StatusInternalServerError, err.Error())
return
}
ok := ForceRateLimit(ctx, "group_qpm:"+group.ID, int(group.QPM), time.Minute)
if !ok {
abortWithMessage(c, http.StatusTooManyRequests,
group.ID+" is requesting too frequently",
Expand Down
31 changes: 22 additions & 9 deletions service/aiproxy/middleware/rate-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package middleware

import (
"context"
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/labring/sealos/service/aiproxy/common"
"github.com/labring/sealos/service/aiproxy/common/config"
"github.com/labring/sealos/service/aiproxy/common/logger"
)

var inMemoryRateLimiter common.InMemoryRateLimiter
Expand Down Expand Up @@ -61,9 +61,28 @@ func RateLimit(ctx context.Context, key string, maxRequestNum int, duration time
if common.RedisEnabled {
return redisRateLimitRequest(ctx, key, maxRequestNum, duration)
}
return MemoryRateLimit(ctx, key, maxRequestNum, duration), nil
}

// ignore redis error
func ForceRateLimit(ctx context.Context, key string, maxRequestNum int, duration time.Duration) bool {
if maxRequestNum == 0 {
return true
}
if common.RedisEnabled {
ok, err := redisRateLimitRequest(ctx, key, maxRequestNum, duration)
if err == nil {
return ok
}
logger.Error(ctx, "rate limit error: "+err.Error())
}
return MemoryRateLimit(ctx, key, maxRequestNum, duration)
}

func MemoryRateLimit(_ context.Context, key string, maxRequestNum int, duration time.Duration) bool {
// It's safe to call multi times.
inMemoryRateLimiter.Init(config.RateLimitKeyExpirationDuration)
return inMemoryRateLimiter.Request(key, maxRequestNum, duration), nil
return inMemoryRateLimiter.Request(key, maxRequestNum, duration)
}

func GlobalAPIRateLimit(c *gin.Context) {
Expand All @@ -72,13 +91,7 @@ func GlobalAPIRateLimit(c *gin.Context) {
c.Next()
return
}
ok, err := RateLimit(c.Request.Context(), "global_qpm", int(globalAPIRateLimitNum), time.Minute)
if err != nil {
fmt.Println(err.Error())
c.Status(http.StatusInternalServerError)
c.Abort()
return
}
ok := ForceRateLimit(c.Request.Context(), "global_qpm", int(globalAPIRateLimitNum), time.Minute)
if !ok {
c.Status(http.StatusTooManyRequests)
c.Abort()
Expand Down
4 changes: 2 additions & 2 deletions service/aiproxy/model/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func CacheGetTokenByKey(key string) (*TokenCache, error) {
}

if err := CacheSetToken(token); err != nil {
logger.SysError("Redis set token error: " + err.Error())
logger.SysError("redis set token error: " + err.Error())
}

return token.ToTokenCache(), nil
Expand Down Expand Up @@ -264,7 +264,7 @@ func CacheGetGroup(id string) (*GroupCache, error) {
}

if err := CacheSetGroup(group); err != nil {
logger.SysError("Redis set group error: " + err.Error())
logger.SysError("redis set group error: " + err.Error())
}

return group.ToGroupCache(), nil
Expand Down

0 comments on commit 097bdd3

Please sign in to comment.