Skip to content

Commit

Permalink
Merge pull request #98 from kubescape/backoff
Browse files Browse the repository at this point in the history
stop trying to connect to backend infinitely
  • Loading branch information
matthyx authored Dec 16, 2024
2 parents 86ed02f + fa833a5 commit bab9f93
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion adapters/incluster/v1/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (a *Adapter) Start(ctx context.Context) error {
go func() {
if err := backoff.RetryNotify(func() error {
return client.Start(ctx)
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(true), func(err error, d time.Duration) {
logger.L().Ctx(ctx).Warning("start client", helpers.Error(err),
helpers.String("resource", client.res.Resource),
helpers.String("retry in", d.String()))
Expand Down
4 changes: 2 additions & 2 deletions adapters/incluster/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Client) Start(ctx context.Context) error {
var err error
watchOpts.ResourceVersion, err = c.getExistingStorageObjects(ctx)
return err
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(true), func(err error, d time.Duration) {
logger.L().Ctx(ctx).Warning("get existing storage objects", helpers.Error(err),
helpers.String("resource", c.res.Resource),
helpers.String("retry in", d.String()))
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *Client) watchRetry(ctx context.Context, watchOpts metav1.ListOptions, e
}
eventQueue.Enqueue(event)
}
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(true), func(err error, d time.Duration) {
if !errors.Is(err, errWatchClosed) {
logger.L().Ctx(ctx).Warning("watch", helpers.Error(err),
helpers.String("resource", c.res.Resource),
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func main() {
return backoff.Permanent(fmt.Errorf("server rejected our client version <%s>, please update", version))
}
return err
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(false), func(err error, d time.Duration) {
logger.L().Ctx(ctx).Warning("connection error", helpers.Error(err),
helpers.String("retry in", d.String()))
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *Synchronizer) sendData(ctx context.Context, data []byte) {
}
}
return nil
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(true), func(err error, d time.Duration) {
logger.L().Ctx(ctx).Warning("send data", helpers.Error(err),
helpers.String("retry in", d.String()))
}); err != nil {
Expand Down Expand Up @@ -463,7 +463,7 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
return fmt.Errorf("invoke inPool: %w", err)
}
return nil
}, utils.NewBackOff(), func(err error, d time.Duration) {
}, utils.NewBackOff(true), func(err error, d time.Duration) {
logger.L().Ctx(ctx).Warning("process incoming messages", helpers.Error(err), helpers.String("retry in", d.String()))
}); err != nil {
return fmt.Errorf("giving up process incoming messages: %w", err)
Expand Down
6 changes: 4 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ func StringValueBigger(s1, s2 string) bool {
return i1 > i2
}

func NewBackOff() backoff.BackOff {
func NewBackOff(neverStop bool) backoff.BackOff {
b := backoff.NewExponentialBackOff()
// never stop retrying (unless PermanentError is returned)
b.MaxElapsedTime = 0
if neverStop {
b.MaxElapsedTime = 0
}
return b
}

Expand Down

0 comments on commit bab9f93

Please sign in to comment.