Skip to content

Commit b284887

Browse files
committed
refactor:sync.map replace map
1 parent 750149d commit b284887

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

controllers/conn_controller.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ func (con connController) List(c *gin.Context) {
3030
}
3131
conns := make([]ConnItem, 0)
3232

33-
for k, v := range global.GlobalConf.RedisServices {
33+
global.GlobalConf.RedisServices.Range(func(k, v interface{}) bool {
3434
conns = append(conns, ConnItem{
35-
Key: k,
36-
Name: v.RedisService,
35+
Key: k.(string),
36+
Name: v.(global.RedisService).RedisService,
3737
})
38-
}
38+
return true
39+
})
40+
3941
con.Success(c, http.StatusOK, conns)
4042
}
4143

@@ -52,7 +54,7 @@ func (con connController) Add(c *gin.Context) {
5254
m.Write([]byte(conf.ServiceName))
5355
key := hex.EncodeToString(m.Sum(nil))
5456

55-
global.GlobalConf.RedisServices[key] = global.RedisService{
57+
tmp := global.RedisService{
5658
RedisService: conf.ServiceName,
5759
Config: model.RedisConfig{
5860
Addr: net.JoinHostPort(conf.Host, conf.Port),
@@ -67,6 +69,14 @@ func (con connController) Add(c *gin.Context) {
6769
SshPassword: conf.SshPassword,
6870
},
6971
}
72+
global.GlobalConf.RedisServices.Store(key, tmp)
73+
74+
tmpRedisServicesCp := make(map[string]global.RedisService)
75+
global.GlobalConf.RedisServices.Range(func(k, v interface{}) bool {
76+
tmpRedisServicesCp[k.(string)] = v.(global.RedisService)
77+
return true
78+
})
79+
global.GlobalConf.RedisServicesCp = tmpRedisServicesCp
7080

7181
//存储
7282
buffer := &bytes.Buffer{}
@@ -94,7 +104,14 @@ func (con connController) Del(c *gin.Context) {
94104
return
95105
}
96106

97-
delete(global.GlobalConf.RedisServices, req.ServiceName)
107+
global.GlobalConf.RedisServices.Delete(req.ServiceName)
108+
109+
tmpRedisServicesCp := make(map[string]global.RedisService)
110+
global.GlobalConf.RedisServices.Range(func(k, v interface{}) bool {
111+
tmpRedisServicesCp[k.(string)] = v.(global.RedisService)
112+
return true
113+
})
114+
global.GlobalConf.RedisServicesCp = tmpRedisServicesCp
98115

99116
//存储
100117
buffer := &bytes.Buffer{}

controllers/index_controller.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func (con indexController) Open(c *gin.Context) {
4242
}
4343

4444
keys := strings.Split(req.Index, "_")
45-
redisServer := global.GlobalConf.RedisServices[keys[1]]
46-
client, err := service.NewRedisClient(redisServer)
45+
redisServer, _ := global.GlobalConf.RedisServices.Load(keys[1])
46+
client, err := service.NewRedisClient(redisServer.(global.RedisService))
4747
if err != nil {
4848
con.Error(c, err.Error())
4949
return
5050
}
5151
global.GlobalClients[keys[1]] = client
52-
global.GlobalConf.RedisServices[keys[1]] = redisServer
52+
global.GlobalConf.RedisServices.Store(keys[1], redisServer)
5353

5454
val, _ := c.Get("username")
5555
ctx := context.WithValue(context.Background(), "username", val)
@@ -298,9 +298,12 @@ func (con indexController) SerInfo(c *gin.Context) {
298298
return
299299
}
300300

301-
redisServer := global.GlobalConf.RedisServices[req.Key]
302-
303-
client, err := service.NewRedisClient(redisServer)
301+
redisServer, ok := global.GlobalConf.RedisServices.Load(req.Key)
302+
if !ok {
303+
fmt.Println(ok)
304+
}
305+
redisServers := redisServer.(global.RedisService)
306+
client, err := service.NewRedisClient(redisServers)
304307
if err != nil {
305308
con.Error(c, err.Error())
306309
return
@@ -319,7 +322,8 @@ func (con indexController) SerInfo(c *gin.Context) {
319322

320323
con.Success(c, http.StatusOK, gin.H{
321324
"info": info,
322-
"name": redisServer.RedisService,
325+
"time": time.Now().Format("15:04:05"),
326+
"name": redisServers.RedisService,
323327
})
324328
}
325329

controllers/ws_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func (con wsController) Ws(c *gin.Context) {
118118
var client *redis.Client
119119
client = global.GlobalClients[cmd.Sk]
120120
if client == nil {
121-
redisServer := global.GlobalConf.RedisServices[cmd.Sk]
122-
client, err = service.NewRedisClient(redisServer)
121+
redisServer, _ := global.GlobalConf.RedisServices.Load(cmd.Sk)
122+
client, err = service.NewRedisClient(redisServer.(global.RedisService))
123123
if err != nil {
124124
err = ws.WriteMessage(mt, ReturnResp(err.Error(), 0, uint8(cmd.Db)))
125125
if err != nil {
@@ -129,7 +129,7 @@ func (con wsController) Ws(c *gin.Context) {
129129
goto LOOP
130130
}
131131
global.GlobalClients[cmd.Sk] = client
132-
global.GlobalConf.RedisServices[cmd.Sk] = redisServer
132+
global.GlobalConf.RedisServices.Store(cmd.Sk, redisServer)
133133
}
134134
err = client.Do(ctx, "select", cmd.Db).Err()
135135
if err != nil {

global/default.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ import (
1313
"grm/model"
1414
"io"
1515
"os"
16+
"sync"
1617

1718
"github.com/go-redis/redis"
1819
)
1920

2021
var GlobalClients map[string]*redis.Client
2122

2223
type GlobalConfig struct {
23-
Accounts map[string]string
24-
RedisServices map[string]RedisService
25-
Separator string
26-
Tree bool
24+
Accounts map[string]string
25+
RedisServicesCp map[string]RedisService
26+
RedisServices sync.Map
27+
Separator string
28+
Tree bool
2729
}
2830

2931
type RedisService struct {
@@ -35,7 +37,7 @@ type RedisService struct {
3537

3638
var GlobalConf = GlobalConfig{
3739
Accounts: make(map[string]string),
38-
RedisServices: make(map[string]RedisService),
40+
RedisServices: sync.Map{},
3941
Separator: ":",
4042
Tree: true,
4143
}
@@ -52,6 +54,11 @@ func init() {
5254

5355
decoder := json.NewDecoder(bytes.NewBuffer(data))
5456
err = decoder.Decode(&GlobalConf)
57+
58+
for k, v := range GlobalConf.RedisServicesCp {
59+
GlobalConf.RedisServices.Store(k, v)
60+
}
61+
5562
if err != nil && err != io.EOF {
5663
fmt.Println(err)
5764
os.Exit(0)

0 commit comments

Comments
 (0)