backoff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inject

func Inject(ctx context.Context, backoff BackoffFunc) context.Context

Inject 将一个回退函数注入到给定的上下文中,并返回一个新的上下文。这样新上下文就携带了回退信息。

Types

type BackoffFunc

type BackoffFunc func(ctx context.Context, attempt uint) time.Duration

BackoffFunc denotes a family of functions that control the backoff duration between call retries.

They are called with an identifier of the attempt, and should return a time the system client should hold off for. If the time returned is longer than the `context.Context.Deadline` of the request the deadline of the request takes precedence and the wait will be interrupted before proceeding with the next iteration. The context can be used to extract context values.

func Constant

func Constant(delta time.Duration) BackoffFunc

Constant it waits for a fixed period of time between calls.

func Context

func Context() BackoffFunc

Context 从给定的上下文中获取一个回退函数,如果存在则调用它并返回回退时间;否则返回0。主要用于动态设置重试间的延迟。

func Exponential

func Exponential(delta time.Duration) BackoffFunc

Exponential it waits for "delta * e^attempts" time between calls.

func Exponential2

func Exponential2(delta time.Duration) BackoffFunc

Exponential2 it waits for "delta * 2^attempts" time between calls.

func Fibonacci

func Fibonacci(delta time.Duration) BackoffFunc

Fibonacci it waits for "delta * fibonacci(attempt)" time between calls.

func JitterUp

func JitterUp(backoff BackoffFunc, jitter float64) BackoffFunc

JitterUp adds random jitter to the interval.

This adds or subtracts time from the interval within a given jitter fraction. For example for 10s and jitter 0.1, it will return a time within [9s, 11s])

func Linear

func Linear(delta time.Duration) BackoffFunc

Linear it waits for "delta * attempt" time between calls.

func Random

func Random(alpha, beta time.Duration) BackoffFunc

Random generates a function that waits for a random time in the range [alpha, beta) between calls.

func Zero

func Zero() BackoffFunc

Zero it waits for zero time between calls.

Jump to

Keyboard shortcuts

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