Skip to content

Commit b4f72d2

Browse files
committed
change server global value to concurrency safety, fix ehang-io#315
1 parent b2d1de4 commit b4f72d2

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

bridge/bridge.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ type Bridge struct {
5050
CloseClient chan int
5151
SecretChan chan *conn.Secret
5252
ipVerify bool
53-
runList map[int]interface{}
53+
runList sync.Map //map[int]interface{}
5454
}
5555

56-
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList map[int]interface{}) *Bridge {
56+
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList sync.Map) *Bridge {
5757
return &Bridge{
5858
TunnelPort: tunnelPort,
5959
tunnelType: tunnelType,
@@ -407,7 +407,8 @@ loop:
407407
})
408408
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
409409
v := value.(*file.Tunnel)
410-
if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
410+
//if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
411+
if _, ok := s.runList.Load(v.Id); ok && v.Client.Id == id {
411412
str += v.Remark + common.CONN_DATA_SEQ
412413
}
413414
return true

server/server.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"strconv"
99
"strings"
10+
"sync"
1011
"time"
1112

1213
"ehang.io/nps/bridge"
@@ -24,11 +25,11 @@ import (
2425

2526
var (
2627
Bridge *bridge.Bridge
27-
RunList map[int]interface{}
28+
RunList sync.Map //map[int]interface{}
2829
)
2930

3031
func init() {
31-
RunList = make(map[int]interface{})
32+
RunList = sync.Map{}
3233
}
3334

3435
//init task from db
@@ -37,7 +38,8 @@ func InitFromCsv() {
3738
if vkey := beego.AppConfig.String("public_vkey"); vkey != "" {
3839
c := file.NewClient(vkey, true, true)
3940
file.GetDb().NewClient(c)
40-
RunList[c.Id] = nil
41+
RunList.Store(c.Id, nil)
42+
//RunList[c.Id] = nil
4143
}
4244
//Initialize services in server-side files
4345
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
@@ -102,7 +104,8 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
102104
if err := svr.Start(); err != nil {
103105
logs.Error(err)
104106
}
105-
RunList[cnf.Id] = svr
107+
RunList.Store(cnf.Id, svr)
108+
//RunList[cnf.Id] = svr
106109
} else {
107110
logs.Error("Incorrect startup mode %s", cnf.Mode)
108111
}
@@ -155,7 +158,8 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
155158

156159
//stop server
157160
func StopServer(id int) error {
158-
if v, ok := RunList[id]; ok {
161+
//if v, ok := RunList[id]; ok {
162+
if v, ok := RunList.Load(id); ok {
159163
if svr, ok := v.(proxy.Service); ok {
160164
if err := svr.Close(); err != nil {
161165
return err
@@ -170,7 +174,8 @@ func StopServer(id int) error {
170174
t.Status = false
171175
file.GetDb().UpdateTask(t)
172176
}
173-
delete(RunList, id)
177+
//delete(RunList, id)
178+
RunList.Delete(id)
174179
return nil
175180
}
176181
return errors.New("task is not running")
@@ -180,7 +185,8 @@ func StopServer(id int) error {
180185
func AddTask(t *file.Tunnel) error {
181186
if t.Mode == "secret" || t.Mode == "p2p" {
182187
logs.Info("secret task %s start ", t.Remark)
183-
RunList[t.Id] = nil
188+
//RunList[t.Id] = nil
189+
RunList.Store(t.Id, nil)
184190
return nil
185191
}
186192
if b := tool.TestServerPort(t.Port, t.Mode); !b && t.Mode != "httpHostServer" {
@@ -192,11 +198,13 @@ func AddTask(t *file.Tunnel) error {
192198
}
193199
if svr := NewMode(Bridge, t); svr != nil {
194200
logs.Info("tunnel task %s start mode:%s port %d", t.Remark, t.Mode, t.Port)
195-
RunList[t.Id] = svr
201+
//RunList[t.Id] = svr
202+
RunList.Store(t.Id, svr)
196203
go func() {
197204
if err := svr.Start(); err != nil {
198205
logs.Error("clientId %d taskId %d start error %s", t.Client.Id, t.Id, err)
199-
delete(RunList, t.Id)
206+
//delete(RunList, t.Id)
207+
RunList.Delete(t.Id)
200208
return
201209
}
202210
}()
@@ -220,7 +228,8 @@ func StartTask(id int) error {
220228

221229
//delete task
222230
func DelTask(id int) error {
223-
if _, ok := RunList[id]; ok {
231+
//if _, ok := RunList[id]; ok {
232+
if _, ok := RunList.Load(id); ok {
224233
if err := StopServer(id); err != nil {
225234
return err
226235
}
@@ -250,7 +259,8 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) (
250259
}
251260
if start--; start < 0 {
252261
if length--; length >= 0 {
253-
if _, ok := RunList[v.Id]; ok {
262+
//if _, ok := RunList[v.Id]; ok {
263+
if _, ok := RunList.Load(v.Id); ok {
254264
v.RunStatus = true
255265
} else {
256266
v.RunStatus = false

0 commit comments

Comments
 (0)