Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

netconn.go: Disable read limit on WebSocket#254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nhooyr merged 2 commits intodevfromnetconn-readlimit
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
netconn: Disable read limit on WebSocket
Closes#245
  • Loading branch information
@nhooyr
nhooyr committedJan 9, 2021
commitea87744105d79f972e58404bb46791b97fc3f314
4 changes: 4 additions & 0 deletionsnetconn.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,11 @@ import (
//
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
// io.EOF when reading.
//
// Furthermore, the ReadLimit is set to -1 to disable it.
func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
c.SetReadLimit(-1)

nc := &netConn{
c: c,
msgType: msgType,
Expand Down
21 changes: 17 additions & 4 deletionsread.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -74,10 +74,16 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
// By default, the connection has a message read limit of 32768 bytes.
//
// When the limit is hit, the connection will be closed with StatusMessageTooBig.
//
// Set to -1 to disable.
func (c *Conn) SetReadLimit(n int64) {
// We add read one more byte than the limit in case
// there is a fin frame that needs to be read.
c.msgReader.limitReader.limit.Store(n + 1)
if n >= 0 {
// We read one more byte than the limit in case
// there is a fin frame that needs to be read.
n++
}

c.msgReader.limitReader.limit.Store(n)
}

const defaultReadLimit = 32768
Expand DownExpand Up@@ -455,7 +461,11 @@ func (lr *limitReader) reset(r io.Reader) {
}

func (lr *limitReader) Read(p []byte) (int, error) {
if lr.n <= 0 {
if lr.n < 0 {
return lr.r.Read(p)
}

if lr.n == 0 {
err := fmt.Errorf("read limited at %v bytes", lr.limit.Load())
lr.c.writeError(StatusMessageTooBig, err)
return 0, err
Expand All@@ -466,6 +476,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
}
n, err := lr.r.Read(p)
lr.n -= int64(n)
if lr.n < 0 {
lr.n = 0
}
return n, err
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp