diff --git a/controller/channel-billing.go b/controller/channel-billing.go index c6d5c511..ffb53fbd 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -146,15 +146,15 @@ func UpdateAllChannelsBalance(c *gin.Context) { }) } -func AutomaticallyUpdateChannels(frequency int) { - if frequency <= 0 { - return - } +// func AutomaticallyUpdateChannels(frequency int) { +// if frequency <= 0 { +// return +// } - for { - time.Sleep(time.Duration(frequency) * time.Minute) - common.SysLog("updating all channels") - _ = updateAllChannelsBalance() - common.SysLog("channels update done") - } -} +// for { +// time.Sleep(time.Duration(frequency) * time.Minute) +// common.SysLog("updating all channels") +// _ = updateAllChannelsBalance() +// common.SysLog("channels update done") +// } +// } diff --git a/main.go b/main.go index fb89d015..b235d76b 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "one-api/model" "one-api/relay/util" "one-api/router" + "time" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" @@ -71,10 +72,11 @@ func initMemoryCache() { common.SysLog("memory cache enabled") common.SysError(fmt.Sprintf("sync frequency: %d seconds", syncFrequency)) go model.SyncOptions(syncFrequency) + go SyncChannelCache(syncFrequency) } func initSync() { - go controller.AutomaticallyUpdateChannels(viper.GetInt("channel.update_frequency")) + // go controller.AutomaticallyUpdateChannels(viper.GetInt("channel.update_frequency")) go controller.AutomaticallyTestChannels(viper.GetInt("channel.test_frequency")) } @@ -99,3 +101,17 @@ func initHttpServer() { common.FatalLog("failed to start HTTP server: " + err.Error()) } } + +func SyncChannelCache(frequency int) { + // 只有 从 服务器端获取数据的时候才会用到 + if common.IsMasterNode { + common.SysLog("master node does't synchronize the channel") + return + } + for { + time.Sleep(time.Duration(frequency) * time.Second) + common.SysLog("syncing channels from database") + model.ChannelGroup.Load() + util.PricingInstance.Init() + } +}