Skip to content

Commit 1d9a7a1

Browse files
committed
fix
1 parent f15400b commit 1d9a7a1

4 files changed

Lines changed: 50 additions & 50 deletions

File tree

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RETRY
88
</h1>
99

1010
<p align="center">
11-
RETRY is a flexible and configurable retry mechanism for Go. With RETRY, you can easily implement retry logic with customizable backoff strategies to enhance the resilience of your applications.
11+
Flexible and configurable retry mechanism for Go. With RETRY, you can easily implement retry logic with customizable backoff strategies to enhance the resilience of your applications.
1212
</p>
1313

1414
<p align="center">
@@ -52,16 +52,16 @@ import (
5252
)
5353

5454
func main() {
55-
retry := retry.New(
56-
Config{
55+
r := retry.New(
56+
retry.Config{
5757
maxAttemptTimes: 10,
5858
})
5959

6060
fn := func() error {
6161
return errors.New("temporary error")
6262
}
6363
ctx := context.Background()
64-
if err := retry.Do(ctx, fn); err != nil {
64+
if err := r.Do(ctx, fn); err != nil {
6565
if retry.IsMaxRetriesError(err) {
6666
fmt.Println("Failed after maximum number of retries")
6767
} else {
@@ -89,20 +89,20 @@ import (
8989
)
9090

9191
func main() {
92-
retry := retry.New(
93-
Config{
94-
maxAttemptTimes: 5,
95-
initialBackoff: 100 * time.Millisecond,
96-
maxBackoff: 1 * time.Second,
97-
maxJitter: 50 * time.Millisecond,
92+
r := retry.New(
93+
retry.Config{
94+
MaxAttemptTimes: 5,
95+
InitialBackoff: 100 * time.Millisecond,
96+
MaxBackoff: 1 * time.Second,
97+
MaxJitter: 50 * time.Millisecond,
9898
})
9999

100100
fn := func() error {
101101
return errors.New("temporary error")
102102
}
103103

104104
ctx := context.Background()
105-
if err := retry.Do(ctx, fn); err != nil {
105+
if err := r.Do(ctx, fn); err != nil {
106106
if retry.IsMaxRetriesError(err) {
107107
fmt.Println("Failed after maximum number of retries")
108108
} else {

config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ import "time"
1818

1919
// Config defines the config for Retry.
2020
type Config struct {
21-
maxAttemptTimes uint // Maximum number of retries
22-
initialBackoff time.Duration // Initial backoff duration
23-
maxBackoff time.Duration // Maximum backoff duration
24-
maxJitter time.Duration // Jitter duration to add randomness to backoff
21+
MaxAttemptTimes uint // Maximum number of retries
22+
InitialBackoff time.Duration // Initial backoff duration
23+
MaxBackoff time.Duration // Maximum backoff duration
24+
MaxJitter time.Duration // Jitter duration to add randomness to backoff
2525
}
2626

2727
var ConfigDefault = Config{
28-
maxAttemptTimes: 3, // Default max retries
29-
initialBackoff: 0 * time.Millisecond, // Default initial backoff
30-
maxBackoff: 0 * time.Millisecond, // Default max backoff
31-
maxJitter: 0 * time.Millisecond, // Default Jitter
28+
MaxAttemptTimes: 3, // Default max retries
29+
InitialBackoff: 0 * time.Millisecond, // Default initial backoff
30+
MaxBackoff: 0 * time.Millisecond, // Default max backoff
31+
MaxJitter: 0 * time.Millisecond, // Default Jitter
3232
}
3333

3434
// Helper function to set default values

retry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ func New(config ...Config) *Retry {
4141
// Do retries the provided function asynchronously until it either succeeds (returns nil error),
4242
// decides not to retry (returns false), or reaches the maximum number of retries if specified.
4343
func (r *Retry) Do(ctx context.Context, fn func() error) error {
44-
maxRetries := r.cfg.maxAttemptTimes
45-
initialBackoff := r.cfg.initialBackoff
46-
maxBackoff := r.cfg.maxBackoff
47-
maxJitter := r.cfg.maxJitter
44+
maxRetries := r.cfg.MaxAttemptTimes
45+
initialBackoff := r.cfg.InitialBackoff
46+
maxBackoff := r.cfg.MaxBackoff
47+
maxJitter := r.cfg.MaxJitter
4848

4949
backoff := initialBackoff
5050
timer := time.NewTimer(0) // Initialize a timer

retry_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,40 @@ import (
2626
// TestNew tests the default values of a new Retry instance.
2727
func TestNew(t *testing.T) {
2828
r := New()
29-
if r.cfg.maxAttemptTimes != 3 {
30-
t.Errorf("Expected maxRetries to be 3, got %d", r.cfg.maxAttemptTimes)
29+
if r.cfg.MaxAttemptTimes != 3 {
30+
t.Errorf("Expected maxRetries to be 3, got %d", r.cfg.MaxAttemptTimes)
3131
}
32-
if r.cfg.initialBackoff != 0 {
33-
t.Errorf("Expected initialBackoff to be 0, got %d", r.cfg.initialBackoff)
32+
if r.cfg.InitialBackoff != 0 {
33+
t.Errorf("Expected initialBackoff to be 0, got %d", r.cfg.InitialBackoff)
3434
}
35-
if r.cfg.maxBackoff != 0 {
36-
t.Errorf("Expected maxBackoff to be 0, got %d", r.cfg.maxBackoff)
35+
if r.cfg.MaxBackoff != 0 {
36+
t.Errorf("Expected maxBackoff to be 0, got %d", r.cfg.MaxBackoff)
3737
}
38-
if r.cfg.maxJitter != 0 {
39-
t.Errorf("Expected jitter to be 0, got %d", r.cfg.maxJitter)
38+
if r.cfg.MaxJitter != 0 {
39+
t.Errorf("Expected jitter to be 0, got %d", r.cfg.MaxJitter)
4040
}
4141
}
4242

4343
// TestWithMaxRetries tests setting the maximum number of retries.
4444
func TestWithMaxAttemptTimes(t *testing.T) {
45-
r := New(Config{maxAttemptTimes: 5})
46-
if r.cfg.maxAttemptTimes != 5 {
47-
t.Errorf("Expected maxAttemptTimes to be 5, got %d", r.cfg.maxAttemptTimes)
45+
r := New(Config{MaxAttemptTimes: 5})
46+
if r.cfg.MaxAttemptTimes != 5 {
47+
t.Errorf("Expected maxAttemptTimes to be 5, got %d", r.cfg.MaxAttemptTimes)
4848
}
4949
}
5050

5151
// TestWithBackoff tests setting the backoff parameters.
5252
func TestWithBackoff(t *testing.T) {
5353
r := New(
54-
Config{initialBackoff: 100 * time.Millisecond, maxBackoff: 2 * time.Second, maxJitter: 50 * time.Millisecond})
55-
if r.cfg.initialBackoff != 100*time.Millisecond {
56-
t.Errorf("Expected initialBackoff to be 100ms, got %d", r.cfg.initialBackoff)
54+
Config{InitialBackoff: 100 * time.Millisecond, MaxBackoff: 2 * time.Second, MaxJitter: 50 * time.Millisecond})
55+
if r.cfg.InitialBackoff != 100*time.Millisecond {
56+
t.Errorf("Expected initialBackoff to be 100ms, got %d", r.cfg.InitialBackoff)
5757
}
58-
if r.cfg.maxBackoff != 2*time.Second {
59-
t.Errorf("Expected maxBackoff to be 2s, got %d", r.cfg.maxBackoff)
58+
if r.cfg.MaxBackoff != 2*time.Second {
59+
t.Errorf("Expected maxBackoff to be 2s, got %d", r.cfg.MaxBackoff)
6060
}
61-
if r.cfg.maxJitter != 50*time.Millisecond {
62-
t.Errorf("Expected jitter to be 50ms, got %d", r.cfg.maxJitter)
61+
if r.cfg.MaxJitter != 50*time.Millisecond {
62+
t.Errorf("Expected jitter to be 50ms, got %d", r.cfg.MaxJitter)
6363
}
6464
}
6565

@@ -78,7 +78,7 @@ func TestDoSuccess(t *testing.T) {
7878

7979
// TestDoMaxRetries tests the retry mechanism when the maximum number of retries is reached.
8080
func TestDoMaxRetries(t *testing.T) {
81-
r := New(Config{maxAttemptTimes: 3})
81+
r := New(Config{MaxAttemptTimes: 3})
8282
fn := func() error {
8383
return errors.New("some error")
8484
}
@@ -93,7 +93,7 @@ func TestDoMaxRetries(t *testing.T) {
9393
func TestDoFailsTwiceThenSucceeds(t *testing.T) {
9494

9595
r := New(
96-
Config{maxAttemptTimes: 5, initialBackoff: 100 * time.Millisecond, maxBackoff: 1 * time.Second, maxJitter: 50 * time.Millisecond})
96+
Config{MaxAttemptTimes: 5, InitialBackoff: 100 * time.Millisecond, MaxBackoff: 1 * time.Second, MaxJitter: 50 * time.Millisecond})
9797
attempts := 0
9898
fn := func() error {
9999
attempts++
@@ -125,7 +125,7 @@ func TestCalculateBackoff(t *testing.T) {
125125

126126
// TestBackoffDisabled tests the retry mechanism with backoff disabled.
127127
func TestBackoffDisabled(t *testing.T) {
128-
r := New(Config{maxAttemptTimes: 3})
128+
r := New(Config{MaxAttemptTimes: 3})
129129
fn := func() error {
130130
return errors.New("some error")
131131
}
@@ -143,7 +143,7 @@ func TestBackoffDisabled(t *testing.T) {
143143

144144
// TestBackoffEnabled tests the retry mechanism with backoff enabled.
145145
func TestBackoffEnabled(t *testing.T) {
146-
r := New(Config{maxAttemptTimes: 3, initialBackoff: 100 * time.Millisecond, maxBackoff: 1 * time.Second, maxJitter: 50 * time.Millisecond})
146+
r := New(Config{MaxAttemptTimes: 3, InitialBackoff: 100 * time.Millisecond, MaxBackoff: 1 * time.Second, MaxJitter: 50 * time.Millisecond})
147147
fn := func() error {
148148
return errors.New("some error")
149149
}
@@ -161,7 +161,7 @@ func TestBackoffEnabled(t *testing.T) {
161161

162162
// TestContextCancelled tests the retry mechanism when the context is cancelled.
163163
func TestContextCancelled(t *testing.T) {
164-
r := New(Config{maxAttemptTimes: 10, initialBackoff: 100 * time.Millisecond, maxBackoff: 1 * time.Second, maxJitter: 50 * time.Millisecond})
164+
r := New(Config{MaxAttemptTimes: 10, InitialBackoff: 100 * time.Millisecond, MaxBackoff: 1 * time.Second, MaxJitter: 50 * time.Millisecond})
165165
fn := func() error {
166166
return errors.New("some error")
167167
}
@@ -194,10 +194,10 @@ func TestRetryThreadSafety(t *testing.T) {
194194

195195
// Create a Retry instance with a maximum of maxAttempts
196196
retry := New(Config{
197-
maxAttemptTimes: maxAttempts,
198-
initialBackoff: 10 * time.Millisecond,
199-
maxBackoff: 100 * time.Millisecond,
200-
maxJitter: 10 * time.Millisecond,
197+
MaxAttemptTimes: maxAttempts,
198+
InitialBackoff: 10 * time.Millisecond,
199+
MaxBackoff: 100 * time.Millisecond,
200+
MaxJitter: 10 * time.Millisecond,
201201
})
202202

203203
// Use a WaitGroup to wait for all goroutines to finish

0 commit comments

Comments
 (0)