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

View File

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