Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vthiery/retry
Yet another retrier \o/
https://github.com/vthiery/retry
go retry
Last synced: 30 days ago
JSON representation
Yet another retrier \o/
- Host: GitHub
- URL: https://github.com/vthiery/retry
- Owner: vthiery
- License: mit
- Created: 2020-08-24T16:44:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-30T17:56:43.000Z (over 1 year ago)
- Last Synced: 2024-09-27T09:04:24.616Z (3 months ago)
- Topics: go, retry
- Language: Go
- Homepage:
- Size: 43.9 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - retry
README
# Retry
[![PkgGoDev](https://pkg.go.dev/badge/vthiery/retry)](https://pkg.go.dev/github.com/vthiery/retry)
[![Go version](https://img.shields.io/github/go-mod/go-version/vthiery/retry.svg)](https://github.com/vthiery/retry)
[![Test Status](https://img.shields.io/github/workflow/status/vthiery/retry/Test?label=Tests)](https://github.com/vthiery/retry/workflows/Test/badge.svg)
[![GolangCI Lint](https://img.shields.io/github/workflow/status/vthiery/retry/Golangci-Lint?label=Lint)](https://github.com/vthiery/retry/workflows/Golangci-Lint/badge.svg)
![License](https://img.shields.io/github/license/vthiery/retry)## Description
Yet another retrier \o/
## Installation
```sh
go get -u github.com/vthiery/retry
```## Usage
```go
package mainimport (
"context"
"errors"
"fmt"
"time""github.com/vthiery/retry"
)var nonRetryableError = errors.New("a non-retryable error")
func main() {
// Define the retry strategy, with 10 attempts and an exponential backoff
retry := retry.New(
retry.WithMaxAttempts(10),
retry.WithBackoff(
retry.NewExponentialBackoff(
100*time.Millisecond, // minWait
1*time.Second, // maxWait
2*time.Millisecond, // maxJitter
),
),
retry.WithPolicy(
func(err error) bool {
return !errors.Is(err, nonRetryableError)
},
),
)// A cancellable context can be used to stop earlier
ctx, cancel := context.WithCancel(context.Background())
defer cancel()// Define the function that can be retried
operation := func(ctx context.Context) error {
fmt.Println("doing something...")
return errors.New("actually, can't do it 🤦")
}// Call the `retry.Do` to attempt to perform `fn`
if err := retry.Do(ctx, operation); err != nil {
fmt.Printf("failed to perform `fn`: %v\n", err)
}
}
```