@@ -20,8 +20,7 @@ import (
2020)
2121
2222// Conn represents a WebSocket connection.
23- // All methods may be called concurrently except for Reader, Read
24- // and SetReadLimit.
23+ // All methods may be called concurrently except for Reader and Read.
2524//
2625// You must always read from the connection. Otherwise control
2726// frames will not be handled. See the docs on Reader and CloseRead.
@@ -55,8 +54,7 @@ type Conn struct {
5554 writeFrameLock chan struct {}
5655 writeHeaderBuf []byte
5756 writeHeader * header
58- // read limit for a message in bytes.
59- msgReadLimit int64
57+ msgReadLimit * atomicInt64
6058
6159 // Used to ensure a previous writer is not used after being closed.
6260 activeWriter atomic.Value
@@ -70,8 +68,7 @@ type Conn struct {
7068 activeReader * messageReader
7169 // readFrameLock is acquired to read from bw.
7270 readFrameLock chan struct {}
73- // Not int32 because of https://github.com/nhooyr/websocket/issues/153
74- readClosed int32
71+ readClosed * atomicInt64
7572 readHeaderBuf []byte
7673 controlPayloadBuf []byte
7774
@@ -91,7 +88,8 @@ type Conn struct {
9188func (c * Conn ) init () {
9289 c .closed = make (chan struct {})
9390
94- c .msgReadLimit = 32768
91+ c .msgReadLimit = & atomicInt64 {}
92+ c .msgReadLimit .Store (32768 )
9593
9694 c .writeMsgLock = make (chan struct {}, 1 )
9795 c .writeFrameLock = make (chan struct {}, 1 )
@@ -106,6 +104,7 @@ func (c *Conn) init() {
106104 c .writeHeaderBuf = makeWriteHeaderBuf ()
107105 c .writeHeader = & header {}
108106 c .readHeaderBuf = makeReadHeaderBuf ()
107+ c .readClosed = & atomicInt64 {}
109108 c .controlPayloadBuf = make ([]byte , maxControlFramePayload )
110109
111110 runtime .SetFinalizer (c , func (c * Conn ) {
@@ -342,7 +341,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
342341// See https://github.com/nhooyr/websocket/issues/87#issue-451703332
343342// Most users should not need this.
344343func (c * Conn ) Reader (ctx context.Context ) (MessageType , io.Reader , error ) {
345- if atomic . LoadInt32 ( & c .readClosed ) == 1 {
344+ if c .readClosed . Load ( ) == 1 {
346345 return 0 , nil , fmt .Errorf ("websocket connection read closed" )
347346 }
348347
@@ -392,7 +391,7 @@ func (c *Conn) reader(ctx context.Context) (MessageType, io.Reader, error) {
392391 c .readerMsgHeader = h
393392 c .readerFrameEOF = false
394393 c .readerMaskPos = 0
395- c .readMsgLeft = c .msgReadLimit
394+ c .readMsgLeft = c .msgReadLimit . Load ()
396395
397396 r := & messageReader {
398397 c : c ,
0 commit comments