mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
refactor: simplify retries (#1407)
* fix: simplify retries Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: simplify retries Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: simplify retries Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * chore: syntax Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
parent
6459cfc0ac
commit
cca25688d0
1
go.mod
1
go.mod
@ -14,7 +14,6 @@ require (
|
||||
github.com/goreleaser/nfpm v1.2.1
|
||||
github.com/imdario/mergo v0.3.9
|
||||
github.com/jarcoal/httpmock v1.0.5
|
||||
github.com/kamilsk/retry/v4 v4.7.2
|
||||
github.com/mattn/go-zglob v0.0.1
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
|
2
go.sum
2
go.sum
@ -153,8 +153,6 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/kamilsk/retry/v4 v4.7.2 h1:8C33aqTQtTSvPf7MpLZ4xSY4JZK2YCvY+hTlsbiHNq8=
|
||||
github.com/kamilsk/retry/v4 v4.7.2/go.mod h1:pIQtBtycHTXScrJmpu3N2SSBT07s07Uruq2Au1aRLks=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
|
@ -47,7 +47,3 @@ type RetriableError struct {
|
||||
func (e RetriableError) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
func (e RetriableError) Retriable() bool {
|
||||
return true
|
||||
}
|
||||
|
@ -11,9 +11,6 @@ import (
|
||||
"github.com/goreleaser/goreleaser/internal/pipe"
|
||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/kamilsk/retry/v4"
|
||||
"github.com/kamilsk/retry/v4/backoff"
|
||||
"github.com/kamilsk/retry/v4/strategy"
|
||||
"github.com/mattn/go-zglob"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -165,17 +162,17 @@ func doPublish(ctx *context.Context, client client.Client) error {
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func upload(ctx *context.Context, client client.Client, releaseID string, artifact *artifact.Artifact) error {
|
||||
var repeats uint
|
||||
what := func(try uint) error {
|
||||
repeats = try + 1
|
||||
func upload(ctx *context.Context, cli client.Client, releaseID string, artifact *artifact.Artifact) error {
|
||||
var try int
|
||||
tryUpload := func() error {
|
||||
try++
|
||||
file, err := os.Open(artifact.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close() // nolint: errcheck
|
||||
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
|
||||
if err := client.Upload(ctx, releaseID, artifact, file); err != nil {
|
||||
if err := cli.Upload(ctx, releaseID, artifact, file); err != nil {
|
||||
log.WithField("try", try).
|
||||
WithField("artifact", artifact.Name).
|
||||
WithError(err).
|
||||
@ -184,15 +181,24 @@ func upload(ctx *context.Context, client client.Client, releaseID string, artifa
|
||||
}
|
||||
return nil
|
||||
}
|
||||
how := []func(uint, error) bool{
|
||||
strategy.Limit(10),
|
||||
strategy.Backoff(backoff.Linear(50 * time.Millisecond)),
|
||||
strategy.CheckError(false),
|
||||
|
||||
var err error
|
||||
loop:
|
||||
for try < 10 {
|
||||
err = tryUpload()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
switch err.(type) {
|
||||
case client.RetriableError:
|
||||
time.Sleep(time.Duration(try*50) * time.Millisecond)
|
||||
continue
|
||||
default:
|
||||
break loop
|
||||
}
|
||||
}
|
||||
if err := retry.Try(ctx, what, how...); err != nil {
|
||||
return errors.Wrapf(err, "failed to upload %s after %d tries", artifact.Name, repeats)
|
||||
}
|
||||
return nil
|
||||
|
||||
return errors.Wrapf(err, "failed to upload %s after %d tries", artifact.Name, try)
|
||||
}
|
||||
|
||||
func findFiles(ctx *context.Context) (map[string]string, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user