https://github.com/gotidy/retry
Retrier operations with different strategies
https://github.com/gotidy/retry
backoff exponential exponential-backoff generic generics go golang retry
Last synced: 23 days ago
JSON representation
Retrier operations with different strategies
- Host: GitHub
- URL: https://github.com/gotidy/retry
- Owner: gotidy
- License: apache-2.0
- Created: 2022-07-15T15:44:30.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-07T11:50:42.000Z (over 2 years ago)
- Last Synced: 2024-06-21T03:13:27.421Z (almost 2 years ago)
- Topics: backoff, exponential, exponential-backoff, generic, generics, go, golang, retry
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Retry operation with different strategies
Retry operations with constant, delays and exponential backoff strategies.
[][godev] [][goreport]
[godev]: https://pkg.go.dev/github.com/gotidy/retry
[goreport]: https://goreportcard.com/report/github.com/gotidy/retry
## Installation
`go get github.com/gotidy/retry`
Required at least 1.18 version of Go compiler.
## Example
### Custom delays
```go
delays := retry.Delays[1*time.Second, 2*time.Second, 4*time.Second]
result, err := retry.DoR(ctx, delays, func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
```
### Exponential
```go
err := retry.Do(ctx, retry.Exponential(time.Second, 1.5, 0.5), func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
```
```go
result, err := retry.DoR(ctx, retry.TruncatedExponential(time.Second, 1.5, 0, 10*time.Second), func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
```
There are also other strategies such as Constant, Zero.
### Permanent error
If need to prevent retrying wrap error with `Permanent``.
```go
result, err := retry.DoR(ctx, retry.Constant(time.Second), func(ctx context.Context) (int, error) {
result, err := DoSomething()
switch {
case errors.Is(err, ErrInvalidArguments)
return retry.Permanent(err) // Prevent retrying.
default:
return err
}
return result, nil
})
```
[More](/examples_test.go)
## Documentation
[godev]
## License
[Apache 2.0](LICENSE)