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