diff --git a/README.en.md b/README.en.md index 51cfb5ba..f98db2e7 100644 --- a/README.en.md +++ b/README.en.md @@ -240,7 +240,7 @@ If the channel ID is not provided, load balancing will be used to distribute the + Example: `CHANNEL_UPDATE_FREQUENCY=1440` 8. `CHANNEL_TEST_FREQUENCY`: When set, it periodically tests the channels, with the unit in minutes. If not set, no test will happen. + Example: `CHANNEL_TEST_FREQUENCY=1440` -9. `REQUEST_INTERVAL`: The time interval (in seconds) between requests when updating channel balances and testing channel availability. Default is no interval. +9. `POLLING_INTERVAL`: The time interval (in seconds) between requests when updating channel balances and testing channel availability. Default is no interval. + Example: `POLLING_INTERVAL=5` ### Command Line Parameters diff --git a/README.md b/README.md index f4712561..14c8e0bf 100644 --- a/README.md +++ b/README.md @@ -251,7 +251,7 @@ graph LR + 例子:`SESSION_SECRET=random_string` 3. `SQL_DSN`:设置之后将使用指定数据库而非 SQLite,请使用 MySQL 8.0 版本。 + 例子:`SQL_DSN=root:123456@tcp(localhost:3306)/oneapi` -4. `FRONTEND_BASE_URL`:设置之后将使用指定的前端地址,而非后端地址。 +4. `FRONTEND_BASE_URL`:设置之后将使用指定的前端地址,而非后端地址,仅限从服务器设置。 + 例子:`FRONTEND_BASE_URL=https://openai.justsong.cn` 5. `SYNC_FREQUENCY`:设置之后将定期与数据库同步配置,单位为秒,未设置则不进行同步。 + 例子:`SYNC_FREQUENCY=60` @@ -261,7 +261,7 @@ graph LR + 例子:`CHANNEL_UPDATE_FREQUENCY=1440` 8. `CHANNEL_TEST_FREQUENCY`:设置之后将定期检查渠道,单位为分钟,未设置则不进行检查。 + 例子:`CHANNEL_TEST_FREQUENCY=1440` -9. `REQUEST_INTERVAL`:批量更新渠道余额以及测试可用性时的请求间隔,单位为秒,默认无间隔。 +9. `POLLING_INTERVAL`:批量更新渠道余额以及测试可用性时的请求间隔,单位为秒,默认无间隔。 + 例子:`POLLING_INTERVAL=5` ### 命令行参数 diff --git a/common/constants.go b/common/constants.go index 04502f28..c345c620 100644 --- a/common/constants.go +++ b/common/constants.go @@ -72,7 +72,7 @@ var RootUserEmail = "" var IsMasterNode = os.Getenv("NODE_TYPE") != "slave" -var requestInterval, _ = strconv.Atoi(os.Getenv("REQUEST_INTERVAL")) +var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL")) var RequestInterval = time.Duration(requestInterval) * time.Second const ( diff --git a/controller/relay-text.go b/controller/relay-text.go index d6f1c39d..51f32da1 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -199,13 +199,17 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { } tokenName := c.GetString("token_name") logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) - model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent, channelName) + model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent,channelName) if strings.Contains(channelName, "免费") == false { quotaDelta := quota - preConsumedQuota err := model.PostConsumeTokenQuota(tokenId, quotaDelta) if err != nil { common.SysError("error consuming token remain quota: " + err.Error()) } + err = model.CacheUpdateUserQuota(userId) + if err != nil { + common.SysError("error update user quota cache: " + err.Error()) + } if quota != 0 { model.UpdateUserUsedQuotaAndRequestCount(userId, quota) channelId := c.GetInt("channel_id") diff --git a/model/cache.go b/model/cache.go index e25b64f4..284e29ad 100644 --- a/model/cache.go +++ b/model/cache.go @@ -83,6 +83,18 @@ func CacheGetUserQuota(id int) (quota int, err error) { return quota, err } +func CacheUpdateUserQuota(id int) error { + if !common.RedisEnabled { + return nil + } + quota, err := GetUserQuota(id) + if err != nil { + return err + } + err = common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), UserId2QuotaCacheSeconds*time.Second) + return err +} + func CacheIsUserEnabled(userId int) bool { if !common.RedisEnabled { return IsUserEnabled(userId)