feat: 通过 golang 模板生成的配置结构代码支持通过 Sync 函数执行安全的配置操作,避免配置被刷新造成的异常

This commit is contained in:
kercylan98 2023-07-17 18:43:38 +08:00
parent 3ee638f4df
commit 8bbd49554f
1 changed files with 11 additions and 1 deletions

View File

@ -27,6 +27,7 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
import ( import (
jsonIter "github.com/json-iterator/go" jsonIter "github.com/json-iterator/go"
"github.com/kercylan98/minotaur/utils/log" "github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/hash"
"sync" "sync"
) )
@ -192,13 +193,22 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
// GetConfigs 获取所有配置 // GetConfigs 获取所有配置
func GetConfigs() map[Sign]any { func GetConfigs() map[Sign]any {
return configs mutex.Lock()
defer mutex.Unlock()
return hash.Copy(configs)
} }
// GetConfigSigns 获取所有配置的标识 // GetConfigSigns 获取所有配置的标识
func GetConfigSigns() []Sign { func GetConfigSigns() []Sign {
return signs return signs
} }
// Sync 同步操作配置
func Sync(handle func(configs map[Sign]any)) {
mutex.Lock()
defer mutex.Unlock()
handle(hash.Copy(configs))
}
`, slf) `, slf)
} }