Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 3 retries for the fallback HTTP downloads #158

Merged
merged 5 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
35 changes: 34 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -318,5 +319,37 @@ func createFetcher(c *cli.Context) migrations.Fetcher {

return migrations.NewMultiFetcher(
lib.NewIpfsFetcher(distPath, 0),
migrations.NewHttpFetcher(distPath, customIpfsGatewayURL, userAgent, 0))
&retryFetcher{
Fetcher: migrations.NewHttpFetcher(distPath, customIpfsGatewayURL, userAgent, 0),
maxRetries: 10,
})
}

type retryFetcher struct {
migrations.Fetcher
maxRetries int
}

var _ migrations.Fetcher = (*retryFetcher)(nil)

func (r *retryFetcher) Fetch(ctx context.Context, filePath string) (io.ReadCloser, error) {
var lastErr error
for i := 0; i < r.maxRetries; i++ {
out, err := r.Fetcher.Fetch(ctx, filePath)
if err == nil {
return out, nil
}

if ctx.Err() != nil {
return nil, err
}

println("error fetching", i, filePath, err.Error())
lastErr = err
}
return nil, fmt.Errorf("exceeded number of retries. last error was %w", lastErr)
}

func (r *retryFetcher) Close() error {
return r.Fetcher.Close()
}
2 changes: 1 addition & 1 deletion sharness/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ clean-test-results:

$(T): clean-test-results deps
@echo "*** $@ ***"
./$@
./$@ -i -v

aggregate: clean-test-results $(T)
@echo "*** $@ ***"
Expand Down