Skip to content

Commit

Permalink
change server global value to concurrency safety, fix #315
Browse files Browse the repository at this point in the history
  • Loading branch information
ffdfgdfg committed Jan 18, 2020
1 parent b2d1de4 commit b4f72d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
7 changes: 4 additions & 3 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ type Bridge struct {
CloseClient chan int
SecretChan chan *conn.Secret
ipVerify bool
runList map[int]interface{}
runList sync.Map //map[int]interface{}
}

func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList map[int]interface{}) *Bridge {
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList sync.Map) *Bridge {
return &Bridge{
TunnelPort: tunnelPort,
tunnelType: tunnelType,
Expand Down Expand Up @@ -407,7 +407,8 @@ loop:
})
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
v := value.(*file.Tunnel)
if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
//if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
if _, ok := s.runList.Load(v.Id); ok && v.Client.Id == id {
str += v.Remark + common.CONN_DATA_SEQ
}
return true
Expand Down
32 changes: 21 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"

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

var (
Bridge *bridge.Bridge
RunList map[int]interface{}
RunList sync.Map //map[int]interface{}
)

func init() {
RunList = make(map[int]interface{})
RunList = sync.Map{}
}

//init task from db
Expand All @@ -37,7 +38,8 @@ func InitFromCsv() {
if vkey := beego.AppConfig.String("public_vkey"); vkey != "" {
c := file.NewClient(vkey, true, true)
file.GetDb().NewClient(c)
RunList[c.Id] = nil
RunList.Store(c.Id, nil)
//RunList[c.Id] = nil
}
//Initialize services in server-side files
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
Expand Down Expand Up @@ -102,7 +104,8 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
if err := svr.Start(); err != nil {
logs.Error(err)
}
RunList[cnf.Id] = svr
RunList.Store(cnf.Id, svr)
//RunList[cnf.Id] = svr
} else {
logs.Error("Incorrect startup mode %s", cnf.Mode)
}
Expand Down Expand Up @@ -155,7 +158,8 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {

//stop server
func StopServer(id int) error {
if v, ok := RunList[id]; ok {
//if v, ok := RunList[id]; ok {
if v, ok := RunList.Load(id); ok {
if svr, ok := v.(proxy.Service); ok {
if err := svr.Close(); err != nil {
return err
Expand All @@ -170,7 +174,8 @@ func StopServer(id int) error {
t.Status = false
file.GetDb().UpdateTask(t)
}
delete(RunList, id)
//delete(RunList, id)
RunList.Delete(id)
return nil
}
return errors.New("task is not running")
Expand All @@ -180,7 +185,8 @@ func StopServer(id int) error {
func AddTask(t *file.Tunnel) error {
if t.Mode == "secret" || t.Mode == "p2p" {
logs.Info("secret task %s start ", t.Remark)
RunList[t.Id] = nil
//RunList[t.Id] = nil
RunList.Store(t.Id, nil)
return nil
}
if b := tool.TestServerPort(t.Port, t.Mode); !b && t.Mode != "httpHostServer" {
Expand All @@ -192,11 +198,13 @@ func AddTask(t *file.Tunnel) error {
}
if svr := NewMode(Bridge, t); svr != nil {
logs.Info("tunnel task %s start mode:%s port %d", t.Remark, t.Mode, t.Port)
RunList[t.Id] = svr
//RunList[t.Id] = svr
RunList.Store(t.Id, svr)
go func() {
if err := svr.Start(); err != nil {
logs.Error("clientId %d taskId %d start error %s", t.Client.Id, t.Id, err)
delete(RunList, t.Id)
//delete(RunList, t.Id)
RunList.Delete(t.Id)
return
}
}()
Expand All @@ -220,7 +228,8 @@ func StartTask(id int) error {

//delete task
func DelTask(id int) error {
if _, ok := RunList[id]; ok {
//if _, ok := RunList[id]; ok {
if _, ok := RunList.Load(id); ok {
if err := StopServer(id); err != nil {
return err
}
Expand Down Expand Up @@ -250,7 +259,8 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) (
}
if start--; start < 0 {
if length--; length >= 0 {
if _, ok := RunList[v.Id]; ok {
//if _, ok := RunList[v.Id]; ok {
if _, ok := RunList.Load(v.Id); ok {
v.RunStatus = true
} else {
v.RunStatus = false
Expand Down

0 comments on commit b4f72d2

Please sign in to comment.