Skip to content

Commit

Permalink
🐛 fix: Consumption is 0 and logs are recorded (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartialBE committed Nov 24, 2024
1 parent d1d6c43 commit c3845a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
3 changes: 3 additions & 0 deletions model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func sendQuotaWarningEmail(userId int, userQuota int, noMoreQuota bool) {
}

func PostConsumeTokenQuota(tokenId int, quota int) (err error) {
if quota == 0 {
return nil
}
token, err := GetTokenById(tokenId)
if err != nil {
return err
Expand Down
23 changes: 11 additions & 12 deletions relay/relay_util/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,18 @@ func (q *Quota) completedQuotaConsumption(usage *types.Usage, tokenName string,
}()

quota := q.GetTotalQuotaByUsage(usage)
if quota == 0 {
return fmt.Errorf("user_id: %d, channel_id: %d, token_id: %d, quota is 0", q.userId, q.channelId, q.tokenId)
}

quotaDelta := quota - q.preConsumedQuota
err := model.PostConsumeTokenQuota(q.tokenId, quotaDelta)
if err != nil {
return errors.New("error consuming token remain quota: " + err.Error())
}
err = model.CacheUpdateUserQuota(q.userId)
if err != nil {
return errors.New("error consuming token remain quota: " + err.Error())
if quota > 0 {
quotaDelta := quota - q.preConsumedQuota
err := model.PostConsumeTokenQuota(q.tokenId, quotaDelta)
if err != nil {
return errors.New("error consuming token remain quota: " + err.Error())
}
err = model.CacheUpdateUserQuota(q.userId)
if err != nil {
return errors.New("error consuming token remain quota: " + err.Error())
}
model.UpdateChannelUsedQuota(q.channelId, quota)
}

model.RecordConsumeLog(
Expand All @@ -161,7 +161,6 @@ func (q *Quota) completedQuotaConsumption(usage *types.Usage, tokenName string,
q.GetLogMeta(usage),
)
model.UpdateUserUsedQuotaAndRequestCount(q.userId, quota)
model.UpdateChannelUsedQuota(q.channelId, quota)

return nil
}
Expand Down

0 comments on commit c3845a6

Please sign in to comment.