Skip to content

Commit

Permalink
Use retryablehttp facilities to retry on read errors
Browse files Browse the repository at this point in the history
This adds additional copy operations for all requests, which can
decrease performance.  However, in practice network and image decoding
should be slow enough where this does not matter.

Addresses #30
  • Loading branch information
leotaku committed Apr 14, 2023
1 parent e1be8bc commit 775dba2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmd/formats/download/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package download

import (
"bytes"
"context"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"time"

Expand All @@ -31,6 +33,7 @@ func init() {
retry.Logger = nil
retry.RetryWaitMin = time.Second * 5
retry.Backoff = retryablehttp.LinearJitterBackoff
retry.CheckRetry = bodyReadableErrorPolicy
httpClient = retry.StandardClient()
mangadexClient = md.NewClient().WithHTTPClient(httpClient)
}
Expand Down Expand Up @@ -224,3 +227,20 @@ func getImage(client *http.Client, ctx context.Context, url string) (image.Image
}
return img, nil
}

func bodyReadableErrorPolicy(ctx context.Context, resp *http.Response, err error) (bool, error) {
if retry, err := retryablehttp.DefaultRetryPolicy(ctx, resp, err); retry || err != nil {
return retry, err
}

buf := bytes.NewBuffer(nil)
_, err = buf.ReadFrom(resp.Body)
resp.Body.Close()
resp.Body = io.NopCloser(buf)

if err != nil {
return true, nil
} else {
return false, nil
}
}

0 comments on commit 775dba2

Please sign in to comment.