retry

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

retry

重试机制:支持最大次数、固定延迟、退避、抖动。

安装

import "github.com/Ccmuyu/gopkg/retry"

快速开始

err := retry.Retry(func() error {
    return doSomething()
}, retry.WithMaxAttempts(5),
   retry.WithDelay(100*time.Millisecond),
   retry.WithBackoff(),
   retry.WithJitter())

需要取消或截止时间时使用 RetryCtx。已取消的 context 不会执行回调。 maxAttempts <= 0 会归一化为 1,确保操作至少执行一次。

err := retry.RetryCtx(ctx, func() error {
    return doSomething()
}, retry.WithMaxAttempts(5))

API 参考

func Retry(fn func() error, opts ...Option) error
func RetryCtx(ctx context.Context, fn func() error, opts ...Option) error
func WithMaxAttempts(n int) Option
func WithDelay(d time.Duration) Option
func WithBackoff() Option
func WithJitter() Option

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(fn func() error, opts ...Option) error

func RetryCtx

func RetryCtx(ctx context.Context, fn func() error, opts ...Option) error

Types

type Option

type Option func(*options)

func WithBackoff

func WithBackoff() Option

func WithDelay

func WithDelay(d time.Duration) Option

func WithJitter

func WithJitter() Option

func WithMaxAttempts

func WithMaxAttempts(n int) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL