Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ynori7/trytryagain
If at first you don't succeed... A simple library for executing an action with retries.
https://github.com/ynori7/trytryagain
go retries retry
Last synced: 28 days ago
JSON representation
If at first you don't succeed... A simple library for executing an action with retries.
- Host: GitHub
- URL: https://github.com/ynori7/trytryagain
- Owner: ynori7
- License: mit
- Created: 2020-05-13T14:18:37.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-20T11:31:12.000Z (over 1 year ago)
- Last Synced: 2024-08-03T23:27:16.608Z (4 months ago)
- Topics: go, retries, retry
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - trytryagain
README
# TryTryAgain [![GoDoc](https://godoc.org/github.com/ynori7/trytryagain?status.png)](https://godoc.org/github.com/ynori7/trytryagain) [![Build Status](https://travis-ci.org/ynori7/trytryagain.svg?branch=master)](https://travis-ci.com/github/ynori7/trytryagain) [![Coverage Status](https://coveralls.io/repos/github/ynori7/trytryagain/badge.svg?branch=master)](https://coveralls.io/github/ynori7/trytryagain?branch=master) [![Go Report Card](https://goreportcard.com/badge/ynori7/trytryagain)](https://goreportcard.com/report/github.com/ynori7/trytryagain)
This library provides a simple utility for performing an action with retries. TryTryAgain is thread-safe and takes
care of handling backoff and expired contexts. The library provides configuration for the backoff strategy, retry
counts, and a callback for errors.# How it works
A retrier is created with the specified options for maxAttempts, backoffFunc, and onErrorFunc. Then you simply call `Do`,
providing a function which wraps the action to be performed. This function should return an error and a boolean to indicate
whether the error is retriable or not.**Defaults:**
- The default max attempts is 3
- The default backoff strategy is exponential
- The default onError callback does nothing# Usage
Here is a trivial example:
```go
r := NewRetrier(
WithMaxAttempts(4),
WithOnError(func(err error) { fmt.Println(err.Error()) }),
WithBackoff(exponentialBackoff),
)err := r.Do(ctx, func() (error, bool) {
return fmt.Errorf("something went wrong"), true
})
```More detailed examples can be found in [examples](./examples)