@@ -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.
@@ -56,7 +55,7 @@ type Conn struct {
5655 writeHeaderBuf []byte
5756 writeHeader * header
5857 // read limit for a message in bytes.
59- msgReadLimit int64
58+ msgReadLimit * atomicInt64
6059
6160 // Used to ensure a previous writer is not used after being closed.
6261 activeWriter atomic.Value
@@ -70,8 +69,7 @@ type Conn struct {
7069 activeReader * messageReader
7170 // readFrameLock is acquired to read from bw.
7271 readFrameLock chan struct {}
73- // Not int32 because of https://github.com/nhooyr/websocket/issues/153
74- readClosed int32
72+ readClosed * atomicInt64
7573 readHeaderBuf []byte
7674 controlPayloadBuf []byte
7775
@@ -91,7 +89,8 @@ type Conn struct {
9189func (c * Conn ) init () {
9290 c .closed = make (chan struct {})
9391
94- c .msgReadLimit = 32768
92+ c .msgReadLimit = & atomicInt64 {}
93+ c .msgReadLimit .Store (32768 )
9594
9695 c .writeMsgLock = make (chan struct {}, 1 )
9796 c .writeFrameLock = make (chan struct {}, 1 )
@@ -106,6 +105,7 @@ func (c *Conn) init() {
106105 c .writeHeaderBuf = makeWriteHeaderBuf ()
107106 c .writeHeader = & header {}
108107 c .readHeaderBuf = makeReadHeaderBuf ()
108+ c .readClosed = & atomicInt64 {}
109109 c .controlPayloadBuf = make ([]byte , maxControlFramePayload )
110110
111111 runtime .SetFinalizer (c , func (c * Conn ) {
@@ -342,7 +342,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
342342// See https://github.com/nhooyr/websocket/issues/87#issue-451703332
343343// Most users should not need this.
344344func (c * Conn ) Reader (ctx context.Context ) (MessageType , io.Reader , error ) {
345- if atomic . LoadInt32 ( & c .readClosed ) == 1 {
345+ if c .readClosed . Load ( ) == 1 {
346346 return 0 , nil , fmt .Errorf ("websocket connection read closed" )
347347 }
348348
@@ -392,7 +392,7 @@ func (c *Conn) reader(ctx context.Context) (MessageType, io.Reader, error) {
392392 c .readerMsgHeader = h
393393 c .readerFrameEOF = false
394394 c .readerMaskPos = 0
395- c .readMsgLeft = c .msgReadLimit
395+ c .readMsgLeft = c .msgReadLimit . Load ()
396396
397397 r := & messageReader {
398398 c : c ,
0 commit comments