Skip to content

Commit

Permalink
feat(gobblr): add an exponential retry before declaring a failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent 15b020e commit 460fff6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
var dbOpts struct {
DataPath string
Driver drivers.Driver
Retries uint64
}

// dbCmd represents the db command
Expand All @@ -43,7 +44,7 @@ var dbCmd = &cobra.Command{
//
// There can be only one PersistentPostRun command.

inserted, err := gobblr.Execute(dbOpts.DataPath, dbOpts.Driver)
inserted, err := gobblr.Execute(dbOpts.DataPath, dbOpts.Driver, dbOpts.Retries)
if err != nil {
return err
}
Expand All @@ -65,5 +66,7 @@ func init() {
cobra.CheckErr(err)

viper.SetDefault("path", path.Join(currentPath, "data"))
viper.SetDefault("retries", 0)
dbCmd.PersistentFlags().StringVar(&dbOpts.DataPath, "path", viper.GetString("path"), "location of the data files")
dbCmd.PersistentFlags().Uint64Var(&dbOpts.Retries, "retries", viper.GetUint64("retries"), "number of retries before declaring a failure")
}
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/mrsimonemms/gobblr
go 1.19

require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
gorm.io/driver/mysql v1.4.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down
12 changes: 11 additions & 1 deletion pkg/gobblr/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gobblr
import (
"fmt"

"github.com/cenkalti/backoff/v4"
"github.com/mrsimonemms/gobblr/pkg/drivers"
)

Expand All @@ -11,7 +12,16 @@ type Inserted struct {
Count int `json:"count"`
}

func Execute(dataPath string, db drivers.Driver) ([]Inserted, error) {
func Execute(dataPath string, db drivers.Driver, retries uint64) ([]Inserted, error) {
return backoff.RetryWithData(
func() ([]Inserted, error) {
return retryExecute(dataPath, db)
},
backoff.WithMaxRetries(backoff.NewExponentialBackOff(), retries),
)
}

func retryExecute(dataPath string, db drivers.Driver) ([]Inserted, error) {
inserted := make([]Inserted, 0)
var err error

Expand Down

0 comments on commit 460fff6

Please sign in to comment.