From cc68feb52dc3c0b536f4b52cfcceacbb3173afa7 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 7 Jan 2023 14:22:07 +0800 Subject: [PATCH] doc&test: add example and update doc for retry package --- README.md | 22 +++++++-- README_zh-CN.md | 22 +++++++-- retry/retry.go | 12 +++-- retry/retry_example_test.go | 93 +++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 retry/retry_example_test.go diff --git a/README.md b/README.md index 61d31c4..4838a01 100644 --- a/README.md +++ b/README.md @@ -588,11 +588,23 @@ import "github.com/duke-git/lancet/v2/retry" #### Function list: -- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context) -- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry) -- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc) -- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration) -- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes) +- **Context** : set retry context config option. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context)] + [[play](https://go.dev/play/p/xnAOOXv9GkS)] +- **Retry** : executes the retryFunc repeatedly until it was successful or canceled by the context. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryFunc** : function that retry executes. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryDuration** : set duration of retry + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryTimes** : set times of retry. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)] + [[play](https://go.dev/play/p/ssfVeU2SwLO)] + + ### 16. Slice contains some functions to manipulate slice. diff --git a/README_zh-CN.md b/README_zh-CN.md index 9096dcb..3d7559f 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -588,11 +588,23 @@ import "github.com/duke-git/lancet/v2/retry" #### 函数列表: -- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context) -- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry) -- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc) -- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration) -- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes) +- **Context** : 设置重试context参数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context)] + [[play](https://go.dev/play/p/xnAOOXv9GkS)] +- **Retry** : 重试执行函数retryFunc,直到函数运行成功,或被context取消。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryFunc** : 重试执行的函数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryDuration** : 设置重试间隔时间,默认3秒。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration)] + [[play](https://go.dev/play/p/nk2XRmagfVF)] +- **RetryTimes** : 设置重试次数,默认5。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)] + [[play](https://go.dev/play/p/ssfVeU2SwLO)] + + ### 16. slice 包含操作切片的方法集合。 diff --git a/retry/retry.go b/retry/retry.go index 59bbdfd..460883b 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -34,21 +34,24 @@ type RetryFunc func() error // Option is for adding retry config type Option func(*RetryConfig) -// RetryTimes set times of retry +// RetryTimes set times of retry. +// Play: https://go.dev/play/p/ssfVeU2SwLO func RetryTimes(n uint) Option { return func(rc *RetryConfig) { rc.retryTimes = n } } -// RetryDuration set duration of retries +// RetryDuration set duration of retries. +// Play: https://go.dev/play/p/nk2XRmagfVF func RetryDuration(d time.Duration) Option { return func(rc *RetryConfig) { rc.retryDuration = d } } -// Context set retry context config +// Context set retry context config. +// Play: https://go.dev/play/p/xnAOOXv9GkS func Context(ctx context.Context) Option { return func(rc *RetryConfig) { rc.context = ctx @@ -56,7 +59,8 @@ func Context(ctx context.Context) Option { } // Retry executes the retryFunc repeatedly until it was successful or canceled by the context -// The default times of retries is 5 and the default duration between retries is 3 seconds +// The default times of retries is 5 and the default duration between retries is 3 seconds. +// Play: https://go.dev/play/p/nk2XRmagfVF func Retry(retryFunc RetryFunc, opts ...Option) error { config := &RetryConfig{ retryTimes: DefaultRetryTimes, diff --git a/retry/retry_example_test.go b/retry/retry_example_test.go new file mode 100644 index 0000000..ac1d003 --- /dev/null +++ b/retry/retry_example_test.go @@ -0,0 +1,93 @@ +package retry + +import ( + "context" + "errors" + "fmt" + "time" +) + +func ExampleContext() { + ctx, cancel := context.WithCancel(context.TODO()) + + number := 0 + increaseNumber := func() error { + number++ + if number > 3 { + cancel() + } + return errors.New("error occurs") + } + + Retry(increaseNumber, + RetryDuration(time.Microsecond*50), + Context(ctx), + ) + + fmt.Println(number) + + // Output: + // 4 +} + +func ExampleRetryDuration() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := Retry(increaseNumber, RetryDuration(time.Microsecond*50)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +} + +func ExampleRetryTimes() { + number := 0 + + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := Retry(increaseNumber, RetryTimes(2)) + if err != nil { + fmt.Println(err) + } + + // Output: + // function retry.ExampleRetryTimes.func1 run failed after 2 times retry +} + +func ExampleRetry() { + number := 0 + increaseNumber := func() error { + number++ + if number == 3 { + return nil + } + return errors.New("error occurs") + } + + err := Retry(increaseNumber, RetryDuration(time.Microsecond*50)) + if err != nil { + return + } + + fmt.Println(number) + + // Output: + // 3 +}