fix: only close user/agentIo connect channel once (#21)

This commit is contained in:
UUBulb 2024-11-26 09:18:13 +08:00 committed by GitHub
parent 45f11483ec
commit 07989705d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 4 deletions

View File

@ -95,5 +95,9 @@ func fmStream(c *gin.Context) (any, error) {
return nil, newWsError("%v", err)
}
return nil, newWsError("%v", rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10))
if err = rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10); err != nil {
return nil, newWsError("%v", err)
}
return nil, newWsError("")
}

View File

@ -95,5 +95,9 @@ func terminalStream(c *gin.Context) (any, error) {
return nil, newWsError("%v", err)
}
return nil, newWsError("%v", rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10))
if err = rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10); err != nil {
return nil, newWsError("%v", err)
}
return nil, newWsError("")
}

View File

@ -15,6 +15,8 @@ type ioStreamContext struct {
agentIo io.ReadWriteCloser
userIoConnectCh chan struct{}
agentIoConnectCh chan struct{}
userIoChOnce sync.Once
agentIoChOnce sync.Once
}
type bp struct {
@ -74,7 +76,9 @@ func (s *NezhaHandler) UserConnected(streamId string, userIo io.ReadWriteCloser)
}
stream.userIo = userIo
close(stream.userIoConnectCh)
stream.userIoChOnce.Do(func() {
close(stream.userIoConnectCh)
})
return nil
}
@ -86,7 +90,9 @@ func (s *NezhaHandler) AgentConnected(streamId string, agentIo io.ReadWriteClose
}
stream.agentIo = agentIo
close(stream.agentIoConnectCh)
stream.agentIoChOnce.Do(func() {
close(stream.agentIoConnectCh)
})
return nil
}