File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,11 @@ import (
37
37
//
38
38
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
39
39
// io.EOF when reading.
40
+ //
41
+ // Furthermore, the ReadLimit is set to -1 to disable it.
40
42
func NetConn (ctx context.Context , c * Conn , msgType MessageType ) net.Conn {
43
+ c .SetReadLimit (- 1 )
44
+
41
45
nc := & netConn {
42
46
c : c ,
43
47
msgType : msgType ,
Original file line number Diff line number Diff line change @@ -69,10 +69,16 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
69
69
// By default, the connection has a message read limit of 32768 bytes.
70
70
//
71
71
// When the limit is hit, the connection will be closed with StatusMessageTooBig.
72
+ //
73
+ // Set to -1 to disable.
72
74
func (c * Conn ) SetReadLimit (n int64 ) {
73
- // We add read one more byte than the limit in case
74
- // there is a fin frame that needs to be read.
75
- c .msgReader .limitReader .limit .Store (n + 1 )
75
+ if n >= 0 {
76
+ // We read one more byte than the limit in case
77
+ // there is a fin frame that needs to be read.
78
+ n ++
79
+ }
80
+
81
+ c .msgReader .limitReader .limit .Store (n )
76
82
}
77
83
78
84
const defaultReadLimit = 32768
@@ -453,7 +459,11 @@ func (lr *limitReader) reset(r io.Reader) {
453
459
}
454
460
455
461
func (lr * limitReader ) Read (p []byte ) (int , error ) {
456
- if lr .n <= 0 {
462
+ if lr .n < 0 {
463
+ return lr .r .Read (p )
464
+ }
465
+
466
+ if lr .n == 0 {
457
467
err := fmt .Errorf ("read limited at %v bytes" , lr .limit .Load ())
458
468
lr .c .writeError (StatusMessageTooBig , err )
459
469
return 0 , err
@@ -464,6 +474,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
464
474
}
465
475
n , err := lr .r .Read (p )
466
476
lr .n -= int64 (n )
477
+ if lr .n < 0 {
478
+ lr .n = 0
479
+ }
467
480
return n , err
468
481
}
469
482
You can’t perform that action at this time.
0 commit comments