Skip to content

Commit

Permalink
unexport timer type
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Dec 18, 2024
1 parent 60056ec commit e5db35e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 21 deletions.
7 changes: 3 additions & 4 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Notify func(error, time.Duration)
// retryOptions holds configuration settings for the retry mechanism.
type retryOptions struct {
BackOff BackOff // Strategy for calculating backoff periods.
Timer Timer // Timer to manage retry delays.
Timer timer // Timer to manage retry delays.
Notify Notify // Optional function to notify on each retry error.
MaxTries uint // Maximum number of retry attempts.
MaxElapsedTime time.Duration // Maximum total time for all retries.
Expand All @@ -33,9 +33,8 @@ func WithBackOff(b BackOff) RetryOption {
}
}

// WithTimer sets a custom timer for managing delays between retries.
// TODO: Decide whether to make this configurable.
func WithTimer(t Timer) RetryOption {
// withTimer sets a custom timer for managing delays between retries.
func withTimer(t timer) RetryOption {
return func(args *retryOptions) {
args.Timer = t
}
Expand Down
8 changes: 4 additions & 4 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRetry(t *testing.T) {
return false, errors.New("error")
}

_, err := Retry(context.Background(), f, WithBackOff(NewExponentialBackOff()), WithTimer(&testTimer{}))
_, err := Retry(context.Background(), f, WithBackOff(NewExponentialBackOff()), withTimer(&testTimer{}))
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
Expand All @@ -73,7 +73,7 @@ func TestRetryWithData(t *testing.T) {
return 1, errors.New("error")
}

res, err := Retry(context.Background(), f, WithBackOff(NewExponentialBackOff()), WithTimer(&testTimer{}))
res, err := Retry(context.Background(), f, WithBackOff(NewExponentialBackOff()), withTimer(&testTimer{}))
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestRetryContext(t *testing.T) {
return false, fmt.Errorf("error (%d)", i)
}

_, err := Retry(ctx, f, WithBackOff(NewConstantBackOff(time.Millisecond)), WithTimer(&testTimer{}))
_, err := Retry(ctx, f, WithBackOff(NewConstantBackOff(time.Millisecond)), withTimer(&testTimer{}))
if err == nil {
t.Errorf("error is unexpectedly nil")
}
Expand All @@ -134,7 +134,7 @@ func TestRetryPermanent(t *testing.T) {
return f()
},
WithBackOff(NewExponentialBackOff()),
WithTimer(&testTimer{}),
withTimer(&testTimer{}),
)

if shouldRetry && numRetries == 0 {
Expand Down
13 changes: 2 additions & 11 deletions ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Ticker struct {
C <-chan time.Time
c chan time.Time
b BackOff
timer Timer
timer timer
stop chan struct{}
stopOnce sync.Once
}
Expand All @@ -25,21 +25,12 @@ type Ticker struct {
// provided backoff policy (notably calling NextBackOff or Reset)
// while the ticker is running.
func NewTicker(b BackOff) *Ticker {
return NewTickerWithTimer(b, &defaultTimer{})
}

// NewTickerWithTimer returns a new Ticker with a custom timer.
// A default timer that uses system timer is used when nil is passed.
func NewTickerWithTimer(b BackOff, timer Timer) *Ticker {
if timer == nil {
timer = &defaultTimer{}
}
c := make(chan time.Time)
t := &Ticker{
C: c,
c: c,
b: b,
timer: timer,
timer: &defaultTimer{},
stop: make(chan struct{}),
}
t.b.Reset()
Expand Down
3 changes: 2 additions & 1 deletion ticker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestTicker(t *testing.T) {
}

b := NewExponentialBackOff()
ticker := NewTickerWithTimer(b, &testTimer{})
ticker := NewTicker(b)
ticker.timer = &testTimer{}

var err error
for range ticker.C {
Expand Down
2 changes: 1 addition & 1 deletion timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package backoff

import "time"

type Timer interface {
type timer interface {
Start(duration time.Duration)
Stop()
C() <-chan time.Time
Expand Down

0 comments on commit e5db35e

Please sign in to comment.