diff --git a/README.md b/README.md index d67eab1..cf0c7db 100644 --- a/README.md +++ b/README.md @@ -1176,6 +1176,16 @@ import "github.com/duke-git/lancet/v2/retry" - **RetryTimes** : set times of retry. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryTimes)] [[play](https://go.dev/play/p/ssfVeU2SwLO)] +- **BackoffStrategy** : An interface that defines a method for calculating backoff intervals. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#BackoffStrategy)] +- **RetryWithCustomBackoff** : set abitary custom backoff strategy. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithCustomBackoff)] +- **RetryWithLinearBackoff** : set linear strategy backoff. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithLinearBackoff)] +- **RetryWithExponentialWithJitterBackoff** : set exponential strategy backoff. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)] + +
定义计算退避间隔的方法的接口。
+ +函数签名: + +```go +// BackoffStrategy is an interface that defines a method for calculating backoff intervals. +type BackoffStrategy interface { + // CalculateInterval returns the time.Duration after which the next retry attempt should be made. + CalculateInterval() time.Duration +} +``` + +示例: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +type ExampleCustomBackoffStrategy struct { + interval time.Duration +} + +func (c *ExampleCustomBackoffStrategy) CalculateInterval() time.Duration { + return c.interval + 1 +} + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&示例CustomBackoffStrategy{interval: time.Microsecond * 50})) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + +### RetryWithCustomBackoff + +设置自定义退避策略。
+ +函数签名: + +```go +func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option +``` + +示例: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +type ExampleCustomBackoffStrategy struct { + interval time.Duration +} + +func (c *ExampleCustomBackoffStrategy) CalculateInterval() time.Duration { + return c.interval + 1 +} + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&示例CustomBackoffStrategy{interval: time.Microsecond * 50})) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + + +### RetryWithLinearBackoff + +设置线性策略退避。
+ +函数签名: + +```go +func RetryWithLinearBackoff(interval time.Duration) Option +``` + +示例: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry.Retry(increaseNumber, retry.RetryWithLinearBackoff(time.Microsecond*50)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + + +### RetryWithExponentialWithJitterBackoff + +设置指数策略退避。
+ +函数签名: + +```go +func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option +``` + +示例: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry.Retry(increaseNumber, retry.RetryWithExponentialWithJitterBackoff(time.Microsecond*50, 2, time.Microsecond*25)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` diff --git a/docs/en/api/packages/retry.md b/docs/en/api/packages/retry.md index 734de3b..553c453 100644 --- a/docs/en/api/packages/retry.md +++ b/docs/en/api/packages/retry.md @@ -27,6 +27,10 @@ import ( - [RetryFunc](#RetryFunc) - [RetryDuration](#RetryDuration) - [RetryTimes](#RetryTimes) +- [BackoffStrategy](#BackoffStrategy) +- [RetryWithCustomBackoff](#RetryWithCustomBackoff) +- [RetryWithLinearBackoff](#RetryWithLinearBackoff) +- [RetryWithExponentialWithJitterBackoff](#RetryWithExponentialWithJitterBackoff) @@ -259,3 +263,202 @@ func main() { // 3 } ``` + + +### BackoffStrategy + +An interface that defines a method for calculating backoff intervals.
+ +Signature: + +```go +// BackoffStrategy is an interface that defines a method for calculating backoff intervals. +type BackoffStrategy interface { + // CalculateInterval returns the time.Duration after which the next retry attempt should be made. + CalculateInterval() time.Duration +} +``` + +Example: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +type ExampleCustomBackoffStrategy struct { + interval time.Duration +} + +func (c *ExampleCustomBackoffStrategy) CalculateInterval() time.Duration { + return c.interval + 1 +} + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50})) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + +### RetryWithCustomBackoff + +Set abitary custom backoff strategy.
+ +Signature: + +```go +func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option +``` + +Example: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +type ExampleCustomBackoffStrategy struct { + interval time.Duration +} + +func (c *ExampleCustomBackoffStrategy) CalculateInterval() time.Duration { + return c.interval + 1 +} + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50})) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + + +### RetryWithLinearBackoff + +Set linear strategy backoff.
+ +Signature: + +```go +func RetryWithLinearBackoff(interval time.Duration) Option +``` + +Example: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry.Retry(increaseNumber, retry.RetryWithLinearBackoff(time.Microsecond*50)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` + + +### RetryWithExponentialWithJitterBackoff + +Set exponential strategy backoff.
+ +Signature: + +```go +func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option +``` + +Example: + +```go +package main + +import ( + "fmt" + "errors" + "log" + "github.com/duke-git/lancet/v2/retry" +) + +func main() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := retry.Retry(increaseNumber, retry.RetryWithExponentialWithJitterBackoff(time.Microsecond*50, 2, time.Microsecond*25)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} +``` diff --git a/retry/retry.go b/retry/retry.go index 7f3c766..8198f1f 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -45,7 +45,7 @@ func RetryTimes(n uint) Option { } // RetryWithCustomBackoff set abitary custom backoff strategy -// todo: Add playground link +// Play: todo func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option { if backoffStrategy == nil { panic("programming error: backoffStrategy must be not nil") @@ -57,7 +57,7 @@ func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option { } // RetryWithLinearBackoff set linear strategy backoff -// todo: Add playground link +// Play: todo func RetryWithLinearBackoff(interval time.Duration) Option { if interval <= 0 { panic("programming error: retry interval should not be lower or equal to 0") @@ -71,7 +71,7 @@ func RetryWithLinearBackoff(interval time.Duration) Option { } // RetryWithExponentialWithJitterBackoff set exponential strategy backoff -// todo: Add playground link +// Play: todo func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option { if interval <= 0 { panic("programming error: retry interval should not be lower or equal to 0")