@@ -20,8 +20,7 @@ import (
20
20
)
21
21
22
22
// 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.
25
24
//
26
25
// You must always read from the connection. Otherwise control
27
26
// frames will not be handled. See the docs on Reader and CloseRead.
@@ -56,7 +55,7 @@ type Conn struct {
56
55
writeHeaderBuf []byte
57
56
writeHeader * header
58
57
// read limit for a message in bytes.
59
- msgReadLimit int64
58
+ msgReadLimit * atomicInt64
60
59
61
60
// Used to ensure a previous writer is not used after being closed.
62
61
activeWriter atomic.Value
@@ -70,7 +69,7 @@ type Conn struct {
70
69
activeReader * messageReader
71
70
// readFrameLock is acquired to read from bw.
72
71
readFrameLock chan struct {}
73
- readClosed int64
72
+ readClosed * atomicInt64
74
73
readHeaderBuf []byte
75
74
controlPayloadBuf []byte
76
75
@@ -90,7 +89,8 @@ type Conn struct {
90
89
func (c * Conn ) init () {
91
90
c .closed = make (chan struct {})
92
91
93
- c .msgReadLimit = 32768
92
+ c .msgReadLimit = & atomicInt64 {}
93
+ c .msgReadLimit .Store (32768 )
94
94
95
95
c .writeMsgLock = make (chan struct {}, 1 )
96
96
c .writeFrameLock = make (chan struct {}, 1 )
@@ -105,6 +105,7 @@ func (c *Conn) init() {
105
105
c .writeHeaderBuf = makeWriteHeaderBuf ()
106
106
c .writeHeader = & header {}
107
107
c .readHeaderBuf = makeReadHeaderBuf ()
108
+ c .readClosed = & atomicInt64 {}
108
109
c .controlPayloadBuf = make ([]byte , maxControlFramePayload )
109
110
110
111
runtime .SetFinalizer (c , func (c * Conn ) {
@@ -341,7 +342,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
341
342
// See https://github.com/nhooyr/websocket/issues/87#issue-451703332
342
343
// Most users should not need this.
343
344
func (c * Conn ) Reader (ctx context.Context ) (MessageType , io.Reader , error ) {
344
- if atomic . LoadInt64 ( & c .readClosed ) == 1 {
345
+ if c .readClosed . Load ( ) == 1 {
345
346
return 0 , nil , fmt .Errorf ("websocket connection read closed" )
346
347
}
347
348
@@ -391,7 +392,7 @@ func (c *Conn) reader(ctx context.Context) (MessageType, io.Reader, error) {
391
392
c .readerMsgHeader = h
392
393
c .readerFrameEOF = false
393
394
c .readerMaskPos = 0
394
- c .readMsgLeft = c .msgReadLimit
395
+ c .readMsgLeft = c .msgReadLimit . Load ()
395
396
396
397
r := & messageReader {
397
398
c : c ,
0 commit comments