perf: use a goroutine to handle quota post consumption (#364)

This commit is contained in:
JustSong 2023-08-12 19:36:31 +08:00
parent e0b4f96b5b
commit 0e9ff8825e

View File

@ -305,9 +305,12 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
}
var textResponse TextResponse
tokenName := c.GetString("token_name")
channelId := c.GetInt("channel_id")
defer func() {
c.Writer.Flush()
// c.Writer.Flush()
go func() {
if consumeQuota {
quota := 0
completionRatio := 1.0
@ -342,15 +345,15 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
common.SysError("error update user quota cache: " + err.Error())
}
if quota != 0 {
tokenName := c.GetString("token_name")
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio)
model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
channelId := c.GetInt("channel_id")
model.UpdateChannelUsedQuota(channelId, quota)
}
}
}()
}()
switch apiType {
case APITypeOpenAI:
if isStream {