Add timeout to writing to responses (#15831) (#15872)

Backport #15831

In #15826 it has become apparent that there are a few occasions when a response can
hang during writing, and because there is no timeout go will happily just block
interminably. This PR adds a fixed 5 second timeout to all writes to a connection.

Fix #15826

Signed-off-by: Andrew Thornton <art27@cantab.net>
pull/15871/head^2
zeripath 4 years ago committed by GitHub
parent f773733252
commit 41422f0df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      modules/graceful/server.go

@ -28,6 +28,9 @@ var (
DefaultMaxHeaderBytes int DefaultMaxHeaderBytes int
) )
// PerWriteWriteTimeout timeout for writes
const PerWriteWriteTimeout = 5 * time.Second
func init() { func init() {
DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB) DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB)
} }
@ -250,6 +253,13 @@ type wrappedConn struct {
closed *int32 closed *int32
} }
func (w wrappedConn) Write(p []byte) (n int, err error) {
if PerWriteWriteTimeout > 0 {
_ = w.Conn.SetWriteDeadline(time.Now().Add(PerWriteWriteTimeout))
}
return w.Conn.Write(p)
}
func (w wrappedConn) Close() error { func (w wrappedConn) Close() error {
if atomic.CompareAndSwapInt32(w.closed, 0, 1) { if atomic.CompareAndSwapInt32(w.closed, 0, 1) {
defer func() { defer func() {

Loading…
Cancel
Save