From 1de4bc28736bf04b9388277f2dfa1b5ff1ab6d6d Mon Sep 17 00:00:00 2001
From: Anmol Sethi <hi@nhooyr.io>
Date: Sat, 20 Jul 2019 14:00:32 +0000
Subject: [PATCH] Improve docs and implementation of NetConn

---
 netconn.go | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/netconn.go b/netconn.go
index 2db1f11..9c70b6f 100644
--- a/netconn.go
+++ b/netconn.go
@@ -26,7 +26,8 @@ import (
 // different from most net.Conn implementations where only the
 // reading/writing goroutines are interrupted but the connection is kept alive.
 //
-// The Addr methods will return a mock net.Addr.
+// The Addr methods will return a mock net.Addr that returns "websocket" for Network
+// and "websocket/unknown-addr" for String.
 //
 // A received StatusNormalClosure close frame will be translated to EOF when reading.
 func NetConn(c *Conn) net.Conn {
@@ -37,11 +38,15 @@ func NetConn(c *Conn) net.Conn {
 	var cancel context.CancelFunc
 	nc.writeContext, cancel = context.WithCancel(context.Background())
 	nc.writeTimer = time.AfterFunc(math.MaxInt64, cancel)
-	nc.writeTimer.Stop()
+	if !nc.writeTimer.Stop() {
+		<-nc.writeTimer.C
+	}
 
 	nc.readContext, cancel = context.WithCancel(context.Background())
 	nc.readTimer = time.AfterFunc(math.MaxInt64, cancel)
-	nc.readTimer.Stop()
+	if !nc.readTimer.Stop() {
+		<-nc.readTimer.C
+	}
 
 	return nc
 }
@@ -103,23 +108,23 @@ func (c *netConn) Read(p []byte) (int, error) {
 	return n, err
 }
 
-type unknownAddr struct {
+type websocketAddr struct {
 }
 
-func (a unknownAddr) Network() string {
-	return "unknown"
+func (a websocketAddr) Network() string {
+	return "websocket"
 }
 
-func (a unknownAddr) String() string {
-	return "unknown"
+func (a websocketAddr) String() string {
+	return "websocket/unknown-addr"
 }
 
 func (c *netConn) RemoteAddr() net.Addr {
-	return unknownAddr{}
+	return websocketAddr{}
 }
 
 func (c *netConn) LocalAddr() net.Addr {
-	return unknownAddr{}
+	return websocketAddr{}
 }
 
 func (c *netConn) SetDeadline(t time.Time) error {
-- 
GitLab