HTTP: increase request timeout (#32542)

This commit is contained in:
andig 2026-08-05 13:25:05 +02:00 • committed by GitHub
parent d71e3ac819
commit 64233c8a1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View file

@ -105,7 +105,7 @@ func NewHTTPd(addr string, hub *SocketHub, customCssFile string) *HTTPd {
Addr: addr,
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
ErrorLog: log.ERROR,
},

38
server/http_test.go Normal file
View file

@ -0,0 +1,38 @@
package server
import (
"io"
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestWriteTimeout ensures a handler exceeding the server's write timeout
// leaves the client without a usable response.
func TestWriteTimeout(t *testing.T) {
timeout := 200 * time.Millisecond
srv := NewHTTPd("", nil, "")
srv.WriteTimeout = timeout
srv.Router().HandleFunc("/slow", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * timeout)
w.Write([]byte(`{"result":"ok"}`))
}).Methods(http.MethodPost)
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
go srv.Serve(l)
t.Cleanup(func() { srv.Close() })
resp, err := http.Post("http://"+l.Addr().String()+"/slow", "application/json", nil)
if err == nil {
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
}
require.Error(t, err, "expected connection to be closed after write timeout")
}