-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeout to writing to responses #15831
Changes from all commits
3e181b3
46cf319
5e3d79a
e33a3fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ var ( | |
DefaultMaxHeaderBytes int | ||
) | ||
|
||
// PerWriteWriteTimeout timeout for writes | ||
const PerWriteWriteTimeout = 5 * time.Second | ||
|
||
func init() { | ||
DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB) | ||
} | ||
|
@@ -250,6 +253,13 @@ type wrappedConn struct { | |
closed *int32 | ||
} | ||
|
||
func (w wrappedConn) Write(p []byte) (n int, err error) { | ||
if PerWriteWriteTimeout > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why check? It's a const value, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A const that is labelled so that you can change it at compile time to something else... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g.
or
|
||
_ = w.Conn.SetWriteDeadline(time.Now().Add(PerWriteWriteTimeout)) | ||
} | ||
return w.Conn.Write(p) | ||
} | ||
|
||
func (w wrappedConn) Close() error { | ||
if atomic.CompareAndSwapInt32(w.closed, 0, 1) { | ||
defer func() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 secs seem a lot; if kernel blocks for that duration the system has bigger problems..
200ms seems to be a lower bound due to write batching with NAGLE (?), I'd go with 500ms?
But as a stopgap to avoid lingering connections a large value seems ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I picked 5s as value everyone could agree meant that the underlying connection is totally blocked and just wasn't coming back. But I have no metric to know what is a good value here as I don't completely understand the situations where this happens.
I'm suspecting that the problem is due to some network reconfiguration that leads to the connection at the go end being disconnected from the client but somehow didn't kill the connection.