fix(app): add read, write and idle timeouts to the HTTP servers
- the API + admin UI server and the public web server get ReadTimeout 30s, WriteTimeout 60s and IdleTimeout 120s next to ReadHeaderTimeout - the archive import extends its own read and write deadlines to 30 minutes with http.ResponseController, so a big mbox is not cut - tests: a stalled body and an idle connection are cut on both servers, an import longer than the default timeouts succeeds, fields are pinned - README: document the limits and timeouts
This commit is contained in:
parent
0a21e998e7
commit
38552f8025
4 changed files with 150 additions and 4 deletions
|
|
@ -561,6 +561,10 @@ func (s *server) discardQueued(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// importTimeout is how long one archive import may read its body and write its
|
||||
// summary, in place of the server's default read and write timeouts.
|
||||
const importTimeout = 30 * time.Minute
|
||||
|
||||
// importArchive reads an mbox from the request body, streaming, and archives it.
|
||||
// Failed messages are reported in the summary; the request itself succeeds.
|
||||
func (s *server) importArchive(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -572,6 +576,11 @@ func (s *server) importArchive(w http.ResponseWriter, r *http.Request) {
|
|||
storeError(w, err)
|
||||
return
|
||||
}
|
||||
// A big mbox streams for longer than the server's default timeouts. Errors
|
||||
// are ignored: a ResponseWriter that cannot extend deadlines has none.
|
||||
rc := http.NewResponseController(w)
|
||||
_ = rc.SetReadDeadline(time.Now().Add(importTimeout))
|
||||
_ = rc.SetWriteDeadline(time.Now().Add(importTimeout))
|
||||
sum, err := archive.ImportMbox(s.store, id, r.Body, time.Now())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("import stopped after %d message(s): %v", sum.Imported, err))
|
||||
|
|
|
|||
|
|
@ -29,6 +29,25 @@ import (
|
|||
// nowFunc is the clock of the store (queue schedules, token expiry). Tests move it.
|
||||
var nowFunc = time.Now
|
||||
|
||||
// HTTP timeouts of the API + admin UI server and of the public web server, so
|
||||
// a slow or silent client cannot pin a goroutine. The archive import extends
|
||||
// its own deadlines (see api.importArchive). Variables so tests can shorten them.
|
||||
var (
|
||||
httpReadTimeout = 30 * time.Second
|
||||
httpWriteTimeout = 60 * time.Second
|
||||
httpIdleTimeout = 120 * time.Second
|
||||
)
|
||||
|
||||
func newHTTPServer(h http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Handler: h,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
ReadTimeout: httpReadTimeout,
|
||||
WriteTimeout: httpWriteTimeout,
|
||||
IdleTimeout: httpIdleTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
// heloName is the EHLO name: the configured one, else this machine's host name.
|
||||
func heloName(configured string) string {
|
||||
if configured != "" {
|
||||
|
|
@ -196,7 +215,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
|
|||
mux := http.NewServeMux()
|
||||
mux.Handle("/api/", api.New(st, apiToken, releaser{rt}))
|
||||
mux.Handle("/", adminui.Embedded())
|
||||
a.web = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}
|
||||
a.web = newHTTPServer(mux)
|
||||
}
|
||||
|
||||
var pubLn net.Listener
|
||||
|
|
@ -211,7 +230,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
|
|||
}
|
||||
a.WebAddr = pubLn.Addr().String()
|
||||
a.lns = append(a.lns, pubLn)
|
||||
a.pub = &http.Server{Handler: web.New(st, rt), ReadHeaderTimeout: 10 * time.Second}
|
||||
a.pub = newHTTPServer(web.New(st, rt))
|
||||
}
|
||||
|
||||
var workerCtx context.Context
|
||||
|
|
|
|||
|
|
@ -1004,3 +1004,115 @@ func TestTheServerNameSettingReachesTheTLSCheck(t *testing.T) {
|
|||
t.Fatal("nothing may be sent to a relay whose certificate does not match")
|
||||
}
|
||||
}
|
||||
|
||||
// shortTimeouts makes the HTTP timeouts small enough to test.
|
||||
func shortTimeouts(t *testing.T) {
|
||||
t.Helper()
|
||||
r, w, i := httpReadTimeout, httpWriteTimeout, httpIdleTimeout
|
||||
httpReadTimeout, httpWriteTimeout, httpIdleTimeout = 300*time.Millisecond, 300*time.Millisecond, 300*time.Millisecond
|
||||
t.Cleanup(func() { httpReadTimeout, httpWriteTimeout, httpIdleTimeout = r, w, i })
|
||||
}
|
||||
|
||||
// stalled sends a request that promises a body and never finishes it, and
|
||||
// reports whether the server hung up within a few seconds.
|
||||
func stalled(t *testing.T, addr, req string) bool {
|
||||
t.Helper()
|
||||
c, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
if _, err := c.Write([]byte(req)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.SetReadDeadline(time.Now().Add(4 * time.Second))
|
||||
_, err = io.Copy(io.Discard, c) // returns nil on EOF, an error on our deadline
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestSlowRequestsAreCutOnBothServers(t *testing.T) {
|
||||
shortTimeouts(t)
|
||||
relayAddr, _ := startSink(t)
|
||||
a, err := Start(testConfig(t, relayAddr), "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.Close() })
|
||||
hdr := "POST %s HTTP/1.1\r\nHost: x\r\nAuthorization: Bearer tok\r\nContent-Type: application/json\r\nContent-Length: 500\r\n\r\n{\"name\":"
|
||||
for name, c := range map[string]struct{ addr, path string }{
|
||||
"api": {a.APIAddr, "/api/v1/lists"},
|
||||
"web": {a.WebAddr, "/unsubscribe/tok"},
|
||||
} {
|
||||
if !stalled(t, c.addr, strings.Replace(hdr, "%s", c.path, 1)) {
|
||||
t.Errorf("%s: a stalled body was not cut", name)
|
||||
}
|
||||
}
|
||||
// An idle keep-alive connection is closed too.
|
||||
for name, addr := range map[string]string{"api": a.APIAddr, "web": a.WebAddr} {
|
||||
if !stalled(t, addr, "GET /nothing HTTP/1.1\r\nHost: x\r\n\r\n") {
|
||||
t.Errorf("%s: an idle connection was kept", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// slowReader hands out its data in two halves, a pause apart.
|
||||
type slowReader struct {
|
||||
parts [][]byte
|
||||
pause time.Duration
|
||||
}
|
||||
|
||||
func (s *slowReader) Read(p []byte) (int, error) {
|
||||
if len(s.parts) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if len(s.parts) == 1 {
|
||||
time.Sleep(s.pause)
|
||||
}
|
||||
n := copy(p, s.parts[0])
|
||||
s.parts = s.parts[1:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func TestArchiveImportIsNotCutByTheDefaultTimeouts(t *testing.T) {
|
||||
shortTimeouts(t)
|
||||
relayAddr, _ := startSink(t)
|
||||
a, err := Start(testConfig(t, relayAddr), "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.Close() })
|
||||
call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, "tok")
|
||||
|
||||
m1 := "From a@example.com Tue Jan 2 15:04:05 2024\nFrom: a@example.com\nSubject: one\nMessage-ID: <1@x>\n\nbody one\n\n"
|
||||
m2 := "From a@example.com Tue Jan 2 15:04:06 2024\nFrom: a@example.com\nSubject: two\nMessage-ID: <2@x>\n\nbody two\n"
|
||||
body := &slowReader{parts: [][]byte{[]byte(m1), []byte(m2)}, pause: 3 * httpReadTimeout}
|
||||
req, _ := http.NewRequest("POST", "http://"+a.APIAddr+"/api/v1/lists/1/archive/import", body)
|
||||
req.Header.Set("Authorization", "Bearer tok")
|
||||
req.ContentLength = -1 // chunked: the body is streamed
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("import: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != 200 || !strings.Contains(string(b), `"imported":2`) {
|
||||
t.Fatalf("import = %d %s", resp.StatusCode, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPServersCarryTheTimeouts(t *testing.T) {
|
||||
relayAddr, _ := startSink(t)
|
||||
a, err := Start(testConfig(t, relayAddr), "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.Close() })
|
||||
if httpReadTimeout != 30*time.Second || httpWriteTimeout != 60*time.Second || httpIdleTimeout != 120*time.Second {
|
||||
t.Fatalf("defaults: %v %v %v", httpReadTimeout, httpWriteTimeout, httpIdleTimeout)
|
||||
}
|
||||
for name, s := range map[string]*http.Server{"api": a.web, "web": a.pub} {
|
||||
if s.ReadTimeout != httpReadTimeout || s.WriteTimeout != httpWriteTimeout || s.IdleTimeout != httpIdleTimeout || s.ReadHeaderTimeout == 0 {
|
||||
t.Errorf("%s: timeouts %v %v %v %v", name, s.ReadHeaderTimeout, s.ReadTimeout, s.WriteTimeout, s.IdleTimeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -297,11 +297,11 @@ func TestManyPartsAreBounded(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestOnlyTheTextPartsThatAreNeededAreRead(t *testing.T) {
|
||||
// 60 text parts of 1 MiB each: the first wins and the rest are not read.
|
||||
// 30 text parts of 1 MiB each: the first wins and the rest are not read.
|
||||
big := strings.Repeat("a", MaxBody)
|
||||
var b strings.Builder
|
||||
b.WriteString("Subject: big\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n")
|
||||
for i := 0; i < 60; i++ {
|
||||
for i := 0; i < 30; i++ {
|
||||
b.WriteString("--b\r\nContent-Type: text/plain\r\n\r\n" + big + "\r\n")
|
||||
}
|
||||
raw := []byte(b.String())
|
||||
|
|
|
|||
Loading…
Reference in a new issue