Skip to content

Commit

Permalink
core/zero: fix ticker usage (#5019)
Browse files Browse the repository at this point in the history
core/zero: fix ticker usage (#4969)

Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
  • Loading branch information
backport-actions-token[bot] and calebdoxsey committed Mar 6, 2024
1 parent 15479cd commit 4fd72ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
9 changes: 3 additions & 6 deletions internal/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func Retry(
})

watches, backoff := newConfig(opts...)
ticker := time.NewTicker(backoff.NextBackOff())
defer ticker.Stop()

s := makeSelect(ctx, watches, name, ticker.C, fn)

restart:
for {
Expand All @@ -51,10 +47,11 @@ restart:
backoff:
for {
interval := backoff.NextBackOff()
ticker.Reset(interval)
log.Ctx(ctx).Warn().Msgf("backing off for %s...", interval.String())

timer := time.NewTimer(interval)
s := makeSelect(ctx, watches, name, timer.C, fn)
next, err := s.Exec(ctx)
timer.Stop()
logNext(ctx, next, err)
switch next {
case nextRestart:
Expand Down
27 changes: 7 additions & 20 deletions internal/zero/connect-mux/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"sync/atomic"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -52,28 +51,16 @@ func (svc *Mux) Run(ctx context.Context, opts ...fanout.Option) error {
}

func (svc *Mux) run(ctx context.Context) error {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 0

ticker := time.NewTicker(time.Microsecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}

err := svc.subscribeAndDispatch(ctx, bo.Reset)
if err != nil {
ticker.Reset(bo.NextBackOff())
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 0

return backoff.Retry(func() error {
err := svc.subscribeAndDispatch(ctx, b.Reset)
if apierror.IsTerminalError(err) {
return err
return backoff.Permanent(err)
}
}
return err
}, backoff.WithContext(b, ctx))
}

func (svc *Mux) subscribeAndDispatch(ctx context.Context, onConnected func()) (err error) {
Expand Down

0 comments on commit 4fd72ee

Please sign in to comment.