Use Compare-And-Swap for atomic booleans indicating session and switchboard closed

This commit is contained in:
Andy Wang 2020-12-25 23:16:57 +00:00
parent 4f34e69006
commit 2f17841f85
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
2 changed files with 21 additions and 25 deletions

View File

@ -66,6 +66,8 @@ type Session struct {
streamsM sync.Mutex streamsM sync.Mutex
streams map[uint32]*Stream streams map[uint32]*Stream
// For accepting new streams
acceptCh chan *Stream
// a pool of heap allocated frame objects so we don't have to allocate a new one each time we receive a frame // a pool of heap allocated frame objects so we don't have to allocate a new one each time we receive a frame
recvFramePool sync.Pool recvFramePool sync.Pool
@ -78,9 +80,6 @@ type Session struct {
// Used for LocalAddr() and RemoteAddr() etc. // Used for LocalAddr() and RemoteAddr() etc.
addrs atomic.Value addrs atomic.Value
// For accepting new streams
acceptCh chan *Stream
closed uint32 closed uint32
terminalMsg atomic.Value terminalMsg atomic.Value
@ -181,7 +180,7 @@ func (sesh *Session) Accept() (net.Conn, error) {
} }
func (sesh *Session) closeStream(s *Stream, active bool) error { func (sesh *Session) closeStream(s *Stream, active bool) error {
if atomic.SwapUint32(&s.closed, 1) == 1 { if !atomic.CompareAndSwapUint32(&s.closed, 0, 1) {
return fmt.Errorf("closing stream %v: %w", s.id, errRepeatStreamClosing) return fmt.Errorf("closing stream %v: %w", s.id, errRepeatStreamClosing)
} }
_ = s.getRecvBuf().Close() // recvBuf.Close should not return error _ = s.getRecvBuf().Close() // recvBuf.Close should not return error
@ -244,6 +243,10 @@ func (sesh *Session) recvDataFromRemote(data []byte) error {
} }
sesh.streamsM.Lock() sesh.streamsM.Lock()
if sesh.IsClosed() {
sesh.streamsM.Unlock()
return ErrBrokenSession
}
existingStream, existing := sesh.streams[frame.StreamID] existingStream, existing := sesh.streams[frame.StreamID]
if existing { if existing {
sesh.streamsM.Unlock() sesh.streamsM.Unlock()
@ -255,10 +258,10 @@ func (sesh *Session) recvDataFromRemote(data []byte) error {
} else { } else {
newStream := makeStream(sesh, frame.StreamID) newStream := makeStream(sesh, frame.StreamID)
sesh.streams[frame.StreamID] = newStream sesh.streams[frame.StreamID] = newStream
sesh.acceptCh <- newStream
sesh.streamsM.Unlock() sesh.streamsM.Unlock()
// new stream // new stream
sesh.streamCountIncr() sesh.streamCountIncr()
sesh.acceptCh <- newStream
return newStream.recvFrame(frame) return newStream.recvFrame(frame)
} }
} }
@ -276,14 +279,14 @@ func (sesh *Session) TerminalMsg() string {
} }
} }
func (sesh *Session) closeSession(closeSwitchboard bool) error { func (sesh *Session) closeSession() error {
if atomic.SwapUint32(&sesh.closed, 1) == 1 { if !atomic.CompareAndSwapUint32(&sesh.closed, 0, 1) {
log.Debugf("session %v has already been closed", sesh.id) log.Debugf("session %v has already been closed", sesh.id)
return errRepeatSessionClosing return errRepeatSessionClosing
} }
sesh.acceptCh <- nil
sesh.streamsM.Lock() sesh.streamsM.Lock()
close(sesh.acceptCh)
for id, stream := range sesh.streams { for id, stream := range sesh.streams {
if stream == nil { if stream == nil {
continue continue
@ -294,26 +297,23 @@ func (sesh *Session) closeSession(closeSwitchboard bool) error {
sesh.streamCountDecr() sesh.streamCountDecr()
} }
sesh.streamsM.Unlock() sesh.streamsM.Unlock()
if closeSwitchboard {
sesh.sb.closeAll()
}
return nil return nil
} }
func (sesh *Session) passiveClose() error { func (sesh *Session) passiveClose() error {
log.Debugf("attempting to passively close session %v", sesh.id) log.Debugf("attempting to passively close session %v", sesh.id)
err := sesh.closeSession(true) err := sesh.closeSession()
if err != nil { if err != nil {
return err return err
} }
sesh.sb.closeAll()
log.Debugf("session %v closed gracefully", sesh.id) log.Debugf("session %v closed gracefully", sesh.id)
return nil return nil
} }
func (sesh *Session) Close() error { func (sesh *Session) Close() error {
log.Debugf("attempting to actively close session %v", sesh.id) log.Debugf("attempting to actively close session %v", sesh.id)
err := sesh.closeSession(false) err := sesh.closeSession()
if err != nil { if err != nil {
return err return err
} }
@ -339,7 +339,6 @@ func (sesh *Session) Close() error {
if err != nil { if err != nil {
return err return err
} }
sesh.sb.closeAll() sesh.sb.closeAll()
log.Debugf("session %v closed gracefully", sesh.id) log.Debugf("session %v closed gracefully", sesh.id)
return nil return nil

View File

@ -71,7 +71,8 @@ func (sb *switchboard) send(data []byte, connId *uint32) (n int, err error) {
n, err = conn.Write(d) n, err = conn.Write(d)
if err != nil { if err != nil {
sb.conns.Delete(*connId) sb.conns.Delete(*connId)
sb.close("failed to write to remote " + err.Error()) sb.session.SetTerminalMsg("failed to write to remote " + err.Error())
sb.session.passiveClose()
return n, err return n, err
} }
sb.valve.AddTx(int64(n)) sb.valve.AddTx(int64(n))
@ -138,16 +139,11 @@ func (sb *switchboard) pickRandConn() (uint32, net.Conn, error) {
return id, conn, nil return id, conn, nil
} }
func (sb *switchboard) close(terminalMsg string) {
atomic.StoreUint32(&sb.broken, 1)
if !sb.session.IsClosed() {
sb.session.SetTerminalMsg(terminalMsg)
sb.session.passiveClose()
}
}
// actively triggered by session.Close() // actively triggered by session.Close()
func (sb *switchboard) closeAll() { func (sb *switchboard) closeAll() {
if !atomic.CompareAndSwapUint32(&sb.broken, 0, 1) {
return
}
sb.conns.Range(func(key, connI interface{}) bool { sb.conns.Range(func(key, connI interface{}) bool {
conn := connI.(net.Conn) conn := connI.(net.Conn)
conn.Close() conn.Close()
@ -168,7 +164,8 @@ func (sb *switchboard) deplex(connId uint32, conn net.Conn) {
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)
sb.conns.Delete(connId) sb.conns.Delete(connId)
atomic.AddUint32(&sb.numConns, ^uint32(0)) atomic.AddUint32(&sb.numConns, ^uint32(0))
sb.close("a connection has dropped unexpectedly") sb.session.SetTerminalMsg("a connection has dropped unexpectedly")
sb.session.passiveClose()
return return
} }