Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 82 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/rs/zerolog"
"go.mau.fi/util/exsync"
"go.mau.fi/util/ptr"
"go.mau.fi/util/random"
"go.mau.fi/util/retryafter"
Expand Down Expand Up @@ -96,6 +98,10 @@ type Client struct {
RequestHook func(req *http.Request)
ResponseHook func(req *http.Request, resp *http.Response, err error, duration time.Duration)

RequestRetryTrigger *exsync.Event

// Deprecated: this hook is no longer used by mautrix internals and is kept only
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could just delete this and consumers can either upgrade to the new behaviour, or just drop the functionality entirely.

Is anyone outside beeper using this atm?

// for compile compatibility with downstream consumers.
UpdateRequestOnRetry func(req *http.Request, cause error) *http.Request

SyncPresence event.Presence
Expand Down Expand Up @@ -620,16 +626,16 @@ func (cli *Client) doRetry(
Str("url", req.URL.String()).
Int("retry_in_seconds", int(backoff.Seconds())).
Msg("Request failed, retrying")
select {
case <-time.After(backoff):
case <-req.Context().Done():
if !errors.Is(context.Cause(req.Context()), ErrContextCancelRetry) {

// if this was due to our RequestRetryTrigger then just retry immediately
// the req.Context() will still be live, otherwise do a normal backoff
if !errors.Is(cause, ErrContextCancelRetry) {
select {
case <-time.After(backoff):
case <-req.Context().Done():
return nil, nil, req.Context().Err()
}
}
if cli.UpdateRequestOnRetry != nil {
req = cli.UpdateRequestOnRetry(req, cause)
}
return cli.executeCompiledRequest(req, retries-1, backoff*2, responseJSON, handler, dontReadResponse, sizeLimit, client)
}

Expand Down Expand Up @@ -738,6 +744,46 @@ func ParseErrorResponse(req *http.Request, res *http.Response) ([]byte, error) {
}
}

func (cli *Client) prepareRequestAttempt(req *http.Request) (*http.Request, func()) {
// if there's no retry trigger, nothing to do
if cli.RequestRetryTrigger == nil {
return req, func() {}
}

attemptCtx, cancel := context.WithCancelCause(req.Context())
var finishOnce sync.Once
finish := func() {
finishOnce.Do(func() {
cancel(context.Canceled)
})
}

resetChan := cli.RequestRetryTrigger.GetChan()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I could just do RequestRetryTrigger.Wait(attemptCtx) in the goroutine?

go func() {
// if we hear of a reset, cancel the request context
select {
case <-resetChan:
cancel(ErrContextCancelRetry)
case <-attemptCtx.Done():
}
}()

return req.WithContext(attemptCtx), finish
}

type cleanupReadCloser struct {
io.ReadCloser
cleanup func()
}

func (crc cleanupReadCloser) Close() error {
err := crc.ReadCloser.Close()
if crc.cleanup != nil {
crc.cleanup()
}
return err
}

func (cli *Client) executeCompiledRequest(
req *http.Request,
retries int,
Expand All @@ -748,30 +794,46 @@ func (cli *Client) executeCompiledRequest(
sizeLimit int64,
client *http.Client,
) ([]byte, *http.Response, error) {
cli.RequestStart(req)
attemptReq, cleanup := cli.prepareRequestAttempt(req)
cli.RequestStart(attemptReq)
startTime := time.Now()
res, err := client.Do(req)
res, err := client.Do(attemptReq)
duration := time.Since(startTime)
if res != nil && !dontReadResponse {
defer res.Body.Close()
if res != nil {
// Cleanup the child attempt context once the body is closed
res.Body = cleanupReadCloser{
ReadCloser: res.Body,
cleanup: cleanup,
}
Comment thread
adamvy marked this conversation as resolved.
Outdated
if !dontReadResponse {
defer res.Body.Close()
}
}
if err != nil {
// Either error is *not* canceled or the underlying cause of cancelation explicitly asks to retry
// cleanup child attempt context on error
cleanup()

// Either error is *not* canceled or the underlying cause of cancellation explicitly asks to retry
attemptCause := context.Cause(attemptReq.Context())
retryCause := err
if errors.Is(attemptCause, ErrContextCancelRetry) {
retryCause = attemptCause
}
canRetry := !errors.Is(err, context.Canceled) ||
errors.Is(context.Cause(req.Context()), ErrContextCancelRetry)
errors.Is(attemptCause, ErrContextCancelRetry)
if retries > 0 && canRetry {
return cli.doRetry(
req, err, retries, backoff, responseJSON, handler, dontReadResponse, sizeLimit, client,
req, retryCause, retries, backoff, responseJSON, handler, dontReadResponse, sizeLimit, client,
)
}
err = HTTPError{
Request: req,
Request: attemptReq,
Response: res,

Message: "request error",
WrappedError: err,
}
cli.LogRequestDone(req, res, err, nil, 0, duration)
cli.LogRequestDone(attemptReq, res, err, nil, 0, duration)
return nil, res, err
}

Expand All @@ -784,11 +846,11 @@ func (cli *Client) executeCompiledRequest(

var body []byte
if res.StatusCode < 200 || res.StatusCode >= 300 {
body, err = ParseErrorResponse(req, res)
cli.LogRequestDone(req, res, nil, nil, len(body), duration)
body, err = ParseErrorResponse(attemptReq, res)
cli.LogRequestDone(attemptReq, res, nil, nil, len(body), duration)
} else {
body, err = handler(req, res, responseJSON, sizeLimit)
cli.LogRequestDone(req, res, nil, err, len(body), duration)
body, err = handler(attemptReq, res, responseJSON, sizeLimit)
cli.LogRequestDone(attemptReq, res, nil, err, len(body), duration)
}
return body, res, err
}
Expand Down
Loading
Loading