Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/zero: fix ticker usage #4969

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading