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

fix(deps): update module github.com/cenkalti/backoff/v4 to v5 #120

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/thegeeklab/wp-git-clone
go 1.23.4

require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cenkalti/backoff/v5 v5.0.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.10.0
github.com/thegeeklab/wp-plugin-go/v4 v4.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7r
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v5 v5.0.0 h1:4ziwFuaVJicDO1ah1Nz1aXXV1caM28PFgf1V5TTFXew=
github.com/cenkalti/backoff/v5 v5.0.0/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
7 changes: 3 additions & 4 deletions plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var (
ErrTypeAssertionFailed = errors.New("type assertion failed")
)

//nolint:revive
func (p *Plugin) run(ctx context.Context) error {
if err := p.FlagsFromContext(); err != nil {
return fmt.Errorf("validation failed: %w", err)
Expand All @@ -38,7 +37,7 @@ func (p *Plugin) run(ctx context.Context) error {
return fmt.Errorf("validation failed: %w", err)
}

if err := p.Execute(); err != nil {
if err := p.Execute(ctx); err != nil {
return fmt.Errorf("execution failed: %w", err)
}

Expand Down Expand Up @@ -71,7 +70,7 @@ func (p *Plugin) Validate() error {
}

// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
func (p *Plugin) Execute(ctx context.Context) error {
var err error

homeDir := plugin_util.GetUserHomeDir()
Expand Down Expand Up @@ -166,7 +165,7 @@ func (p *Plugin) Execute() error {

switch {
case err != nil && shouldRetry(buf.String()):
return retryCmd(cmd)
return retryCmd(ctx, cmd)
case err != nil:
return err
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestClone(t *testing.T) {
Lfs: tt.lfs,
}

if err := plugin.Execute(); err != nil {
if err := plugin.Execute(context.Background()); err != nil {
t.Errorf("Expected successful clone. Got error. %s.", err)
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func TestCloneNonEmpty(t *testing.T) {
Lfs: tt.lfs,
}

if err := plugin.Execute(); err != nil {
if err := plugin.Execute(context.Background()); err != nil {
t.Errorf("Expected successful clone. Got error. %s.", err)
}

Expand Down
29 changes: 16 additions & 13 deletions plugin/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package plugin

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/cenkalti/backoff/v5"
"github.com/rs/zerolog/log"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v4/exec"
)
Expand All @@ -26,30 +27,32 @@ func shouldRetry(s string) bool {
return strings.Contains(s, "find remote ref")
}

func newBackoff(maxRetries uint64) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = daemonBackoffInitialInterval
b.Multiplier = daemonBackoffMultiplier
func retryCmd(ctx context.Context, cmd *plugin_exec.Cmd) error {
bf := backoff.NewExponentialBackOff()
bf.InitialInterval = daemonBackoffInitialInterval
bf.Multiplier = daemonBackoffMultiplier

return backoff.WithMaxRetries(b, maxRetries)
}

func retryCmd(cmd *plugin_exec.Cmd) error {
backoffOps := func() error {
bfOpts := func() (any, error) {
// copy the original command
retry := plugin_exec.Command(cmd.Cmd.Path, cmd.Cmd.Args...)
retry.Env = cmd.Env
retry.Stdout = cmd.Stdout
retry.Stderr = cmd.Stderr
retry.Dir = cmd.Dir

return retry.Run()
return nil, retry.Run()
}
backoffLog := func(err error, delay time.Duration) {

bfNotify := func(err error, delay time.Duration) {
log.Error().Msgf("failed to find remote ref: %v: retry in %s", err, delay.Truncate(time.Second))
}

return backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog)
_, err := backoff.Retry(ctx, bfOpts,
backoff.WithBackOff(bf),
backoff.WithMaxTries(daemonBackoffMaxRetries),
backoff.WithNotify(bfNotify))

return err
}

// WriteNetrc writes the netrc file.
Expand Down