Skip to content

Commit 88a4c8d

Browse files
committed
Introduce UntilSucceeded option to improve semantic
1 parent 64083c1 commit 88a4c8d

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
Simple library for retry mechanism
1212

13-
slightly inspired by
13+
Slightly inspired by
1414
[Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
1515

1616
# SYNOPSIS
1717

18-
http get with retry:
18+
HTTP GET with retry:
1919

2020
url := "http://example.com"
2121
var body []byte
@@ -31,17 +31,17 @@ http get with retry:
3131
if err != nil {
3232
return err
3333
}
34-
3534
return nil
3635
},
3736
)
37+
3838
if err != nil {
3939
// handle error
4040
}
4141

4242
fmt.Println(string(body))
4343

44-
http get with retry with data:
44+
HTTP GET with retry with data:
4545

4646
url := "http://example.com"
4747

@@ -60,13 +60,14 @@ http get with retry with data:
6060
return body, nil
6161
},
6262
)
63+
6364
if err != nil {
6465
// handle error
6566
}
6667

6768
fmt.Println(string(body))
6869

69-
[next examples](https://github.com/avast/retry-go/tree/master/examples)
70+
[More examples](https://github.com/avast/retry-go/tree/master/examples)
7071

7172
# SEE ALSO
7273

@@ -241,10 +242,10 @@ used with that library.
241242
#### type OnRetryFunc
242243

243244
```go
244-
type OnRetryFunc func(n uint, err error)
245+
type OnRetryFunc func(attempt uint, err error)
245246
```
246247

247-
Function signature of OnRetry function n = count of attempts
248+
Function signature of OnRetry function
248249

249250
#### type Option
250251

@@ -366,7 +367,7 @@ skip retry if special error example:
366367
return false
367368
}
368369
return true
369-
}),
370+
})
370371
)
371372
372373
By default RetryIf stops execution if the error is wrapped using
@@ -378,6 +379,14 @@ By default RetryIf stops execution if the error is wrapped using
378379
}
379380
)
380381
382+
#### func UntilSucceeded
383+
384+
```go
385+
func UntilSucceeded() Option
386+
```
387+
UntilSucceeded will retry until the retried function succeeds. Equivalent to
388+
setting Attempts(0).
389+
381390
#### func WithTimer
382391
383392
```go

options.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
type RetryIfFunc func(error) bool
1212

1313
// Function signature of OnRetry function
14-
// n = count of attempts
15-
type OnRetryFunc func(n uint, err error)
14+
type OnRetryFunc func(attempt uint, err error)
1615

1716
// DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.
1817
type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
@@ -60,6 +59,13 @@ func Attempts(attempts uint) Option {
6059
}
6160
}
6261

62+
// UntilSucceeded will retry until the retried function succeeds. Equivalent to setting Attempts(0).
63+
func UntilSucceeded() Option {
64+
return func(c *Config) {
65+
c.attempts = 0
66+
}
67+
}
68+
6369
// AttemptsForError sets count of retry in case execution results in given `err`
6470
// Retries for the given `err` are also counted against total retries.
6571
// The retry will stop if any of given retries is exhausted.

retry.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
Simple library for retry mechanism
33
4-
slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
4+
Slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
55
66
# SYNOPSIS
77
8-
http get with retry:
8+
HTTP GET with retry:
99
1010
url := "http://example.com"
1111
var body []byte
@@ -21,17 +21,17 @@ http get with retry:
2121
if err != nil {
2222
return err
2323
}
24-
2524
return nil
2625
},
2726
)
27+
2828
if err != nil {
2929
// handle error
3030
}
3131
3232
fmt.Println(string(body))
3333
34-
http get with retry with data:
34+
HTTP GET with retry with data:
3535
3636
url := "http://example.com"
3737
@@ -50,13 +50,14 @@ http get with retry with data:
5050
return body, nil
5151
},
5252
)
53+
5354
if err != nil {
5455
// handle error
5556
}
5657
5758
fmt.Println(string(body))
5859
59-
[next examples](https://github.com/avast/retry-go/tree/master/examples)
60+
[More examples](https://github.com/avast/retry-go/tree/master/examples)
6061
6162
# SEE ALSO
6263

0 commit comments

Comments
 (0)