backend: improve error on http request timeout

Now yields a "request timeout" error instead of "context canceled".
This commit is contained in:
Michael Eischer 2024-06-01 18:52:39 +02:00
parent 456ebfb0c7
commit 6bf3d4859f
2 changed files with 23 additions and 5 deletions

View File

@ -2,11 +2,16 @@ package backend
import ( import (
"context" "context"
"errors"
"fmt"
"io" "io"
"net/http" "net/http"
"sync/atomic"
"time" "time"
) )
var errRequestTimeout = fmt.Errorf("request timeout")
// watchdogRoundtripper cancels an http request if an upload or download did not make progress // watchdogRoundtripper cancels an http request if an upload or download did not make progress
// within timeout. The time between fully sending the request and receiving an response is also // within timeout. The time between fully sending the request and receiving an response is also
// limited by this timeout. This ensures that stuck requests are cancelled after some time. // limited by this timeout. This ensures that stuck requests are cancelled after some time.
@ -32,12 +37,14 @@ func newWatchdogRoundtripper(rt http.RoundTripper, timeout time.Duration, chunkS
func (w *watchdogRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) { func (w *watchdogRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
timer := time.NewTimer(w.timeout) timer := time.NewTimer(w.timeout)
ctx, cancel := context.WithCancel(req.Context()) ctx, cancel := context.WithCancel(req.Context())
timedOut := &atomic.Bool{}
// cancel context if timer expires // cancel context if timer expires
go func() { go func() {
defer timer.Stop() defer timer.Stop()
select { select {
case <-timer.C: case <-timer.C:
timedOut.Store(true)
cancel() cancel()
case <-ctx.Done(): case <-ctx.Done():
} }
@ -46,11 +53,14 @@ func (w *watchdogRoundtripper) RoundTrip(req *http.Request) (*http.Response, err
kick := func() { kick := func() {
timer.Reset(w.timeout) timer.Reset(w.timeout)
} }
isTimeout := func(err error) bool {
return timedOut.Load() && errors.Is(err, context.Canceled)
}
req = req.Clone(ctx) req = req.Clone(ctx)
if req.Body != nil { if req.Body != nil {
// kick watchdog timer as long as uploading makes progress // kick watchdog timer as long as uploading makes progress
req.Body = newWatchdogReadCloser(req.Body, w.chunkSize, kick, nil) req.Body = newWatchdogReadCloser(req.Body, w.chunkSize, kick, nil, isTimeout)
} }
resp, err := w.rt.RoundTrip(req) resp, err := w.rt.RoundTrip(req)
@ -60,16 +70,17 @@ func (w *watchdogRoundtripper) RoundTrip(req *http.Request) (*http.Response, err
// kick watchdog timer as long as downloading makes progress // kick watchdog timer as long as downloading makes progress
// cancel context to stop goroutine once response body is closed // cancel context to stop goroutine once response body is closed
resp.Body = newWatchdogReadCloser(resp.Body, w.chunkSize, kick, cancel) resp.Body = newWatchdogReadCloser(resp.Body, w.chunkSize, kick, cancel, isTimeout)
return resp, nil return resp, nil
} }
func newWatchdogReadCloser(rc io.ReadCloser, chunkSize int, kick func(), close func()) *watchdogReadCloser { func newWatchdogReadCloser(rc io.ReadCloser, chunkSize int, kick func(), close func(), isTimeout func(err error) bool) *watchdogReadCloser {
return &watchdogReadCloser{ return &watchdogReadCloser{
rc: rc, rc: rc,
chunkSize: chunkSize, chunkSize: chunkSize,
kick: kick, kick: kick,
close: close, close: close,
isTimeout: isTimeout,
} }
} }
@ -78,6 +89,7 @@ type watchdogReadCloser struct {
chunkSize int chunkSize int
kick func() kick func()
close func() close func()
isTimeout func(err error) bool
} }
var _ io.ReadCloser = &watchdogReadCloser{} var _ io.ReadCloser = &watchdogReadCloser{}
@ -93,6 +105,9 @@ func (w *watchdogReadCloser) Read(p []byte) (n int, err error) {
n, err = w.rc.Read(p) n, err = w.rc.Read(p)
w.kick() w.kick()
if err != nil && w.isTimeout(err) {
err = errRequestTimeout
}
return n, err return n, err
} }

View File

@ -23,8 +23,11 @@ func TestRead(t *testing.T) {
onClose := func() { onClose := func() {
closed = true closed = true
} }
isTimeout := func(err error) bool {
return false
}
wd := newWatchdogReadCloser(io.NopCloser(bytes.NewReader(data)), 1, kick, onClose) wd := newWatchdogReadCloser(io.NopCloser(bytes.NewReader(data)), 1, kick, onClose, isTimeout)
out, err := io.ReadAll(wd) out, err := io.ReadAll(wd)
rtest.OK(t, err) rtest.OK(t, err)
@ -196,6 +199,6 @@ func TestDownloadTimeout(t *testing.T) {
rtest.Equals(t, 200, resp.StatusCode, "unexpected status code") rtest.Equals(t, 200, resp.StatusCode, "unexpected status code")
_, err = io.ReadAll(resp.Body) _, err = io.ReadAll(resp.Body)
rtest.Equals(t, context.Canceled, err, "response download not canceled") rtest.Equals(t, errRequestTimeout, err, "response download not canceled")
rtest.OK(t, resp.Body.Close()) rtest.OK(t, resp.Body.Close())
} }