Skip to content

Commit

Permalink
完善session关闭和发送的互斥逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Nov 1, 2016
1 parent 9c7e4a3 commit 2a02e40
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (session *Session) sendLoop() {
}

func (session *Session) Send(msg interface{}) error {
if session.IsClosed() {
return SessionClosedError
}

if session.sendChan == nil {
if session.IsClosed() {
return SessionClosedError
}

err := session.codec.Send(msg)
if err != nil {
session.Close()
Expand All @@ -116,13 +116,14 @@ func (session *Session) Send(msg interface{}) error {
}

session.sendMutex.RLock()
if session.IsClosed() {
return SessionClosedError
}

select {
case session.sendChan <- msg:
session.sendMutex.RUnlock()
return nil
case <-session.closeChan:
session.sendMutex.RUnlock()
return SessionClosedError
default:
session.sendMutex.RUnlock()
session.Close()
Expand Down

0 comments on commit 2a02e40

Please sign in to comment.