https://github.com/nidorx/retry
Simple golang library for retry mechanism
https://github.com/nidorx/retry
Last synced: 5 months ago
JSON representation
Simple golang library for retry mechanism
- Host: GitHub
- URL: https://github.com/nidorx/retry
- Owner: nidorx
- License: mit
- Created: 2023-11-19T19:14:55.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-19T23:33:16.000Z (almost 2 years ago)
- Last Synced: 2025-04-30T05:04:03.856Z (5 months ago)
- Language: Go
- Size: 6.84 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Retry
[](https://github.com/nidorx/retry/actions/workflows/ci.yml)
Simple golang library for retry mechanism
```go
package mainimport (
"context"
"errors"
"fmt"
"github.com/nidorx/retry"
)func main() {
numOfRetries := 3
logErrors := func(ctx context.Context, err error, attempt int, willRetry bool, nextRetry time.Duration) {
fmt.Println("Something happened")
fmt.Println(err)
if willRetry {
fmt.Println("Retrying in " + (nextRetry).String() + "...")
}
}// 1) create a retry strategy (FixedBackOff 1000ms)
retries := retry.New(numOfRetries, logErrors)
// 2) execute your code
err := retries.Execute(context.Background(), func(ctx context.Context, attempt int) error {
if attempt <= 2 {
return errors.New("My Custom Error")
}
return nil
})
// 3) check for error
if err == nil {
fmt.Println(err)
}
}
```## FixedBackOff
```go
retries.SetFixedBackOff(500)// retry 1 = +500ms
// retry 2 = +500ms
```## ExponentialBackoff
```go
// initTime - in milliseconds for which the execution is suspended after the first attempt
// maxTime - in milliseconds for which the execution can be suspended
// factor - is the base of the power by which the waiting time increasesinitTime := 500
maxTime := 5000
factor := 2retries.SetExponentialBackoff(initTime, maxTime, factor)
// retry 1 = +500ms = Math.pow(2, 0)*500
// retry 2 = +1000ms = Math.pow(2, 1)*500
// retry 3 = +2000ms = Math.pow(2, 2)*500
// retry 4 = +4000ms = Math.pow(2, 3)*500
// retry 5 = +5000ms = Math.pow(2, 4)*500 = 8000 > 5000
// retry 6 = +5000ms = Math.pow(2, 5)*500 = 16000 > 5000
```## Custom Backoff
```go
type CustomBackoff struct {
}func (b *CustomBackoff) Next(attempt int) int {
return 200
}retries.Backoff = &CustomBackoff{}
```