[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
464 lines (342 loc) · 8.59 KB

retry.md

File metadata and controls

464 lines (342 loc) · 8.59 KB

Retry

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

Source:

Usage:

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

Index

Documentation

Context

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

Signature:

func Context(ctx context.Context)

Example:Run

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:

type RetryFunc func() error

Example:Run

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:

func RetryTimes(n uint)

Example:Run

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:

func RetryDuration(d time.Duration)

Example:Run

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:

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

Example:Run

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
}

BackoffStrategy

An interface that defines a method for calculating backoff intervals.

Signature:

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

Example:

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(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
    if err != nil {
        return
    }

    fmt.Println(number)

    // Output:
    // 3
}

RetryWithCustomBackoff

Set abitary custom backoff strategy.

Signature:

func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option 

Example:Run

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(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
    if err != nil {
        return
    }

    fmt.Println(number)

    // Output:
    // 3
}

RetryWithLinearBackoff

Set linear strategy backoff.

Signature:

func RetryWithLinearBackoff(interval time.Duration) Option

Example:Run

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

Set exponential strategy backoff.

Signature:

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

Example:Run

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
}