Skip to content

Commit

Permalink
Add timeout to writing to responses (#15831)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
zeripath committed May 14, 2021
1 parent 2d87a84 commit 1a56599
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/graceful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -250,6 +253,13 @@ type wrappedConn struct {
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 {
if atomic.CompareAndSwapInt32(w.closed, 0, 1) {
defer func() {
Expand Down

0 comments on commit 1a56599

Please sign in to comment.