|
@@ -39,13 +39,16 @@ func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
|
|
|
if userQuota-preConsumedQuota < 0 {
|
|
if userQuota-preConsumedQuota < 0 {
|
|
|
return 0, types.NewErrorWithStatusCode(fmt.Errorf("pre-consume quota failed, user quota: %s, need quota: %s", logger.FormatQuota(userQuota), logger.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
|
|
return 0, types.NewErrorWithStatusCode(fmt.Errorf("pre-consume quota failed, user quota: %s, need quota: %s", logger.FormatQuota(userQuota), logger.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ trustQuota := common.GetTrustQuota()
|
|
|
|
|
+
|
|
|
relayInfo.UserQuota = userQuota
|
|
relayInfo.UserQuota = userQuota
|
|
|
- if userQuota > 100*preConsumedQuota {
|
|
|
|
|
|
|
+ if userQuota > trustQuota {
|
|
|
// 用户额度充足,判断令牌额度是否充足
|
|
// 用户额度充足,判断令牌额度是否充足
|
|
|
if !relayInfo.TokenUnlimited {
|
|
if !relayInfo.TokenUnlimited {
|
|
|
// 非无限令牌,判断令牌额度是否充足
|
|
// 非无限令牌,判断令牌额度是否充足
|
|
|
tokenQuota := c.GetInt("token_quota")
|
|
tokenQuota := c.GetInt("token_quota")
|
|
|
- if tokenQuota > 100*preConsumedQuota {
|
|
|
|
|
|
|
+ if tokenQuota > trustQuota {
|
|
|
// 令牌额度充足,信任令牌
|
|
// 令牌额度充足,信任令牌
|
|
|
preConsumedQuota = 0
|
|
preConsumedQuota = 0
|
|
|
logger.LogInfo(c, fmt.Sprintf("user %d quota %s and token %d quota %d are enough, trusted and no need to pre-consume", relayInfo.UserId, logger.FormatQuota(userQuota), relayInfo.TokenId, tokenQuota))
|
|
logger.LogInfo(c, fmt.Sprintf("user %d quota %s and token %d quota %d are enough, trusted and no need to pre-consume", relayInfo.UserId, logger.FormatQuota(userQuota), relayInfo.TokenId, tokenQuota))
|
|
@@ -67,6 +70,7 @@ func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return 0, types.NewError(err, types.ErrorCodeUpdateDataError, types.ErrOptionWithSkipRetry())
|
|
return 0, types.NewError(err, types.ErrorCodeUpdateDataError, types.ErrOptionWithSkipRetry())
|
|
|
}
|
|
}
|
|
|
|
|
+ logger.LogInfo(c, fmt.Sprintf("用户 %d 预扣费 %s, 预扣费后剩余额度: %s", relayInfo.UserId, logger.FormatQuota(preConsumedQuota), logger.FormatQuota(userQuota-preConsumedQuota)))
|
|
|
}
|
|
}
|
|
|
relayInfo.FinalPreConsumedQuota = preConsumedQuota
|
|
relayInfo.FinalPreConsumedQuota = preConsumedQuota
|
|
|
return preConsumedQuota, nil
|
|
return preConsumedQuota, nil
|