Skip to content

Commit 1a56599

Browse files
authored
Add timeout to writing to responses (#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 <[email protected]>
1 parent 2d87a84 commit 1a56599

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

modules/graceful/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
DefaultMaxHeaderBytes int
2929
)
3030

31+
// PerWriteWriteTimeout timeout for writes
32+
const PerWriteWriteTimeout = 5 * time.Second
33+
3134
func init() {
3235
DefaultMaxHeaderBytes = 0 // use http.DefaultMaxHeaderBytes - which currently is 1 << 20 (1MB)
3336
}
@@ -250,6 +253,13 @@ type wrappedConn struct {
250253
closed *int32
251254
}
252255

256+
func (w wrappedConn) Write(p []byte) (n int, err error) {
257+
if PerWriteWriteTimeout > 0 {
258+
_ = w.Conn.SetWriteDeadline(time.Now().Add(PerWriteWriteTimeout))
259+
}
260+
return w.Conn.Write(p)
261+
}
262+
253263
func (w wrappedConn) Close() error {
254264
if atomic.CompareAndSwapInt32(w.closed, 0, 1) {
255265
defer func() {

0 commit comments

Comments
 (0)