Skip to content

Commit

Permalink
rename functions based on petr-k review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Seidl committed Nov 24, 2017
1 parent a24d863 commit e31ba39
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ now implement via functions produces Options (aka `retry.OnRetryFunction`)
#### func Do

```go
func Do(retryableFunction Retryable, opts ...Option) error
func Do(retryableFunc RetryableFunc, opts ...Option) error
```

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

#### type OnRetry
#### type OnRetryFunc

```go
type OnRetry func(n uint, err error)
type OnRetryFunc func(n uint, err error)
```

Function signature of OnRetry function n = count of tries
Function signature of OnRetry function n = count of attempts

#### type Option

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

Option represents an option for retry.

#### func Attempts

```go
func Attempts(attempts uint) Option
```
Attempts set count of retry default is 10

#### func Delay

```go
func Delay(delay time.Duration) Option
```
Delay set delay between retry default are 1e5 units

#### func OnRetryFunction
#### func OnRetry

```go
func OnRetryFunction(onRetryFunction OnRetry) Option
func OnRetry(onRetry OnRetryFunc) Option
```
OnRetryFunction function callback are called each retry
OnRetry function callback are called each retry

log each retry example:

retry.Do(
func() error {
return errors.New("some error")
},
retry.OnRetryFunction(func(n unit, err error) {
retry.OnRetry(func(n unit, err error) {
log.Printf("#%d: %s\n", n, err)
}),
)

#### func RetryIfFunction
#### func RetryIf

```go
func RetryIfFunction(retryIfFunction RetryIfFunc) Option
func RetryIf(retryIf RetryIfFunc) Option
```
RetryIfFunction controls whether a retry should be attempted after an error
(assuming there are any retry attempts remaining)
RetryIf controls whether a retry should be attempted after an error (assuming
there are any retry attempts remaining)

skip retry if special error example:

retry.Do(
func() error {
return errors.New("special error")
},
retry.RetryIfFunction(func(err error) bool {
if strings.Contains(err.Error, "special error") {
retry.RetryIf(func(err error) bool {
if err.Error() == "special error" {
return false
}
return true
})
)

#### func Tries

```go
func Tries(tries uint) Option
```
Tries set count of retry default is 10

#### func Units

```go
Expand All @@ -191,10 +191,10 @@ type RetryIfFunc func(error) bool

Function signature of retry if function

#### type Retryable
#### type RetryableFunc

```go
type Retryable func() error
type RetryableFunc func() error
```

Function signature of retryable function
Expand Down
39 changes: 23 additions & 16 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@ import (
"time"
)

// Function signature of retry if function
type RetryIfFunc func(error) bool

// Function signature of OnRetry function
// n = count of attempts
type OnRetryFunc func(n uint, err error)

type config struct {
tries uint
delay time.Duration
units time.Duration
onRetryFunction OnRetry
retryIfFunction RetryIfFunc
attempts uint
delay time.Duration
units time.Duration
onRetry OnRetryFunc
retryIf RetryIfFunc
}

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

// Tries set count of retry
// Attempts set count of retry
// default is 10
func Tries(tries uint) Option {
func Attempts(attempts uint) Option {
return func(c *config) {
c.tries = tries
c.attempts = attempts
}
}

Expand All @@ -39,25 +46,25 @@ func Units(units time.Duration) Option {
}
}

// OnRetryFunction function callback are called each retry
// OnRetry function callback are called each retry
//
// log each retry example:
//
// retry.Do(
// func() error {
// return errors.New("some error")
// },
// retry.OnRetryFunction(func(n unit, err error) {
// retry.OnRetry(func(n unit, err error) {
// log.Printf("#%d: %s\n", n, err)
// }),
// )
func OnRetryFunction(onRetryFunction OnRetry) Option {
func OnRetry(onRetry OnRetryFunc) Option {
return func(c *config) {
c.onRetryFunction = onRetryFunction
c.onRetry = onRetry
}
}

// RetryIfFunction controls whether a retry should be attempted after an error
// RetryIf controls whether a retry should be attempted after an error
// (assuming there are any retry attempts remaining)
//
// skip retry if special error example:
Expand All @@ -66,15 +73,15 @@ func OnRetryFunction(onRetryFunction OnRetry) Option {
// func() error {
// return errors.New("special error")
// },
// retry.RetryIfFunction(func(err error) bool {
// retry.RetryIf(func(err error) bool {
// if err.Error() == "special error" {
// return false
// }
// return true
// })
// )
func RetryIfFunction(retryIfFunction RetryIfFunc) Option {
func RetryIf(retryIf RetryIfFunc) Option {
return func(c *config) {
c.retryIfFunction = retryIfFunction
c.retryIf = retryIf
}
}
33 changes: 13 additions & 20 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ BREAKING CHANGES
* `retry.Retry` function are changed to `retry.Do` function
* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are now implement via functions produces Options (aka `retry.OnRetryFunction`)
* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are now implement via functions produces Options (aka `retry.OnRetry`)
*/
Expand All @@ -61,42 +61,35 @@ import (
"time"
)

// Function signature of retry if function
type RetryIfFunc func(error) bool

// Function signature of retryable function
type Retryable func() error

// Function signature of OnRetry function
// n = count of tries
type OnRetry func(n uint, err error)
type RetryableFunc func() error

func Do(retryableFunction Retryable, opts ...Option) error {
func Do(retryableFunc RetryableFunc, opts ...Option) error {
var n uint

//default
config := &config{
tries: 10,
delay: 1e5,
onRetryFunction: func(n uint, err error) {},
retryIfFunction: func(err error) bool { return true },
attempts: 10,
delay: 1e5,
onRetry: func(n uint, err error) {},
retryIf: func(err error) bool { return true },
}

//apply opts
for _, opt := range opts {
opt(config)
}

errorLog := make(Error, config.tries)
errorLog := make(Error, config.attempts)

for n < config.tries {
err := retryableFunction()
for n < config.attempts {
err := retryableFunc()

if err != nil {
config.onRetryFunction(n, err)
config.onRetry(n, err)
errorLog[n] = err

if !config.retryIfFunction(err) {
if !config.retryIf(err) {
break
}

Expand Down Expand Up @@ -125,7 +118,7 @@ func (e Error) Error() string {
}
}

return fmt.Sprintf("All retries fail:\n%s", strings.Join(logWithNumber, "\n"))
return fmt.Sprintf("All attempts fail:\n%s", strings.Join(logWithNumber, "\n"))
}

func lenWithoutNil(e Error) (count int) {
Expand Down
12 changes: 6 additions & 6 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func TestDo(t *testing.T) {
var retrySum uint
err := Do(
func() error { return errors.New("test") },
OnRetryFunction(func(n uint, err error) { retrySum += n }),
OnRetry(func(n uint, err error) { retrySum += n }),
Units(time.Nanosecond),
)
assert.Error(t, err)

expectedErrorFormat := `All retries fail:
expectedErrorFormat := `All attempts fail:
#1: test
#2: test
#3: test
Expand All @@ -34,7 +34,7 @@ func TestDo(t *testing.T) {
retrySum = 0
err = Do(
func() error { return nil },
OnRetryFunction(func(n uint, err error) { retrySum += n }),
OnRetry(func(n uint, err error) { retrySum += n }),
)
assert.NoError(t, err)
assert.Equal(t, uint(0), retrySum, "no retry")
Expand All @@ -48,15 +48,15 @@ func TestDo(t *testing.T) {
return errors.New("test")
}
},
OnRetryFunction(func(n uint, err error) { retryCount++ }),
RetryIfFunction(func(err error) bool {
OnRetry(func(n uint, err error) { retryCount++ }),
RetryIf(func(err error) bool {
return err.Error() != "special"
}),
Units(time.Nanosecond),
)
assert.Error(t, err)

expectedErrorFormat = `All retries fail:
expectedErrorFormat = `All attempts fail:
#1: test
#2: test
#3: special`
Expand Down

0 comments on commit e31ba39

Please sign in to comment.