fix: 配置加载后无限刷新修复

This commit is contained in:
kercylan98 2023-07-05 21:29:58 +08:00
parent b234568e56
commit 6634aa675e
2 changed files with 17 additions and 13 deletions

View File

@ -29,6 +29,7 @@ var (
cRefreshHandle RefreshHandle cRefreshHandle RefreshHandle
json = jsonIter.ConfigCompatibleWithStandardLibrary json = jsonIter.ConfigCompatibleWithStandardLibrary
mutex sync.Mutex mutex sync.Mutex
isInit = true
) )
// Init 配置初始化 // Init 配置初始化
@ -44,24 +45,23 @@ func Init(loadDir string, loadHandle LoadHandle, refreshHandle RefreshHandle) {
// - 加载后并不会刷新线上配置,需要执行 Refresh 函数对线上配置进行刷新 // - 加载后并不会刷新线上配置,需要执行 Refresh 函数对线上配置进行刷新
func Load() { func Load() {
mutex.Lock() mutex.Lock()
if cTicker != nil {
WithTickerLoad(cTicker, cInterval)
} else {
cLoadHandle(func(filename string, config any) error { cLoadHandle(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath.Join(cLoadDir, filename)) bytes, err := os.ReadFile(filepath.Join(cLoadDir, filename))
if err != nil { if err != nil {
return err return err
} }
if err = json.Unmarshal(bytes, &config); err == nil { if err = json.Unmarshal(bytes, &config); err == nil && isInit {
log.Info("Config", zap.String("Name", filename), zap.Bool("LoadSuccess", true)) log.Info("Config", zap.String("Name", filename), zap.Bool("LoadSuccess", true))
} }
return err return err
}) })
} isInit = false
mutex.Unlock() mutex.Unlock()
} }
// WithTickerLoad 通过定时器加载配置 // WithTickerLoad 通过定时器加载配置
// - 通过定时器加载配置后,会自动刷新线上配置
// - 调用该函数后将会立即加载并刷新一次配置,随后每隔 interval 时间加载并刷新一次配置
func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) { func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) {
if ticker != cTicker && cTicker != nil { if ticker != cTicker && cTicker != nil {
cTicker.StopTimer(tickerLoadRefresh) cTicker.StopTimer(tickerLoadRefresh)

View File

@ -264,6 +264,10 @@ func (slf *Config) initData() error {
if slf.horizontal { if slf.horizontal {
c := slf.matrix.Get(x+i, y) c := slf.matrix.Get(x+i, y)
if c == nil { if c == nil {
if currentIndex < slf.IndexCount {
stop = true
break
}
value = getValueZero(field.SourceType) value = getValueZero(field.SourceType)
zero = true zero = true
} else if currentIndex < slf.IndexCount && len(strings.TrimSpace(c.String())) == 0 { } else if currentIndex < slf.IndexCount && len(strings.TrimSpace(c.String())) == 0 {