Skip to content

Commit

Permalink
feat: add exponential backoff for go-cat push
Browse files Browse the repository at this point in the history
  • Loading branch information
srevinsaju committed Nov 6, 2023
1 parent af1bed5 commit 3f26d8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ops/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ops
import (
"encoding/json"
"fmt"
"github.com/cenkalti/backoff/v4"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5"
"gitlab.com/sorcero/community/go-cat/config"
Expand Down Expand Up @@ -39,7 +40,7 @@ func PushWithDbQueue(cfg config.GlobalConfig, queueDB string) error {
}
infraMetaQueue.Title = cfg.Title

err = PushFromStorage(repo, fs, infraMetaQueue, cfg)
err = SafePushFromStorage(repo, fs, infraMetaQueue, cfg)
if err != nil {
logger.Fatal(err)
}
Expand All @@ -48,7 +49,25 @@ func PushWithDbQueue(cfg config.GlobalConfig, queueDB string) error {
panic(err)
}
return nil
}

func SafePushFromStorage(repo *git.Repository, fs billy.Filesystem, infraMetaQueue *infrastructure.MetadataGroup, cfg config.GlobalConfig) error {
operation := func() error {
err := PushFromStorage(repo, fs, infraMetaQueue, cfg)
if err != nil {
logger.Warn(err)
var errClone error
repo, fs, errClone = storage.Clone(cfg)
if errClone != nil {
panic(errClone)
}
}
if err != nil {
logger.Warn("retrying...")
}
return err
}
return backoff.Retry(operation, backoff.NewExponentialBackOff())
}

func PushFromStorage(repo *git.Repository, fs billy.Filesystem, infraMetaQueue *infrastructure.MetadataGroup, cfg config.GlobalConfig) error {
Expand Down
4 changes: 4 additions & 0 deletions ops/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ func SafeUpsertFromStorage(cfg config.GlobalConfig, repo *git.Repository, fs bil
operation := func() error {
err := UpsertFromStorage(cfg, repo, fs, infra)
if err != nil {
logger.Warn(err)
var errClone error
repo, fs, errClone = storage.Clone(cfg)
if errClone != nil {
panic(errClone)
}
}
if err != nil {
logger.Warn("retrying...")
}
return err
}
return backoff.Retry(operation, backoff.NewExponentialBackOff())
Expand Down

0 comments on commit 3f26d8d

Please sign in to comment.