From 2e19c246b6702636859adba79f5c1f31b645761b Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Tue, 1 Feb 2022 13:38:48 -0500 Subject: [PATCH] feat: add 3 retries for the fallback HTTP downloads --- main.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 3b69bc4..347477b 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -318,5 +319,31 @@ 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: 3, + }) +} + +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 + } + lastErr = err + } + return nil, fmt.Errorf("exceeded number of retries. last error was %w", lastErr) +} + +func (r *retryFetcher) Close() error { + return r.Fetcher.Close() }