mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-05 07:56:50 +08:00
fix concurrent map iteration and map write
This commit is contained in:
@@ -49,6 +49,7 @@ type ProcessBase struct {
|
|||||||
changControlTime time.Time
|
changControlTime time.Time
|
||||||
}
|
}
|
||||||
ws map[string]ConnectInstance
|
ws map[string]ConnectInstance
|
||||||
|
wsLock sync.Mutex
|
||||||
Config struct {
|
Config struct {
|
||||||
AutoRestart bool
|
AutoRestart bool
|
||||||
compulsoryRestart bool
|
compulsoryRestart bool
|
||||||
@@ -178,10 +179,14 @@ func (p *ProcessBase) AddConn(user string, c ConnectInstance) {
|
|||||||
log.Logger.Error("已存在连接")
|
log.Logger.Error("已存在连接")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
p.wsLock.Lock()
|
||||||
|
defer p.wsLock.Unlock()
|
||||||
p.ws[user] = c
|
p.ws[user] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProcessBase) DeleteConn(user string) {
|
func (p *ProcessBase) DeleteConn(user string) {
|
||||||
|
p.wsLock.Lock()
|
||||||
|
defer p.wsLock.Unlock()
|
||||||
delete(p.ws, user)
|
delete(p.ws, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -103,9 +103,14 @@ func (p *ProcessPty) readInit() {
|
|||||||
{
|
{
|
||||||
n, _ := p.pty.Read(buf)
|
n, _ := p.pty.Read(buf)
|
||||||
p.bufHanle(buf[:n])
|
p.bufHanle(buf[:n])
|
||||||
|
if len(p.ws) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
p.wsLock.Lock()
|
||||||
for _, v := range p.ws {
|
for _, v := range p.ws {
|
||||||
v.Write(buf[:n])
|
v.Write(buf[:n])
|
||||||
}
|
}
|
||||||
|
p.wsLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -109,9 +109,14 @@ func (p *ProcessStd) readInit() {
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
output = p.Read()
|
output = p.Read()
|
||||||
|
if len(p.ws) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
p.wsLock.Lock()
|
||||||
for _, v := range p.ws {
|
for _, v := range p.ws {
|
||||||
v.WriteString(output)
|
v.WriteString(output)
|
||||||
}
|
}
|
||||||
|
p.wsLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user