Skip to content
本页内容

# Retry ​

retry 重试执行函数直到函数运行成功或被 context cancel。

## 源码: ​

- https://github.com/duke-git/lancet/blob/main/retry/retry.go

## 用法: ​

go

    import (
        "github.com/duke-git/lancet/v2/retry"
    )

    import (
        "github.com/duke-git/lancet/v2/retry"
    )

## 目录 ​

- Context
- Retry
- RetryFunc
- RetryDuration
- RetryTimes

## 文档 ​

### Context ​

设置重试context参数

函数签名:

go

    func Context(ctx context.Context)

    func Context(ctx context.Context)

示例:运行

go

    import (
        "context"
        "errors"
        "fmt"
        "lancet-demo/retry"
        "time"
    )

    func main() {
        ctx, cancel := context.WithCancel(context.TODO())

        number := 0
        increaseNumber := func() error {
            number++
            if number > 3 {
                cancel()
            }
            return errors.New("error occurs")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        retry.Retry(increaseNumber,
            duration,
            retry.Context(ctx),
        )

        fmt.Println(number)

        // Output:
        // 4
    }

    import (
        "context"
        "errors"
        "fmt"
        "lancet-demo/retry"
        "time"
    )

    func main() {
        ctx, cancel := context.WithCancel(context.TODO())

        number := 0
        increaseNumber := func() error {
            number++
            if number > 3 {
                cancel()
            }
            return errors.New("error occurs")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        retry.Retry(increaseNumber,
            duration,
            retry.Context(ctx),
        )

        fmt.Println(number)

        // Output:
        // 4
    }

### RetryFunc ​

被重试执行的函数

函数签名:

go

    type RetryFunc func() error

    type RetryFunc func() error

示例:运行

go

    package main

    import (
        "fmt"
        "errors"
        "log"
        "github.com/duke-git/lancet/v2/retry"
    )

    func main() {
        number := 0
        var increaseNumber retry.RetryFunc = func() error {
            number++
            if number == 3 {
                return nil
            }
            return errors.New("error occurs")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }

    package main

    import (
        "fmt"
        "errors"
        "log"
        "github.com/duke-git/lancet/v2/retry"
    )

    func main() {
        number := 0
        var increaseNumber retry.RetryFunc = func() error {
            number++
            if number == 3 {
                return nil
            }
            return errors.New("error occurs")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }

### RetryTimes ​

设置重试次数，默认5

函数签名:

go

    func RetryTimes(n uint)

    func RetryTimes(n uint)

示例:运行

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.RetryTimes(2))
        if err != nil {
            fmt.Println(err)
        }

        // Output:
        // function main.main.func1 run failed after 2 times retry
    }

    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.RetryTimes(2))
        if err != nil {
            fmt.Println(err)
        }

        // Output:
        // function main.main.func1 run failed after 2 times retry
    }

### RetryDuration ​

设置重试间隔时间，默认3秒

函数签名:

go

    func RetryDuration(d time.Duration)

    func RetryDuration(d time.Duration)

示例:运行

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")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }

    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")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }

### Retry ​

重试执行函数retryFunc，直到函数运行成功，或被context停止

函数签名:

go

    func Retry(retryFunc RetryFunc, opts ...Option) error

    func Retry(retryFunc RetryFunc, opts ...Option) error

示例:运行

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")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }

    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")
        }

        duration := retry.RetryDuration(time.Microsecond*50)

        err := retry.Retry(increaseNumber, duration)
        if err != nil {
            return
        }

        fmt.Println(number)

        // Output:
        // 3
    }