@@ -74,10 +74,16 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
74
74
// By default, the connection has a message read limit of 32768 bytes.
75
75
//
76
76
// When the limit is hit, the connection will be closed with StatusMessageTooBig.
77
+ //
78
+ // Set to -1 to disable.
77
79
func (c * Conn )SetReadLimit (n int64 ) {
78
- // We add read one more byte than the limit in case
79
- // there is a fin frame that needs to be read.
80
- c .msgReader .limitReader .limit .Store (n + 1 )
80
+ if n >= 0 {
81
+ // We read one more byte than the limit in case
82
+ // there is a fin frame that needs to be read.
83
+ n ++
84
+ }
85
+
86
+ c .msgReader .limitReader .limit .Store (n )
81
87
}
82
88
83
89
const defaultReadLimit = 32768
@@ -455,7 +461,11 @@ func (lr *limitReader) reset(r io.Reader) {
455
461
}
456
462
457
463
func (lr * limitReader )Read (p []byte ) (int ,error ) {
458
- if lr .n <= 0 {
464
+ if lr .n < 0 {
465
+ return lr .r .Read (p )
466
+ }
467
+
468
+ if lr .n == 0 {
459
469
err := fmt .Errorf ("read limited at %v bytes" ,lr .limit .Load ())
460
470
lr .c .writeError (StatusMessageTooBig ,err )
461
471
return 0 ,err
@@ -466,6 +476,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
466
476
}
467
477
n ,err := lr .r .Read (p )
468
478
lr .n -= int64 (n )
479
+ if lr .n < 0 {
480
+ lr .n = 0
481
+ }
469
482
return n ,err
470
483
}
471
484