🐛 fix: Fixed the slave service unsynchronized data (#191)

This commit is contained in:
MartialBE 2024-05-18 13:27:13 +08:00
parent f26a7839c9
commit 6ab116c0eb
No known key found for this signature in database
GPG Key ID: 27C0267EC84B0A5C
2 changed files with 28 additions and 12 deletions

View File

@ -146,15 +146,15 @@ func UpdateAllChannelsBalance(c *gin.Context) {
}) })
} }
func AutomaticallyUpdateChannels(frequency int) { // func AutomaticallyUpdateChannels(frequency int) {
if frequency <= 0 { // if frequency <= 0 {
return // return
} // }
for { // for {
time.Sleep(time.Duration(frequency) * time.Minute) // time.Sleep(time.Duration(frequency) * time.Minute)
common.SysLog("updating all channels") // common.SysLog("updating all channels")
_ = updateAllChannelsBalance() // _ = updateAllChannelsBalance()
common.SysLog("channels update done") // common.SysLog("channels update done")
} // }
} // }

18
main.go
View File

@ -15,6 +15,7 @@ import (
"one-api/model" "one-api/model"
"one-api/relay/util" "one-api/relay/util"
"one-api/router" "one-api/router"
"time"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie" "github.com/gin-contrib/sessions/cookie"
@ -71,10 +72,11 @@ func initMemoryCache() {
common.SysLog("memory cache enabled") common.SysLog("memory cache enabled")
common.SysError(fmt.Sprintf("sync frequency: %d seconds", syncFrequency)) common.SysError(fmt.Sprintf("sync frequency: %d seconds", syncFrequency))
go model.SyncOptions(syncFrequency) go model.SyncOptions(syncFrequency)
go SyncChannelCache(syncFrequency)
} }
func initSync() { 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")) go controller.AutomaticallyTestChannels(viper.GetInt("channel.test_frequency"))
} }
@ -99,3 +101,17 @@ func initHttpServer() {
common.FatalLog("failed to start HTTP server: " + err.Error()) 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()
}
}