From 4addc29b2c71fb2608bf68dd99dd2c6e1e172c8c Mon Sep 17 00:00:00 2001 From: YGXB_net Date: Tue, 28 Apr 2026 14:59:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E6=B7=BB=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=86=99=E5=85=A5=E7=9A=84=E5=B9=B6=E5=8F=91=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E9=94=81=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 sync.Mutex 确保配置访问的线程安全性 - 在 WriteConfig 函数中实现读写锁定机制 - 防止多协程同时修改配置导致的数据竞争问题 --- internal/config/config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 223362d..306a134 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "sync" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" @@ -17,6 +18,7 @@ type Config struct { } var APPConfig Config +var configMu sync.Mutex const ( Url = "url" @@ -70,6 +72,8 @@ func InitConfig() { } func WriteConfig(key string, value any) { + configMu.Lock() viper.Set(key, value) viper.WriteConfig() + configMu.Unlock() }