pre_consume_quota.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package service
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/logger"
  7. "github.com/QuantumNous/new-api/model"
  8. relaycommon "github.com/QuantumNous/new-api/relay/common"
  9. "github.com/QuantumNous/new-api/types"
  10. "github.com/bytedance/gopkg/util/gopool"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func ReturnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo) {
  14. if relayInfo.FinalPreConsumedQuota != 0 {
  15. logger.LogInfo(c, fmt.Sprintf("用户 %d 请求失败, 返还预扣费额度 %s", relayInfo.UserId, logger.FormatQuota(relayInfo.FinalPreConsumedQuota)))
  16. gopool.Go(func() {
  17. relayInfoCopy := *relayInfo
  18. err := PostConsumeQuota(&relayInfoCopy, -relayInfoCopy.FinalPreConsumedQuota, 0, false)
  19. if err != nil {
  20. common.SysLog("error return pre-consumed quota: " + err.Error())
  21. }
  22. })
  23. }
  24. }
  25. // PreConsumeQuota checks if the user has enough quota to pre-consume.
  26. // It returns the pre-consumed quota if successful, or an error if not.
  27. func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) *types.NewAPIError {
  28. userQuota, err := model.GetUserQuota(relayInfo.UserId, false)
  29. if err != nil {
  30. return types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
  31. }
  32. if userQuota <= 0 {
  33. return types.NewErrorWithStatusCode(fmt.Errorf("用户额度不足, 剩余额度: %s", logger.FormatQuota(userQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
  34. }
  35. if userQuota-preConsumedQuota < 0 {
  36. return types.NewErrorWithStatusCode(fmt.Errorf("预扣费额度失败, 用户剩余额度: %s, 需要预扣费额度: %s", logger.FormatQuota(userQuota), logger.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
  37. }
  38. trustQuota := common.GetTrustQuota()
  39. relayInfo.UserQuota = userQuota
  40. if userQuota > trustQuota {
  41. // 用户额度充足,判断令牌额度是否充足
  42. if !relayInfo.TokenUnlimited {
  43. // 非无限令牌,判断令牌额度是否充足
  44. tokenQuota := c.GetInt("token_quota")
  45. if tokenQuota > trustQuota {
  46. // 令牌额度充足,信任令牌
  47. preConsumedQuota = 0
  48. logger.LogInfo(c, fmt.Sprintf("用户 %d 剩余额度 %s 且令牌 %d 额度 %d 充足, 信任且不需要预扣费", relayInfo.UserId, logger.FormatQuota(userQuota), relayInfo.TokenId, tokenQuota))
  49. }
  50. } else {
  51. // in this case, we do not pre-consume quota
  52. // because the user has enough quota
  53. preConsumedQuota = 0
  54. logger.LogInfo(c, fmt.Sprintf("用户 %d 额度充足且为无限额度令牌, 信任且不需要预扣费", relayInfo.UserId))
  55. }
  56. }
  57. if preConsumedQuota > 0 {
  58. err := PreConsumeTokenQuota(relayInfo, preConsumedQuota)
  59. if err != nil {
  60. return types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
  61. }
  62. err = model.DecreaseUserQuota(relayInfo.UserId, preConsumedQuota)
  63. if err != nil {
  64. return types.NewError(err, types.ErrorCodeUpdateDataError, types.ErrOptionWithSkipRetry())
  65. }
  66. logger.LogInfo(c, fmt.Sprintf("用户 %d 预扣费 %s, 预扣费后剩余额度: %s", relayInfo.UserId, logger.FormatQuota(preConsumedQuota), logger.FormatQuota(userQuota-preConsumedQuota)))
  67. }
  68. relayInfo.FinalPreConsumedQuota = preConsumedQuota
  69. return nil
  70. }