A Go package that provides a backoff algorithm for retrying operations.
import "github.com/blizzy78/gobackoff"
const maxAttempts = 5
backoff := gobackoff.New( /* ... options ... */ )
_ = backoff.Do(context.Background(), func(ctx context.Context) error {
attempt := gobackoff.AttemptFromContext(ctx)
if attempt == 1 {
// simulate error
return io.EOF
}
fmt.Printf("success after %d attempts\n", attempt)
return nil
}, maxAttempts)
// Output: success after 2 attempts
This package is licensed under the MIT license.