Skip to content
On this page

# Retry ​

Package retry is for executing a function repeatedly until it was successful or canceled by the context.

## Source: ​

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

## Usage: ​

go

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

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

## Index ​

- Context
- Retry
- RetryFunc
- RetryDuration
- RetryTimes

## Documentation ​

### Context ​

Set retry context config, can cancel the retry with context.

Signature:

go

    func Context(ctx context.Context)

    func Context(ctx context.Context)

Example:Run

go

    import (
        "context"
        "errors"
        "fmt"
        "github.com/duke-git/lancet/v2/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"
        "github.com/duke-git/lancet/v2/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 ​

Function that retry executes.

Signature:

go

    type RetryFunc func() error

    type RetryFunc func() error

Example:Run

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 ​

Set times of retry. Default times is 5.

Signature:

go

    func RetryTimes(n uint)

    func RetryTimes(n uint)

Example:Run

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 ​

Set duration of retries. Default duration is 3 second.

Signature:

go

    func RetryDuration(d time.Duration)

    func RetryDuration(d time.Duration)

Example:Run

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 ​

Executes the retryFunc repeatedly until it was successful or canceled by the context.

Signature:

go

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

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

Example:Run

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
    }