Skip to content

Commit e31ba39

Browse files
author
Jan Seidl
committed
rename functions based on petr-k review
1 parent a24d863 commit e31ba39

File tree

4 files changed

+65
-65
lines changed

4 files changed

+65
-65
lines changed

README.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ now implement via functions produces Options (aka `retry.OnRetryFunction`)
7676
#### func Do
7777

7878
```go
79-
func Do(retryableFunction Retryable, opts ...Option) error
79+
func Do(retryableFunc RetryableFunc, opts ...Option) error
8080
```
8181

8282
#### type Error
@@ -105,13 +105,13 @@ implementation of the `errwrap.Wrapper` interface in package
105105
[errwrap](https://github.com/hashicorp/errwrap) so that `retry.Error` can be
106106
used with that library.
107107

108-
#### type OnRetry
108+
#### type OnRetryFunc
109109

110110
```go
111-
type OnRetry func(n uint, err error)
111+
type OnRetryFunc func(n uint, err error)
112112
```
113113

114-
Function signature of OnRetry function n = count of tries
114+
Function signature of OnRetry function n = count of attempts
115115

116116
#### type Option
117117

@@ -121,60 +121,60 @@ type Option func(*config)
121121

122122
Option represents an option for retry.
123123

124+
#### func Attempts
125+
126+
```go
127+
func Attempts(attempts uint) Option
128+
```
129+
Attempts set count of retry default is 10
130+
124131
#### func Delay
125132

126133
```go
127134
func Delay(delay time.Duration) Option
128135
```
129136
Delay set delay between retry default are 1e5 units
130137

131-
#### func OnRetryFunction
138+
#### func OnRetry
132139

133140
```go
134-
func OnRetryFunction(onRetryFunction OnRetry) Option
141+
func OnRetry(onRetry OnRetryFunc) Option
135142
```
136-
OnRetryFunction function callback are called each retry
143+
OnRetry function callback are called each retry
137144

138145
log each retry example:
139146

140147
retry.Do(
141148
func() error {
142149
return errors.New("some error")
143150
},
144-
retry.OnRetryFunction(func(n unit, err error) {
151+
retry.OnRetry(func(n unit, err error) {
145152
log.Printf("#%d: %s\n", n, err)
146153
}),
147154
)
148155

149-
#### func RetryIfFunction
156+
#### func RetryIf
150157

151158
```go
152-
func RetryIfFunction(retryIfFunction RetryIfFunc) Option
159+
func RetryIf(retryIf RetryIfFunc) Option
153160
```
154-
RetryIfFunction controls whether a retry should be attempted after an error
155-
(assuming there are any retry attempts remaining)
161+
RetryIf controls whether a retry should be attempted after an error (assuming
162+
there are any retry attempts remaining)
156163

157164
skip retry if special error example:
158165

159166
retry.Do(
160167
func() error {
161168
return errors.New("special error")
162169
},
163-
retry.RetryIfFunction(func(err error) bool {
164-
if strings.Contains(err.Error, "special error") {
170+
retry.RetryIf(func(err error) bool {
171+
if err.Error() == "special error" {
165172
return false
166173
}
167174
return true
168175
})
169176
)
170177

171-
#### func Tries
172-
173-
```go
174-
func Tries(tries uint) Option
175-
```
176-
Tries set count of retry default is 10
177-
178178
#### func Units
179179

180180
```go
@@ -191,10 +191,10 @@ type RetryIfFunc func(error) bool
191191

192192
Function signature of retry if function
193193

194-
#### type Retryable
194+
#### type RetryableFunc
195195

196196
```go
197-
type Retryable func() error
197+
type RetryableFunc func() error
198198
```
199199

200200
Function signature of retryable function

options.go

+23-16
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ import (
44
"time"
55
)
66

7+
// Function signature of retry if function
8+
type RetryIfFunc func(error) bool
9+
10+
// Function signature of OnRetry function
11+
// n = count of attempts
12+
type OnRetryFunc func(n uint, err error)
13+
714
type config struct {
8-
tries uint
9-
delay time.Duration
10-
units time.Duration
11-
onRetryFunction OnRetry
12-
retryIfFunction RetryIfFunc
15+
attempts uint
16+
delay time.Duration
17+
units time.Duration
18+
onRetry OnRetryFunc
19+
retryIf RetryIfFunc
1320
}
1421

1522
// Option represents an option for retry.
1623
type Option func(*config)
1724

18-
// Tries set count of retry
25+
// Attempts set count of retry
1926
// default is 10
20-
func Tries(tries uint) Option {
27+
func Attempts(attempts uint) Option {
2128
return func(c *config) {
22-
c.tries = tries
29+
c.attempts = attempts
2330
}
2431
}
2532

@@ -39,25 +46,25 @@ func Units(units time.Duration) Option {
3946
}
4047
}
4148

42-
// OnRetryFunction function callback are called each retry
49+
// OnRetry function callback are called each retry
4350
//
4451
// log each retry example:
4552
//
4653
// retry.Do(
4754
// func() error {
4855
// return errors.New("some error")
4956
// },
50-
// retry.OnRetryFunction(func(n unit, err error) {
57+
// retry.OnRetry(func(n unit, err error) {
5158
// log.Printf("#%d: %s\n", n, err)
5259
// }),
5360
// )
54-
func OnRetryFunction(onRetryFunction OnRetry) Option {
61+
func OnRetry(onRetry OnRetryFunc) Option {
5562
return func(c *config) {
56-
c.onRetryFunction = onRetryFunction
63+
c.onRetry = onRetry
5764
}
5865
}
5966

60-
// RetryIfFunction controls whether a retry should be attempted after an error
67+
// RetryIf controls whether a retry should be attempted after an error
6168
// (assuming there are any retry attempts remaining)
6269
//
6370
// skip retry if special error example:
@@ -66,15 +73,15 @@ func OnRetryFunction(onRetryFunction OnRetry) Option {
6673
// func() error {
6774
// return errors.New("special error")
6875
// },
69-
// retry.RetryIfFunction(func(err error) bool {
76+
// retry.RetryIf(func(err error) bool {
7077
// if err.Error() == "special error" {
7178
// return false
7279
// }
7380
// return true
7481
// })
7582
// )
76-
func RetryIfFunction(retryIfFunction RetryIfFunc) Option {
83+
func RetryIf(retryIf RetryIfFunc) Option {
7784
return func(c *config) {
78-
c.retryIfFunction = retryIfFunction
85+
c.retryIf = retryIf
7986
}
8087
}

retry.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ BREAKING CHANGES
4949
5050
* `retry.Retry` function are changed to `retry.Do` function
5151
52-
* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are now implement via functions produces Options (aka `retry.OnRetryFunction`)
52+
* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are now implement via functions produces Options (aka `retry.OnRetry`)
5353
5454
5555
*/
@@ -61,42 +61,35 @@ import (
6161
"time"
6262
)
6363

64-
// Function signature of retry if function
65-
type RetryIfFunc func(error) bool
66-
6764
// Function signature of retryable function
68-
type Retryable func() error
69-
70-
// Function signature of OnRetry function
71-
// n = count of tries
72-
type OnRetry func(n uint, err error)
65+
type RetryableFunc func() error
7366

74-
func Do(retryableFunction Retryable, opts ...Option) error {
67+
func Do(retryableFunc RetryableFunc, opts ...Option) error {
7568
var n uint
7669

7770
//default
7871
config := &config{
79-
tries: 10,
80-
delay: 1e5,
81-
onRetryFunction: func(n uint, err error) {},
82-
retryIfFunction: func(err error) bool { return true },
72+
attempts: 10,
73+
delay: 1e5,
74+
onRetry: func(n uint, err error) {},
75+
retryIf: func(err error) bool { return true },
8376
}
8477

8578
//apply opts
8679
for _, opt := range opts {
8780
opt(config)
8881
}
8982

90-
errorLog := make(Error, config.tries)
83+
errorLog := make(Error, config.attempts)
9184

92-
for n < config.tries {
93-
err := retryableFunction()
85+
for n < config.attempts {
86+
err := retryableFunc()
9487

9588
if err != nil {
96-
config.onRetryFunction(n, err)
89+
config.onRetry(n, err)
9790
errorLog[n] = err
9891

99-
if !config.retryIfFunction(err) {
92+
if !config.retryIf(err) {
10093
break
10194
}
10295

@@ -125,7 +118,7 @@ func (e Error) Error() string {
125118
}
126119
}
127120

128-
return fmt.Sprintf("All retries fail:\n%s", strings.Join(logWithNumber, "\n"))
121+
return fmt.Sprintf("All attempts fail:\n%s", strings.Join(logWithNumber, "\n"))
129122
}
130123

131124
func lenWithoutNil(e Error) (count int) {

retry_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func TestDo(t *testing.T) {
1212
var retrySum uint
1313
err := Do(
1414
func() error { return errors.New("test") },
15-
OnRetryFunction(func(n uint, err error) { retrySum += n }),
15+
OnRetry(func(n uint, err error) { retrySum += n }),
1616
Units(time.Nanosecond),
1717
)
1818
assert.Error(t, err)
1919

20-
expectedErrorFormat := `All retries fail:
20+
expectedErrorFormat := `All attempts fail:
2121
#1: test
2222
#2: test
2323
#3: test
@@ -34,7 +34,7 @@ func TestDo(t *testing.T) {
3434
retrySum = 0
3535
err = Do(
3636
func() error { return nil },
37-
OnRetryFunction(func(n uint, err error) { retrySum += n }),
37+
OnRetry(func(n uint, err error) { retrySum += n }),
3838
)
3939
assert.NoError(t, err)
4040
assert.Equal(t, uint(0), retrySum, "no retry")
@@ -48,15 +48,15 @@ func TestDo(t *testing.T) {
4848
return errors.New("test")
4949
}
5050
},
51-
OnRetryFunction(func(n uint, err error) { retryCount++ }),
52-
RetryIfFunction(func(err error) bool {
51+
OnRetry(func(n uint, err error) { retryCount++ }),
52+
RetryIf(func(err error) bool {
5353
return err.Error() != "special"
5454
}),
5555
Units(time.Nanosecond),
5656
)
5757
assert.Error(t, err)
5858

59-
expectedErrorFormat = `All retries fail:
59+
expectedErrorFormat = `All attempts fail:
6060
#1: test
6161
#2: test
6262
#3: special`

0 commit comments

Comments
 (0)