Close session when one connection drops

This commit is contained in:
Andy Wang 2019-10-16 23:00:23 +01:00
parent 34a4805c34
commit 57f0c3d20a

View File

@ -54,20 +54,6 @@ func (sb *switchboard) addConn(conn net.Conn) {
go sb.deplex(connId, conn) go sb.deplex(connId, conn)
} }
func (sb *switchboard) removeConn(connId uint32) {
sb.connsM.Lock()
delete(sb.conns, connId)
remaining := len(sb.conns)
sb.connsM.Unlock()
if remaining == 0 {
atomic.StoreUint32(&sb.broken, 1)
if !sb.session.IsClosed() {
sb.session.SetTerminalMsg("no underlying connection left")
sb.session.passiveClose()
}
}
}
// a pointer to connId is passed here so that the switchboard can reassign it // a pointer to connId is passed here so that the switchboard can reassign it
func (sb *switchboard) send(data []byte, connId *uint32) (n int, err error) { func (sb *switchboard) send(data []byte, connId *uint32) (n int, err error) {
sb.Valve.txWait(len(data)) sb.Valve.txWait(len(data))
@ -148,9 +134,6 @@ func (sb *switchboard) assignRandomConn() (uint32, error) {
// actively triggered by session.Close() // actively triggered by session.Close()
func (sb *switchboard) closeAll() { func (sb *switchboard) closeAll() {
if atomic.SwapUint32(&sb.broken, 1) == 1 {
return
}
sb.connsM.Lock() sb.connsM.Lock()
for key, conn := range sb.conns { for key, conn := range sb.conns {
conn.Close() conn.Close()
@ -169,7 +152,11 @@ func (sb *switchboard) deplex(connId uint32, conn net.Conn) {
if err != nil { if err != nil {
log.Debugf("a connection for session %v has closed: %v", sb.session.id, err) log.Debugf("a connection for session %v has closed: %v", sb.session.id, err)
go conn.Close() go conn.Close()
sb.removeConn(connId) atomic.StoreUint32(&sb.broken, 1)
if !sb.session.IsClosed() {
sb.session.SetTerminalMsg("a connection has dropped unexpectedly")
sb.session.passiveClose()
}
return return
} }