How do I retry mid-body response read network failures? #135
-
|
It seems as if none of the libs from https://github.com/earthboundkid/requests/wiki#retries will help if the request fails somewhere in the middle of response read. Meanwhile the API itself suggests it might be possible. So I'm wondering if
❤️ for the great lib btw! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Thanks. Yes, for a body read failure, you would need to go outside the transport and redo the whole request with a generic retry mechanism. Transports can only retry status errors like 429 Too Many Requests. |
Beta Was this translation helpful? Give feedback.
-
|
This is how we're handling it "from the outside" to catch connection errors with // Validator that returns errors the backoff library uses
func ToRetryOrNotToRetry(resp *http.Response) error {
switch {
case resp.StatusCode == http.StatusOK:
return nil
case resp.StatusCode == http.StatusTooManyRequests:
seconds, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
if err == nil {
return backoff.RetryAfter(int(seconds))
}
return fmt.Errorf("429: will retry")
case resp.StatusCode >= 500 && resp.StatusCode <= 599:
return fmt.Errorf("5xx: will retry")
// default to permanent errors
default:
return backoff.Permanent(fmt.Errorf("unknown status: won't retry"))
}
}// The retry function
func Retry(ctx context.Context, f func() error) error {
operation := func() (struct{}, error) {
err := f()
return struct{}{}, err
}
_, err := backoff.Retry(
ctx,
operation,
backoff.WithBackOff(backoff.NewExponentialBackoff()),
backoff.WithMaxTries(3),
)
return err
}// making the request
err := Retry(ctx, func() error {
err := req.AddValidator(ToRetryOrNotToRetry).Fetch(ctx)
if err != nil {
slog.WarnContext(ctx, "error fetching data", "err", err.Error())
}
return err
}) |
Beta Was this translation helpful? Give feedback.
Thanks. Yes, for a body read failure, you would need to go outside the transport and redo the whole request with a generic retry mechanism. Transports can only retry status errors like 429 Too Many Requests.