1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: normalize document

This commit is contained in:
dudaodong
2023-01-14 12:32:27 +08:00
parent 4c5524354c
commit f976941e36
9 changed files with 2311 additions and 1617 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -56,8 +56,8 @@ import (
)
func main() {
randBytes := random.RandBytes(4)
fmt.Println(randBytes)
randBytes := random.RandBytes(4)
fmt.Println(randBytes)
}
```
@@ -82,8 +82,8 @@ import (
)
func main() {
rInt := random.RandInt(1, 10)
fmt.Println(rInt)
rInt := random.RandInt(1, 10)
fmt.Println(rInt)
}
```
@@ -108,8 +108,8 @@ import (
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze
randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze
}
```
@@ -134,8 +134,8 @@ import (
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
@@ -160,8 +160,8 @@ import (
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
@@ -186,8 +186,8 @@ import (
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
@@ -212,8 +212,8 @@ import (
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
@@ -238,10 +238,10 @@ import (
)
func main() {
uuid, err := random.UUIdV4()
uuid, err := random.UUIdV4()
if err != nil {
return
}
fmt.Println(uuid)
fmt.Println(uuid)
}
```

View File

@@ -56,8 +56,8 @@ import (
)
func main() {
randBytes := random.RandBytes(4)
fmt.Println(randBytes)
randBytes := random.RandBytes(4)
fmt.Println(randBytes)
}
```
@@ -82,8 +82,8 @@ import (
)
func main() {
rInt := random.RandInt(1, 10)
fmt.Println(rInt)
rInt := random.RandInt(1, 10)
fmt.Println(rInt)
}
```
@@ -108,8 +108,8 @@ import (
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze
randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze
}
```
@@ -134,8 +134,8 @@ import (
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
@@ -160,8 +160,8 @@ import (
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
@@ -186,8 +186,8 @@ import (
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
@@ -212,8 +212,8 @@ import (
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
@@ -238,10 +238,10 @@ import (
)
func main() {
uuid, err := random.UUIdV4()
uuid, err := random.UUIdV4()
if err != nil {
return
}
fmt.Println(uuid)
fmt.Println(uuid)
}
```

View File

@@ -1,16 +1,17 @@
# Retry
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
<div STYLE="page-break-after: always;"></div>
## Usage:
```go
import (
"github.com/duke-git/lancet/v2/retry"
@@ -20,18 +21,19 @@ import (
<div STYLE="page-break-after: always;"></div>
## Index
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="Context">Context</span>
<p>Set retry context config, can cancel the retry with context.</p>
<b>Signature:</b>
@@ -39,43 +41,46 @@ import (
```go
func Context(ctx context.Context)
```
<b>Example:</b>
```go
import (
"context"
"errors"
"fmt"
"github.com/duke-git/lancet/v2/retry"
"time"
"context"
"errors"
"fmt"
"github.com/duke-git/lancet/v2/retry"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.TODO())
var number int
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
ctx, cancel := context.WithCancel(context.TODO())
err := retry.Retry(increaseNumber,
retry.RetryDuration(time.Microsecond*50),
retry.Context(ctx),
)
number := 0
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
if err != nil {
fmt.Println(err) //retry is cancelled
}
duration := retry.RetryDuration(time.Microsecond*50)
retry.Retry(increaseNumber,
duration,
retry.Context(ctx),
)
fmt.Println(number)
// Output:
// 4
}
```
### <span id="RetryFunc">RetryFunc</span>
<p>Function that retry executes.</p>
<b>Signature:</b>
@@ -83,6 +88,7 @@ func main() {
```go
type RetryFunc func() error
```
<b>Example:</b>
```go
@@ -96,28 +102,31 @@ import (
)
func main() {
var number int
number := 0
var increaseNumber retry.RetryFunc = func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```
### <span id="RetryTimes">RetryTimes</span>
<p>Set times of retry. Default times is 5.</p>
<b>Signature:</b>
@@ -125,6 +134,7 @@ func main() {
```go
func RetryTimes(n uint)
```
<b>Example:</b>
```go
@@ -138,26 +148,28 @@ import (
)
func main() {
var number int
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
if err != nil {
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1
}
fmt.Println(err)
}
// Output:
// function main.main.func1 run failed after 2 times retry
}
```
### <span id="RetryDuration">RetryDuration</span>
<p>Set duration of retries. Default duration is 3 second.</p>
<b>Signature:</b>
@@ -165,6 +177,7 @@ func main() {
```go
func RetryDuration(d time.Duration)
```
<b>Example:</b>
```go
@@ -178,26 +191,31 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```
### <span id="Retry">Retry</span>
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
<b>Signature:</b>
@@ -205,6 +223,7 @@ func main() {
```go
func Retry(retryFunc RetryFunc, opts ...Option) error
```
<b>Example:</b>
```go
@@ -218,20 +237,25 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```

View File

@@ -1,16 +1,17 @@
# Retry
retry重试执行函数直到函数运行成功或被context cancel。
retry 重试执行函数直到函数运行成功或被 context cancel。
<div STYLE="page-break-after: always;"></div>
## 源码:
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
<div STYLE="page-break-after: always;"></div>
## 用法:
```go
import (
"github.com/duke-git/lancet/v2/retry"
@@ -20,20 +21,19 @@ import (
<div STYLE="page-break-after: always;"></div>
## 目录
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
<div STYLE="page-break-after: always;"></div>
## Document文档
## Document 文档
### <span id="Context">Context</span>
<p>设置重试context参数</p>
<b>函数签名:</b>
@@ -41,43 +41,46 @@ import (
```go
func Context(ctx context.Context)
```
<b>例子:</b>
```go
import (
"context"
"errors"
"fmt"
"lancet-demo/retry"
"time"
"context"
"errors"
"fmt"
"lancet-demo/retry"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.TODO())
var number int
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
ctx, cancel := context.WithCancel(context.TODO())
err := retry.Retry(increaseNumber,
retry.RetryDuration(time.Microsecond*50),
retry.Context(ctx),
)
number := 0
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
if err != nil {
fmt.Println(err) //retry is cancelled
}
duration := retry.RetryDuration(time.Microsecond*50)
retry.Retry(increaseNumber,
duration,
retry.Context(ctx),
)
fmt.Println(number)
// Output:
// 4
}
```
### <span id="RetryFunc">RetryFunc</span>
<p>被重试执行的函数</p>
<b>函数签名:</b>
@@ -85,6 +88,7 @@ func main() {
```go
type RetryFunc func() error
```
<b>例子:</b>
```go
@@ -98,27 +102,31 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
var increaseNumber retry.RetryFunc = func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```
### <span id="RetryTimes">RetryTimes</span>
<p>设置重试次数默认5</p>
<b>函数签名:</b>
@@ -126,6 +134,7 @@ func main() {
```go
func RetryTimes(n uint)
```
<b>例子:</b>
```go
@@ -139,25 +148,28 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
if err != nil {
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1
}
fmt.Println(err)
}
// Output:
// function main.main.func1 run failed after 2 times retry
}
```
### <span id="RetryDuration">RetryDuration</span>
<p>设置重试间隔时间默认3秒</p>
<b>函数签名:</b>
@@ -165,6 +177,7 @@ func main() {
```go
func RetryDuration(d time.Duration)
```
<b>例子:</b>
```go
@@ -178,26 +191,31 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```
### <span id="Retry">Retry</span>
<p>重试执行函数retryFunc直到函数运行成功或被context停止</p>
<b>函数签名:</b>
@@ -205,6 +223,7 @@ func main() {
```go
func Retry(retryFunc RetryFunc, opts ...Option) error
```
<b>例子:</b>
```go
@@ -218,20 +237,25 @@ import (
)
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil {
log.Fatal(err)
}
return
}
fmt.Println(number) //3
fmt.Println(number)
// Output:
// 3
}
```

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,6 @@ import (
)
func ExampleRandInt() {
result := RandInt(1, 10)
if result >= 1 && result < 10 {