[fix] package connection: 先关闭net.Close再发送通知消息至channel

pull/3/head
q191201771 4 years ago
parent bfe988cab4
commit 390e278435

@ -37,15 +37,19 @@ var (
)
type Connection interface {
// 包含interface net.Conn的所有方法
// Read
// Write
// Close
// LocalAddr
// RemoteAddr
// SetDeadline
// SetReadDeadline
// SetWriteDeadline
// Conn 包含net.Conn interface的所有方法
//
// Read(b []byte) (n int, err error)
// Write(b []byte) (n int, err error)
// // Close 允许调用多次
// //
// Close() error
// LocalAddr() net.Addr
// RemoteAddr() net.Addr
// SetDeadline(t time.Time) error
// SetReadDeadline(t time.Time) error
// SetWriteDeadline(t time.Time) error
//
net.Conn
ReadAtLeast(buf []byte, min int) (n int, err error)
@ -56,8 +60,14 @@ type Connection interface {
// 一般在Close前想要将剩余数据发送完毕时调用
Flush() error
// 阻塞直到连接关闭或发生错误
// Done 阻塞直到连接关闭或发生错误
//
// 注意向上层严格保证消息发送后后续ReadWrite等调用都将失败
//
// 注意,向上层严格保证,消息只发送一次
//
// @return 返回nil则是本端主动调用Close关闭
//
Done() <-chan error
// TODO chef: 这几个接口是否不提供
@ -420,9 +430,12 @@ func (c *connection) close(err error) {
if c.option.WriteChanSize > 0 {
c.exitChan <- struct{}{}
}
c.doneChan <- err
// 注意先Close后再发送消息保证消息发送前已经Close掉了
_ = c.Conn.Close()
// 如果使用了wChan并不关闭它避免竞态条件下connection继续使用它造成问题。让它随connection对象释放。
c.doneChan <- err
// 注意如果使用了wChan并不关闭它避免竞态条件下connection继续使用它造成问题。让它随connection对象释放。
})
}

Loading…
Cancel
Save