From 477c6653f14f848f586119e09ffc5fc2b64eeec4 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 14 Jan 2024 13:44:38 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"fix:=20=E4=BF=AE=E5=A4=8D=E9=AB=98?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E4=B8=8B=EF=BC=8C=E9=AB=98=E9=A2=9D=E5=BA=A6?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BD=BF=E7=94=A8=E4=BD=8E=E9=A2=9D=E5=BA=A6?= =?UTF-8?q?=E4=BB=A4=E7=89=8C=E6=B2=A1=E6=9C=89=E9=A2=84=E6=89=A3=E8=B4=B9?= =?UTF-8?q?=E8=80=8C=E5=AF=BC=E8=87=B4=E4=BB=A4=E7=89=8C=E5=A4=A7=E9=A2=9D?= =?UTF-8?q?=E6=AC=A0=E8=B4=B9"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f0ffe144371a4e118478306b906ddb7a5f027024. --- controller/relay-text.go | 24 +++++++++--------------- middleware/auth.go | 4 ---- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/controller/relay-text.go b/controller/relay-text.go index 5a7544fb..64338545 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -53,7 +53,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { tokenId := c.GetInt("token_id") userId := c.GetInt("id") group := c.GetString("group") - tokenUnlimited := c.GetBool("token_unlimited_quota") var textRequest GeneralOpenAIRequest err := common.UnmarshalBodyReusable(c, &textRequest) if err != nil { @@ -241,20 +240,15 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { return errorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError) } if userQuota > 100*preConsumedQuota { - // 用户额度充足,判断令牌额度是否充足 - if !tokenUnlimited { - // 非无限令牌,判断令牌额度是否充足 - tokenQuota := c.GetInt("token_quota") - if tokenQuota > 100*preConsumedQuota { - // 令牌额度充足,信任令牌 - preConsumedQuota = 0 - common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d quota %d and token %d quota %d are enough, trusted and no need to pre-consume", userId, userQuota, tokenId, tokenQuota)) - } - } else { - // in this case, we do not pre-consume quota - // because the user has enough quota - preConsumedQuota = 0 - common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d with unlimited token has enough quota %d, trusted and no need to pre-consume", userId, userQuota)) + // in this case, we do not pre-consume quota + // because the user has enough quota + preConsumedQuota = 0 + common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d has enough quota %d, trusted and no need to pre-consume", userId, userQuota)) + } + if preConsumedQuota > 0 { + err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota) + if err != nil { + return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden) } } var requestBody io.Reader diff --git a/middleware/auth.go b/middleware/auth.go index b3ad67b4..ad7e64b7 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -106,10 +106,6 @@ func TokenAuth() func(c *gin.Context) { c.Set("id", token.UserId) c.Set("token_id", token.Id) c.Set("token_name", token.Name) - c.Set("token_unlimited_quota", token.UnlimitedQuota) - if !token.UnlimitedQuota { - c.Set("token_quota", token.RemainQuota) - } if len(parts) > 1 { if model.IsAdmin(token.UserId) { c.Set("channelId", parts[1])