Skip to content

Commit 1de4bc2

Browse files
committed
Improve docs and implementation of NetConn
1 parent e2d44e3 commit 1de4bc2

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

netconn.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
// different from most net.Conn implementations where only the
2727
// reading/writing goroutines are interrupted but the connection is kept alive.
2828
//
29-
// The Addr methods will return a mock net.Addr.
29+
// The Addr methods will return a mock net.Addr that returns "websocket" for Network
30+
// and "websocket/unknown-addr" for String.
3031
//
3132
// A received StatusNormalClosure close frame will be translated to EOF when reading.
3233
func NetConn(c *Conn) net.Conn {
@@ -37,11 +38,15 @@ func NetConn(c *Conn) net.Conn {
3738
var cancel context.CancelFunc
3839
nc.writeContext, cancel = context.WithCancel(context.Background())
3940
nc.writeTimer = time.AfterFunc(math.MaxInt64, cancel)
40-
nc.writeTimer.Stop()
41+
if !nc.writeTimer.Stop() {
42+
<-nc.writeTimer.C
43+
}
4144

4245
nc.readContext, cancel = context.WithCancel(context.Background())
4346
nc.readTimer = time.AfterFunc(math.MaxInt64, cancel)
44-
nc.readTimer.Stop()
47+
if !nc.readTimer.Stop() {
48+
<-nc.readTimer.C
49+
}
4550

4651
return nc
4752
}
@@ -103,23 +108,23 @@ func (c *netConn) Read(p []byte) (int, error) {
103108
return n, err
104109
}
105110

106-
type unknownAddr struct {
111+
type websocketAddr struct {
107112
}
108113

109-
func (a unknownAddr) Network() string {
110-
return "unknown"
114+
func (a websocketAddr) Network() string {
115+
return "websocket"
111116
}
112117

113-
func (a unknownAddr) String() string {
114-
return "unknown"
118+
func (a websocketAddr) String() string {
119+
return "websocket/unknown-addr"
115120
}
116121

117122
func (c *netConn) RemoteAddr() net.Addr {
118-
return unknownAddr{}
123+
return websocketAddr{}
119124
}
120125

121126
func (c *netConn) LocalAddr() net.Addr {
122-
return unknownAddr{}
127+
return websocketAddr{}
123128
}
124129

125130
func (c *netConn) SetDeadline(t time.Time) error {

0 commit comments

Comments
 (0)