2024-05-01 18:06:54 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-01 16:52:39 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-05-01 18:06:54 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-06-01 16:52:39 +00:00
|
|
|
"sync/atomic"
|
2024-05-01 18:06:54 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-06-01 16:52:39 +00:00
|
|
|
var errRequestTimeout = fmt.Errorf("request timeout")
|
|
|
|
|
2024-05-01 18:06:54 +00:00
|
|
|
// 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
|
|
|
|
// limited by this timeout. This ensures that stuck requests are cancelled after some time.
|
|
|
|
//
|
|
|
|
// The roundtriper makes the assumption that the upload and download happen continuously. In particular,
|
|
|
|
// the caller must not make long pauses between individual read requests from the response body.
|
|
|
|
type watchdogRoundtripper struct {
|
|
|
|
rt http.RoundTripper
|
|
|
|
timeout time.Duration
|
|
|
|
chunkSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.RoundTripper = &watchdogRoundtripper{}
|
|
|
|
|
|
|
|
func newWatchdogRoundtripper(rt http.RoundTripper, timeout time.Duration, chunkSize int) *watchdogRoundtripper {
|
|
|
|
return &watchdogRoundtripper{
|
|
|
|
rt: rt,
|
|
|
|
timeout: timeout,
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watchdogRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
timer := time.NewTimer(w.timeout)
|
|
|
|
ctx, cancel := context.WithCancel(req.Context())
|
2024-06-01 16:52:39 +00:00
|
|
|
timedOut := &atomic.Bool{}
|
2024-05-01 18:06:54 +00:00
|
|
|
|
|
|
|
// cancel context if timer expires
|
|
|
|
go func() {
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
2024-06-01 16:52:39 +00:00
|
|
|
timedOut.Store(true)
|
2024-05-01 18:06:54 +00:00
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
kick := func() {
|
|
|
|
timer.Reset(w.timeout)
|
|
|
|
}
|
2024-06-01 16:52:39 +00:00
|
|
|
isTimeout := func(err error) bool {
|
|
|
|
return timedOut.Load() && errors.Is(err, context.Canceled)
|
|
|
|
}
|
2024-05-01 18:06:54 +00:00
|
|
|
|
|
|
|
req = req.Clone(ctx)
|
|
|
|
if req.Body != nil {
|
|
|
|
// kick watchdog timer as long as uploading makes progress
|
2024-06-01 16:52:39 +00:00
|
|
|
req.Body = newWatchdogReadCloser(req.Body, w.chunkSize, kick, nil, isTimeout)
|
2024-05-01 18:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := w.rt.RoundTrip(req)
|
|
|
|
if err != nil {
|
2024-08-10 16:06:24 +00:00
|
|
|
if isTimeout(err) {
|
|
|
|
err = errRequestTimeout
|
|
|
|
}
|
2024-05-01 18:06:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// kick watchdog timer as long as downloading makes progress
|
|
|
|
// cancel context to stop goroutine once response body is closed
|
2024-06-01 16:52:39 +00:00
|
|
|
resp.Body = newWatchdogReadCloser(resp.Body, w.chunkSize, kick, cancel, isTimeout)
|
2024-05-01 18:06:54 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-06-01 16:52:39 +00:00
|
|
|
func newWatchdogReadCloser(rc io.ReadCloser, chunkSize int, kick func(), close func(), isTimeout func(err error) bool) *watchdogReadCloser {
|
2024-05-01 18:06:54 +00:00
|
|
|
return &watchdogReadCloser{
|
|
|
|
rc: rc,
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
kick: kick,
|
|
|
|
close: close,
|
2024-06-01 16:52:39 +00:00
|
|
|
isTimeout: isTimeout,
|
2024-05-01 18:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type watchdogReadCloser struct {
|
|
|
|
rc io.ReadCloser
|
|
|
|
chunkSize int
|
|
|
|
kick func()
|
|
|
|
close func()
|
2024-06-01 16:52:39 +00:00
|
|
|
isTimeout func(err error) bool
|
2024-05-01 18:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.ReadCloser = &watchdogReadCloser{}
|
|
|
|
|
|
|
|
func (w *watchdogReadCloser) Read(p []byte) (n int, err error) {
|
|
|
|
w.kick()
|
|
|
|
|
|
|
|
// Read is not required to fill the whole passed in byte slice
|
|
|
|
// Thus, keep things simple and just stay within our chunkSize.
|
|
|
|
if len(p) > w.chunkSize {
|
|
|
|
p = p[:w.chunkSize]
|
|
|
|
}
|
|
|
|
n, err = w.rc.Read(p)
|
|
|
|
w.kick()
|
|
|
|
|
2024-06-01 16:52:39 +00:00
|
|
|
if err != nil && w.isTimeout(err) {
|
|
|
|
err = errRequestTimeout
|
|
|
|
}
|
2024-05-01 18:06:54 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watchdogReadCloser) Close() error {
|
|
|
|
if w.close != nil {
|
|
|
|
w.close()
|
|
|
|
}
|
|
|
|
return w.rc.Close()
|
|
|
|
}
|