-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(retier): Add
WithNotifier
option
`WithNotier` option sets a callback function that gets triggered on each retry attempt, providing feedback on errors and backoff.
- Loading branch information
1 parent
ef8a550
commit 73f1e3d
Showing
5 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
retrier "github.com/hueristiq/hq-go-retrier" | ||
"github.com/hueristiq/hq-go-retrier/backoff" | ||
) | ||
|
||
func main() { | ||
operation := func() error { | ||
// Simulate a failing operation | ||
fmt.Println("Trying operation...") | ||
return errors.New("operation failed") | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
// Retry the operation with custom configuration | ||
err := retrier.Retry(ctx, operation, | ||
retrier.WithMaxRetries(5), | ||
retrier.WithMinDelay(100*time.Millisecond), | ||
retrier.WithMaxDelay(1*time.Second), | ||
retrier.WithBackoff(backoff.ExponentialWithDecorrelatedJitter()), | ||
retrier.WithNotifier(func(err error, backoff time.Duration) { | ||
fmt.Printf("Operation failed: %v\n", err) | ||
fmt.Printf("...wait %d seconds for the next retry\n\n", backoff) | ||
}), | ||
) | ||
|
||
if err != nil { | ||
fmt.Printf("Operation failed after retries: %v\n", err) | ||
} else { | ||
fmt.Println("Operation succeeded") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters