Skip to content

Commit e5db35e

Browse files
committed
unexport timer type
1 parent 60056ec commit e5db35e

File tree

5 files changed

+12
-21
lines changed

5 files changed

+12
-21
lines changed

retry.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Notify func(error, time.Duration)
1818
// retryOptions holds configuration settings for the retry mechanism.
1919
type retryOptions struct {
2020
BackOff BackOff // Strategy for calculating backoff periods.
21-
Timer Timer // Timer to manage retry delays.
21+
Timer timer // Timer to manage retry delays.
2222
Notify Notify // Optional function to notify on each retry error.
2323
MaxTries uint // Maximum number of retry attempts.
2424
MaxElapsedTime time.Duration // Maximum total time for all retries.
@@ -33,9 +33,8 @@ func WithBackOff(b BackOff) RetryOption {
3333
}
3434
}
3535

36-
// WithTimer sets a custom timer for managing delays between retries.
37-
// TODO: Decide whether to make this configurable.
38-
func WithTimer(t Timer) RetryOption {
36+
// withTimer sets a custom timer for managing delays between retries.
37+
func withTimer(t timer) RetryOption {
3938
return func(args *retryOptions) {
4039
args.Timer = t
4140
}

retry_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestRetry(t *testing.T) {
4646
return false, errors.New("error")
4747
}
4848

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

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

110-
_, err := Retry(ctx, f, WithBackOff(NewConstantBackOff(time.Millisecond)), WithTimer(&testTimer{}))
110+
_, err := Retry(ctx, f, WithBackOff(NewConstantBackOff(time.Millisecond)), withTimer(&testTimer{}))
111111
if err == nil {
112112
t.Errorf("error is unexpectedly nil")
113113
}
@@ -134,7 +134,7 @@ func TestRetryPermanent(t *testing.T) {
134134
return f()
135135
},
136136
WithBackOff(NewExponentialBackOff()),
137-
WithTimer(&testTimer{}),
137+
withTimer(&testTimer{}),
138138
)
139139

140140
if shouldRetry && numRetries == 0 {

ticker.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Ticker struct {
1313
C <-chan time.Time
1414
c chan time.Time
1515
b BackOff
16-
timer Timer
16+
timer timer
1717
stop chan struct{}
1818
stopOnce sync.Once
1919
}
@@ -25,21 +25,12 @@ type Ticker struct {
2525
// provided backoff policy (notably calling NextBackOff or Reset)
2626
// while the ticker is running.
2727
func NewTicker(b BackOff) *Ticker {
28-
return NewTickerWithTimer(b, &defaultTimer{})
29-
}
30-
31-
// NewTickerWithTimer returns a new Ticker with a custom timer.
32-
// A default timer that uses system timer is used when nil is passed.
33-
func NewTickerWithTimer(b BackOff, timer Timer) *Ticker {
34-
if timer == nil {
35-
timer = &defaultTimer{}
36-
}
3728
c := make(chan time.Time)
3829
t := &Ticker{
3930
C: c,
4031
c: c,
4132
b: b,
42-
timer: timer,
33+
timer: &defaultTimer{},
4334
stop: make(chan struct{}),
4435
}
4536
t.b.Reset()

ticker_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func TestTicker(t *testing.T) {
2525
}
2626

2727
b := NewExponentialBackOff()
28-
ticker := NewTickerWithTimer(b, &testTimer{})
28+
ticker := NewTicker(b)
29+
ticker.timer = &testTimer{}
2930

3031
var err error
3132
for range ticker.C {

timer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package backoff
22

33
import "time"
44

5-
type Timer interface {
5+
type timer interface {
66
Start(duration time.Duration)
77
Stop()
88
C() <-chan time.Time

0 commit comments

Comments
 (0)