From 88ed83f41927eacc43526b5739592016d2ae4c10 Mon Sep 17 00:00:00 2001 From: tbphp Date: Mon, 5 May 2025 20:00:06 +0800 Subject: [PATCH] feat: Modellimitgroup check --- controller/option.go | 9 +++++++++ setting/rate_limit.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/controller/option.go b/controller/option.go index 81ef463c..250f16bb 100644 --- a/controller/option.go +++ b/controller/option.go @@ -110,6 +110,15 @@ func UpdateOption(c *gin.Context) { }) return } + case "ModelRequestRateLimitGroup": + err = setting.CheckModelRequestRateLimitGroup(option.Value) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } } err = model.UpdateOption(option.Key, option.Value) diff --git a/setting/rate_limit.go b/setting/rate_limit.go index aab030cd..14680791 100644 --- a/setting/rate_limit.go +++ b/setting/rate_limit.go @@ -2,6 +2,7 @@ package setting import ( "encoding/json" + "fmt" "one-api/common" "sync" ) @@ -46,3 +47,18 @@ func GetGroupRateLimit(group string) (totalCount, successCount int, found bool) } return limits[0], limits[1], true } + +func CheckModelRequestRateLimitGroup(jsonStr string) error { + checkModelRequestRateLimitGroup := make(map[string][2]int) + err := json.Unmarshal([]byte(jsonStr), &checkModelRequestRateLimitGroup) + if err != nil { + return err + } + for group, limits := range checkModelRequestRateLimitGroup { + if limits[0] < 0 || limits[1] < 0 { + return fmt.Errorf("group %s has negative rate limit values: [%d, %d]", group, limits[0], limits[1]) + } + } + + return nil +}