refactor(config): publish config singletons through atomic pointers
The three configuration singletons (C_globalConfig, C_botUserConfig, C_botNodeConfig) were plain variables: LoadGlobalConfig and friends assigned them from the goroutine handling a settings update, while bot pipes, the node tracker and the HTTP handlers read them from their own goroutines. That is an unsynchronized read of a concurrently written variable — a data race the race detector reports, and one that already existed before any hot-reload work because /api/webhook/add reloads the configuration while the bots run. Replace them with atomic.Pointer values behind Current(), BotUsers() and BotNodes(). Each reload builds a fresh value and publishes it atomically, so a reader either sees the previous configuration or the new one, never a partial one. Callers read through the accessor on every use instead of caching it. Two spots that read several fields of one guard now snapshot once per call, so a reload cannot split a combined check mid-flight: - qq_napcat.handleNapcatEvent, which evaluates the debug guards per event - the komari task-echo guard, now behind taskEchoEnabled() The NapCat HTTP methods keep logging on showNapcatAction alone (without requiring debugMode), matching their existing behaviour; that inconsistency with the WebSocket path is preserved, not introduced, and is called out in actionLogEnabled. Also adds TestConcurrentReloadAndRead, which drives every accessor from four reader goroutines while two writers reload the configuration, and sanitises the member IDs used as test fixtures in config/settings_test.go. Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+10
-11
@@ -17,7 +17,7 @@ func LoadBotNodeConfig(configPath string) error {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
C_botNodeConfig = cfg
|
||||
botNodeConfig.Store(&cfg)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to read bot node config file: %w", err)
|
||||
@@ -27,19 +27,17 @@ func LoadBotNodeConfig(configPath string) error {
|
||||
return fmt.Errorf("failed to parse bot node config file: %w", err)
|
||||
}
|
||||
}
|
||||
C_botNodeConfig = cfg
|
||||
botNodeConfig.Store(&cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NodeStatusNotifyEnabled reports whether the node identified by uuid should
|
||||
// broadcast status-change notifications, per bot_node_config.json.
|
||||
// enableStatusNotify defaults to true: a node notifies unless its entry
|
||||
// explicitly sets the flag to false.
|
||||
// explicitly sets the flag to false. A missing configuration (nil map) yields
|
||||
// the same default.
|
||||
func NodeStatusNotifyEnabled(uuid string) bool {
|
||||
if C_botNodeConfig == nil {
|
||||
return true
|
||||
}
|
||||
opts, ok := C_botNodeConfig[uuid]
|
||||
opts, ok := BotNodes()[uuid]
|
||||
if !ok || opts.EnableStatusNotify == nil {
|
||||
return true
|
||||
}
|
||||
@@ -146,7 +144,7 @@ func LoadGlobalConfig(configPath string) (*Config, error) {
|
||||
cfg.ControllerMessage.ServerExecuteResult = "Command execute result:\nServer Name: {{ serverName }}\nCommand: {{ command }}\n***Result***\n\n{{ result }}\n\n************\nTime: {{ time }}"
|
||||
}
|
||||
|
||||
C_globalConfig = &cfg
|
||||
globalConfig.Store(&cfg)
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -162,7 +160,7 @@ func LoadBotUserConfig(configPath string) (*BotUserConfig, error) {
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse bot user config file: %w", err)
|
||||
}
|
||||
C_botUserConfig = &cfg
|
||||
botUserConfig.Store(&cfg)
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -213,8 +211,9 @@ func SaveBotNodeConfig(configPath string, uuids []string) error {
|
||||
|
||||
// IsDebugMode returns whether debug mode is enabled.
|
||||
func IsDebugMode() bool {
|
||||
if C_globalConfig == nil {
|
||||
cfg := Current()
|
||||
if cfg == nil {
|
||||
return false
|
||||
}
|
||||
return C_globalConfig.System.DebugMode
|
||||
return cfg.System.DebugMode
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user