Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ func decreaseTokenQuota(id int, quota int64) (err error) {
return err
}

// AddUsedQuota only increase used_quota, won't change remain_quota
func AddUsedQuota(id int, quota int64) (err error) {
err = DB.Model(&Token{}).Where("id = ?", id).Updates(
map[string]interface{}{
"used_quota": gorm.Expr("used_quota + ?", quota),
"accessed_time": helper.GetTimestamp(),
},
).Error
return fmt.Errorf("AddUserQuota failed: %w", err)
}

func PreConsumeTokenQuota(tokenId int, quota int64) (err error) {
if quota < 0 {
return errors.New("quota 不能为负数!")
Expand Down Expand Up @@ -289,15 +300,17 @@ func PostConsumeTokenQuota(tokenId int, quota int64) (err error) {
} else {
err = IncreaseUserQuota(token.UserId, -quota)
}
if !token.UnlimitedQuota {
if quota > 0 {
err = DecreaseTokenQuota(tokenId, quota)
} else {
err = IncreaseTokenQuota(tokenId, -quota)
}
if err != nil {
return err
}
if err != nil {
return err
}
// if the token has unlimited quota, we don't need to change its quota
if token.UnlimitedQuota {
return AddUsedQuota(tokenId, quota)
}
return nil
if quota > 0 {
err = DecreaseTokenQuota(tokenId, quota)
} else {
err = IncreaseTokenQuota(tokenId, -quota)
}
return err
}