- Notifications
You must be signed in to change notification settings - Fork0
Flexible and configurable retry mechanism for Go.
License
NotificationsYou must be signed in to change notification settings
itpey/retry
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Flexible and configurable retry mechanism for Go. With RETRY, you can easily implement retry logic with customizable backoff strategies to enhance the resilience of your applications.
- Configurable Retries: Set the maximum number of retries to control how many times an operation is attempted.
- Backoff Strategies: Define initial and maximum backoff durations with optional jitter to avoid synchronized retries.
- Jitter Support: Add randomness to backoff durations to prevent thundering herd problems.
- Context Support: Utilize Go contexts for cancellation and timeouts, ensuring operations can be gracefully terminated.
- Thread-Safe: Concurrently modify retry configurations safely across multiple goroutines.
Make sure you have Go installed and configured on your system. Use go install to installRETRY
:
go get -u github.com/itpey/retry
package mainimport ("context""errors""fmt""github.com/itpey/retry")funcmain() {r:=retry.New(retry.Config{MaxAttemptTimes:10,})fn:=func()error {returnerrors.New("temporary error")}ctx:=context.Background()iferr:=r.Do(ctx,fn);err!=nil {ifretry.IsMaxRetriesError(err) {fmt.Println("Failed after maximum number of retries")}else {fmt.Printf("Failed with error: %v\n",err)}}else {fmt.Println("Operation succeeded")}}
package mainimport ("context""errors""fmt""time""github.com/itpey/retry")funcmain() {r:=retry.New(retry.Config{MaxAttemptTimes:5,InitialBackoff:100*time.Millisecond,MaxBackoff:1*time.Second,MaxJitter:50*time.Millisecond,})fn:=func()error {returnerrors.New("temporary error")}ctx:=context.Background()iferr:=r.Do(ctx,fn);err!=nil {ifretry.IsMaxRetriesError(err) {fmt.Println("Failed after maximum number of retries")}else {fmt.Printf("Failed with error: %v\n",err)}}else {fmt.Println("Operation succeeded")}}
If you encounter any issues or have suggestions for improvement, pleaseopen an issue on GitHub.
We welcome contributions! Fork the repository, make your changes, and submit a pull request.
RETRY is open-source software released under the Apache License, Version 2.0. You can find a copy of the license in theLICENSE file.
RETRY was created byitpey
About
Flexible and configurable retry mechanism for Go.