Skip to content

Commit 12f8aa5

Browse files
fix: 修复并发写入webConfig问题
1 parent c5dfff5 commit 12f8aa5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

service/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package service
22

33
import (
4+
"sync"
5+
46
"github.com/quarkcloudio/quark-go/v3/dal/db"
57
"github.com/quarkcloudio/quark-go/v3/model"
68
)
@@ -9,6 +11,7 @@ type ConfigService struct{}
911

1012
// 存储配置
1113
var webConfig = make(map[string]string)
14+
var mu sync.Mutex
1215

1316
// 初始化
1417
func NewConfigService() *ConfigService {
@@ -19,6 +22,9 @@ func NewConfigService() *ConfigService {
1922
func (p *ConfigService) Refresh() {
2023
configs := []model.Config{}
2124
db.Client.Where("status", 1).Find(&configs)
25+
// 确保对 webConfig map 的写入操作是互斥的,防止并发写入导致的数据竞争
26+
mu.Lock()
27+
defer mu.Unlock()
2228
for _, config := range configs {
2329
webConfig[config.Name] = config.Value
2430
}

0 commit comments

Comments
 (0)