From 84fb0849d3dbb4ff58691893fa5a60ce326c1a4d Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Thu, 26 Sep 2019 08:34:48 +0200 Subject: [PATCH 1/2] Small code changes --- conn.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/conn.go b/conn.go index 3d7d574e..60ec8af8 100644 --- a/conn.go +++ b/conn.go @@ -19,6 +19,10 @@ import ( "time" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + // Conn represents a WebSocket connection. // All methods may be called concurrently except for Reader, Read // and SetReadLimit. @@ -70,7 +74,7 @@ type Conn struct { activeReader *messageReader // readFrameLock is acquired to read from bw. readFrameLock chan struct{} - readClosed int64 + isReadClosed int64 readHeaderBuf []byte controlPayloadBuf []byte @@ -341,7 +345,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error { // See https://github.com/nhooyr/websocket/issues/87#issue-451703332 // Most users should not need this. func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) { - if atomic.LoadInt64(&c.readClosed) == 1 { + if atomic.LoadInt64(&c.isReadClosed) == 1 { return 0, nil, fmt.Errorf("websocket connection read closed") } @@ -685,9 +689,8 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte case <-c.closed: return n, c.closeErr case c.setWriteTimeout <- context.Background(): + return n, nil } - - return n, nil } func (c *Conn) realWriteFrame(ctx context.Context, h header, p []byte) (n int, err error) { @@ -838,10 +841,6 @@ func (c *Conn) writeClose(p []byte, cerr error) error { return nil } -func init() { - rand.Seed(time.Now().UnixNano()) -} - // Ping sends a ping to the peer and waits for a pong. // Use this to measure latency or ensure the peer is responsive. // Ping must be called concurrently with Reader as it does From a348854113bb94890eac53e10e5f4c76d604bdea Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Thu, 26 Sep 2019 08:35:42 +0200 Subject: [PATCH 2/2] add forgotten file --- conn_common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn_common.go b/conn_common.go index ae0fe554..546f5aa5 100644 --- a/conn_common.go +++ b/conn_common.go @@ -178,7 +178,7 @@ func (c *netConn) SetReadDeadline(t time.Time) error { // Use this when you do not want to read data messages from the connection anymore but will // want to write messages to it. func (c *Conn) CloseRead(ctx context.Context) context.Context { - atomic.StoreInt64(&c.readClosed, 1) + atomic.StoreInt64(&c.isReadClosed, 1) ctx, cancel := context.WithCancel(ctx) go func() {