Skip to content

Commit

Permalink
doc: add doc for retry package
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Mar 5, 2024
1 parent c58c503 commit d21edd1
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,16 @@ import "github.com/duke-git/lancet/v2/retry"
- **<big>RetryTimes</big>** : 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)]
- **<big>BackoffStrategy</big>** : 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)]
- **<big>RetryWithCustomBackoff</big>** : set abitary custom backoff strategy.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithCustomBackoff)]
- **<big>RetryWithLinearBackoff</big>** : set linear strategy backoff.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithLinearBackoff)]
- **<big>RetryWithExponentialWithJitterBackoff</big>** : set exponential strategy backoff.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]



<h3 id="slice"> 18. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

Expand Down
10 changes: 10 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,16 @@ import "github.com/duke-git/lancet/v2/retry"
- **<big>RetryTimes</big>** : 设置重试次数,默认 5。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)]
- **<big>BackoffStrategy</big>** : 定义计算退避间隔的方法的接口。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#BackoffStrategy)]
- **<big>RetryWithCustomBackoff</big>** : 设置自定义退避策略。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithCustomBackoff)]
- **<big>RetryWithLinearBackoff</big>** : 设置线性策略退避。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithLinearBackoff)]
- **<big>RetryWithExponentialWithJitterBackoff</big>** : 设置指数策略退避。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]



<h3 id="slice"> 18. slice 包含操作切片的方法集合。&nbsp; &nbsp; &nbsp; &nbsp; <a href="#index">回到目录</a></h3>

Expand Down
202 changes: 202 additions & 0 deletions docs/api/packages/retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
- [BackoffStrategy](#BackoffStrategy)
- [RetryWithCustomBackoff](#RetryWithCustomBackoff)
- [RetryWithLinearBackoff](#RetryWithLinearBackoff)
- [RetryWithExponentialWithJitterBackoff](#RetryWithExponentialWithJitterBackoff)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -260,3 +264,201 @@ func main() {
// 3
}
```

### <span id="BackoffStrategy">BackoffStrategy</span>

<p>定义计算退避间隔的方法的接口。</p>

<b>函数签名:</b>

```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
}
```

<b>示例:</b>

```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
}
```

### <span id="RetryWithCustomBackoff">RetryWithCustomBackoff</span>

<p>设置自定义退避策略。</p>

<b>函数签名:</b>

```go
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
```

<b>示例:</b>

```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
}
```


### <span id="RetryWithLinearBackoff">RetryWithLinearBackoff</span>

<p>设置线性策略退避。</p>

<b>函数签名:</b>

```go
func RetryWithLinearBackoff(interval time.Duration) Option
```

<b>示例:</b>

```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
}
```


### <span id="RetryWithExponentialWithJitterBackoff">RetryWithExponentialWithJitterBackoff</span>

<p>设置指数策略退避。</p>

<b>函数签名:</b>

```go
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
```

<b>示例:</b>

```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
}
```
Loading

0 comments on commit d21edd1

Please sign in to comment.