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

Loading…
Cancel
Save