mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
Add custom backoff setter (#176)
Users should have the capability to customize the backoff pattern and accordingly adjust it within the retry mechanism.
This commit is contained in:
@@ -51,6 +51,35 @@ func ExampleRetryWithLinearBackoff() {
|
||||
// 3
|
||||
}
|
||||
|
||||
type ExampleCustomBackoffStrategy struct {
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
func (c *ExampleCustomBackoffStrategy) CalculateInterval() time.Duration {
|
||||
return c.interval + 1
|
||||
}
|
||||
|
||||
func ExampleRetryWithCustomBackoff() {
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
return nil
|
||||
}
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
|
||||
func ExampleRetryWithExponentialWithJitterBackoff() {
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
|
||||
Reference in New Issue
Block a user