Be fix goroutine leak (#252)

* fix(gateway):fix goroutine leak

* fix(term):remove useless code

* fix(be):add pprof for debug mode
This commit is contained in:
1mtrue
2022-05-05 17:44:05 +08:00
committed by GitHub
parent 90751bacfc
commit 9e44b25b87
5 changed files with 95 additions and 66 deletions

View File

@ -1,7 +1,13 @@
package gateway
import (
"sync"
"next-terminal/server/log"
)
type Manager struct {
gateways map[string]*Gateway
gateways *sync.Map
Add chan *Gateway
Del chan string
@ -11,7 +17,7 @@ func NewManager() *Manager {
return &Manager{
Add: make(chan *Gateway),
Del: make(chan string),
gateways: map[string]*Gateway{},
gateways: new(sync.Map),
}
}
@ -19,19 +25,26 @@ func (m *Manager) Start() {
for {
select {
case g := <-m.Add:
m.gateways[g.ID] = g
m.gateways.Store(g.ID, g)
log.Info("add gateway: %s", g.ID)
go g.Run()
case k := <-m.Del:
if _, ok := m.gateways[k]; ok {
m.gateways[k].Close()
delete(m.gateways, k)
if val, ok := m.gateways.Load(k); ok {
if vv, vok := val.(*Gateway); vok {
vv.Close()
m.gateways.Delete(k)
}
}
}
}
}
func (m Manager) GetById(id string) *Gateway {
return m.gateways[id]
if val, ok := m.gateways.Load(id); ok {
return val.(*Gateway)
}
return nil
}
var GlobalGatewayManager *Manager