Skip to content

Commit

Permalink
feat: add backoff retry to docker version to avoid transient failure (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Sep 4, 2023
1 parent 3edc764 commit b419036
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ linters-settings:
gofumpt:
extra-rules: true
lang-version: "1.21"
ireturn:
allow:
- error
- backoff.BackOff
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/thegeeklab/wp-docker-buildx
go 1.21

require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/coreos/go-semver v0.3.1
github.com/rs/zerolog v1.30.0
github.com/thegeeklab/wp-plugin-go v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
Expand Down
45 changes: 37 additions & 8 deletions plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"github.com/cenkalti/backoff"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/types"
"github.com/urfave/cli/v2"
Expand All @@ -16,7 +17,12 @@ import (

var ErrTypeAssertionFailed = errors.New("type assertion failed")

const strictFilePerm = 0o600
const (
strictFilePerm = 0o600
daemonBackoffMaxRetries = 3
daemonBackoffInitialInterval = 2 * time.Second
daemonBackoffMultiplier = 3.5
)

//nolint:revive
func (p *Plugin) run(ctx context.Context) error {
Expand Down Expand Up @@ -149,16 +155,31 @@ func (p *Plugin) Execute() error {
// add proxy build args
addProxyBuildArgs(&p.Settings.Build)

var cmds []*execabs.Cmd
cmds = append(cmds, commandVersion()) // docker version
cmds = append(cmds, commandInfo()) // docker info
cmds = append(cmds, commandBuilder(p.Settings.Daemon))
cmds = append(cmds, commandBuildx())
versionCmd := commandVersion() // docker version

versionCmd.Stdout = os.Stdout
versionCmd.Stderr = os.Stderr
trace(versionCmd)

backoffOps := func() error {
return versionCmd.Run()
}
backoffLog := func(err error, delay time.Duration) {
log.Error().Msgf("failed to exec docker version: %v: retry in %s", err, delay.Truncate(time.Second))
}

if err := backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog); err != nil {
return err
}

cmds = append(cmds, commandBuild(p.Settings.Build, p.Settings.Dryrun)) // docker build
var batchCmd []*execabs.Cmd
batchCmd = append(batchCmd, commandInfo()) // docker info
batchCmd = append(batchCmd, commandBuilder(p.Settings.Daemon))
batchCmd = append(batchCmd, commandBuildx())
batchCmd = append(batchCmd, commandBuild(p.Settings.Build, p.Settings.Dryrun)) // docker build

// execute all commands in batch mode.
for _, cmd := range cmds {
for _, cmd := range batchCmd {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
Expand Down Expand Up @@ -189,3 +210,11 @@ func (p *Plugin) FlagsFromContext() error {

return nil
}

func newBackoff(maxRetries uint64) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = daemonBackoffInitialInterval
b.Multiplier = daemonBackoffMultiplier

return backoff.WithMaxRetries(b, maxRetries)
}

0 comments on commit b419036

Please sign in to comment.