|
|
@@ -2,6 +2,7 @@ package controller
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
+ "context"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
@@ -210,6 +211,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
// 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 consumeQuota && preConsumedQuota > 0 {
|
|
|
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota)
|
|
|
@@ -348,13 +350,13 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
if preConsumedQuota != 0 {
|
|
|
- go func() {
|
|
|
+ go func(ctx context.Context) {
|
|
|
// return pre-consumed quota
|
|
|
err := model.PostConsumeTokenQuota(tokenId, -preConsumedQuota)
|
|
|
if err != nil {
|
|
|
- common.SysError("error return pre-consumed quota: " + err.Error())
|
|
|
+ common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
|
|
}
|
|
|
- }()
|
|
|
+ }(c.Request.Context())
|
|
|
}
|
|
|
return relayErrorHandler(resp)
|
|
|
}
|
|
|
@@ -364,7 +366,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
tokenName := c.GetString("token_name")
|
|
|
channelId := c.GetInt("channel_id")
|
|
|
|
|
|
- defer func() {
|
|
|
+ defer func(ctx context.Context) {
|
|
|
// c.Writer.Flush()
|
|
|
go func() {
|
|
|
if consumeQuota {
|
|
|
@@ -387,21 +389,21 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
quotaDelta := quota - preConsumedQuota
|
|
|
err := model.PostConsumeTokenQuota(tokenId, quotaDelta)
|
|
|
if err != nil {
|
|
|
- common.SysError("error consuming token remain quota: " + err.Error())
|
|
|
+ common.LogError(ctx, "error consuming token remain quota: "+err.Error())
|
|
|
}
|
|
|
err = model.CacheUpdateUserQuota(userId)
|
|
|
if err != nil {
|
|
|
- common.SysError("error update user quota cache: " + err.Error())
|
|
|
+ common.LogError(ctx, "error update user quota cache: "+err.Error())
|
|
|
}
|
|
|
if quota != 0 {
|
|
|
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio)
|
|
|
- model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
|
|
|
+ model.RecordConsumeLog(ctx, userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
|
|
|
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
|
|
model.UpdateChannelUsedQuota(channelId, quota)
|
|
|
}
|
|
|
}
|
|
|
}()
|
|
|
- }()
|
|
|
+ }(c.Request.Context())
|
|
|
switch apiType {
|
|
|
case APITypeOpenAI:
|
|
|
if isStream {
|