Skip to content

Commit

Permalink
Merge pull request #318 from aceld/feature/aceld
Browse files Browse the repository at this point in the history
fix bug: [Panic]: send on closed channel
  • Loading branch information
GStones authored Apr 24, 2024
2 parents 80de41f + 7b4ca6d commit a7a32fd
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions znet/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ type Connection struct {
// (保护当前property的锁)
propertyLock sync.Mutex

// The current connection's close state
// (当前连接的关闭状态)
closed int32

// Which Connection Manager the current connection belongs to
// (当前链接是属于哪个Connection Manager的)
connManager ziface.IConnManager
Expand Down Expand Up @@ -127,7 +123,6 @@ func newServerConn(server ziface.IServer, conn net.Conn, connID uint64) ziface.I
conn: conn,
connID: connID,
connIdStr: strconv.FormatUint(connID, 10),
closed: 0,
startWriterFlag: 0,
msgBuffChan: nil,
property: nil,
Expand Down Expand Up @@ -165,7 +160,6 @@ func newClientConn(client ziface.IClient, conn net.Conn) ziface.IConnection {
conn: conn,
connID: 0, // client ignore
connIdStr: "", // client ignore
closed: 0,
startWriterFlag: 0,
msgBuffChan: nil,
property: nil,
Expand Down Expand Up @@ -393,10 +387,15 @@ func (c *Connection) SendToQueue(data []byte) error {

// Send timeout
select {
case <-idleTimeout.C:
return errors.New("send buff msg timeout")
case c.msgBuffChan <- data:
return nil
case <-c.ctx.Done():
return errors.New("connection closed when send buff msg")
default:
select {
case <-idleTimeout.C:
return errors.New("send buff msg timeout")
case c.msgBuffChan <- data:
return nil
}
}
}

Expand Down Expand Up @@ -466,16 +465,6 @@ func (c *Connection) Context() context.Context {
}

func (c *Connection) finalizer() {
// If the connection has already been closed
if c.isClosed() == true {
return
}

//set closed
if !c.setClose() {
return
}

// Call the callback function registered by the user when closing the connection if it exists
// (如果用户注册了该链接的 关闭回调业务,那么在此刻应该显示调用)
c.callOnConnStop()
Expand Down Expand Up @@ -560,11 +549,7 @@ func (c *Connection) GetMsgHandler() ziface.IMsgHandle {
}

func (c *Connection) isClosed() bool {
return atomic.LoadInt32(&c.closed) != 0
}

func (c *Connection) setClose() bool {
return atomic.CompareAndSwapInt32(&c.closed, 0, 1)
return c.ctx.Err() != nil
}

func (c *Connection) setStartWriterFlag() bool {
Expand Down

0 comments on commit a7a32fd

Please sign in to comment.