Skip to content

Commit 8ea1249

Browse files
author
Jan Seidl
committed
improve documentation and use golint notes
1 parent cb11d19 commit 8ea1249

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

retry.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
package retry
44

55
import (
6-
"errors"
76
"fmt"
87
"time"
98
)
109

10+
// Function signature of retryable function
1111
type Retryable func() error
12-
type OnRetry func(n uint, err error)
1312

14-
var defaultTries uint = 10
15-
var defaultDelay time.Duration = 1e5
13+
// Function signature of OnRetry function
14+
// n = count of tries
15+
type OnRetry func(n uint, err error)
1616

1717
// Retry - simple retry
1818
//
@@ -47,7 +47,7 @@ func RetryWithOpts(retryableFunction Retryable, opts RetryOpts) error {
4747
// is possible set OnRetry function callback
4848
// which are called each retry
4949
func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts RetryOpts) error {
50-
var n uint = 0
50+
var n uint
5151

5252
for n < opts.tries {
5353
err := retryableFunction()
@@ -64,5 +64,5 @@ func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts Retr
6464
n++
6565
}
6666

67-
return errors.New(fmt.Sprintf("All (%d) retries fail", opts.tries))
67+
return fmt.Errorf("All (%d) retries fail", opts.tries)
6868
}

retry_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestCustom(t *testing.T) {
11-
var retrySum uint = 0
11+
var retrySum uint
1212
err := RetryCustom(
1313
func() error { return errors.New("test") },
1414
func(n uint, err error) { retrySum += n },

retryopts.go

+14
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@ import (
44
"time"
55
)
66

7+
// Struct for configure retry
8+
// tries - count of tries
9+
// delay - waiting time
10+
// units - waiting time unit (for tests purpose)
711
type RetryOpts struct {
812
tries uint
913
delay time.Duration
1014
units time.Duration
1115
}
1216

17+
var defaultTries uint = 10
18+
var defaultDelay time.Duration = 1e5
19+
20+
// Create new RetryOpts struct with default values
21+
// default tries are 10
22+
// default delay are 1e5
23+
// default units are microsecond
1324
func NewRetryOpts() RetryOpts {
1425
return RetryOpts{tries: defaultTries, delay: defaultDelay, units: time.Microsecond}
1526
}
1627

28+
// Units setter
1729
func (opts RetryOpts) Units(timeUnit time.Duration) RetryOpts {
1830
opts.units = timeUnit
1931
return opts
2032
}
2133

34+
// Delay setter
2235
func (opts RetryOpts) Delay(delay time.Duration) RetryOpts {
2336
opts.delay = delay
2437
return opts
2538
}
2639

40+
// Tries setter
2741
func (opts RetryOpts) Tries(tries uint) RetryOpts {
2842
opts.tries = tries
2943
return opts

0 commit comments

Comments
 (0)