Skip to content

Retry

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

源码:

用法:

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

目录

文档

Context

设置重试context参数

函数签名:

go
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.RetryWithLinearBackoff(time.Microsecond*50)

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

    fmt.Println(number)

    // Output:
    // 4
}

RetryFunc

被重试执行的函数

函数签名:

go
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.RetryWithLinearBackoff(time.Microsecond*50)

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

    fmt.Println(number)

    // Output:
    // 3
}

RetryTimes

设置重试次数,默认5

函数签名:

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

Retry

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

函数签名:

go
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.RetryWithLinearBackoff(time.Microsecond*50)

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

    fmt.Println(number)

    // Output:
    // 3
}

BackoffStrategy

定义计算退避间隔的方法的接口。

函数签名:

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
}

示例:

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
}

RetryWithCustomBackoff

设置自定义退避策略。

函数签名:

go
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option

示例:运行

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
}

RetryWithLinearBackoff

设置线性策略退避。

函数签名:

go
func RetryWithLinearBackoff(interval time.Duration) Option

示例:运行

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
}

RetryWithExponentialWithJitterBackoff

设置指数策略退避。

函数签名:

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

示例:运行

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
}