fix concurrent map iteration and map write

This commit is contained in:
akrike
2025-02-05 13:28:28 +08:00
parent 460a1f428b
commit 4fa227ee82
3 changed files with 15 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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()
}
}
}

View File

@@ -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()
}
}
}