retry

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExponentialBackoffRetryStrategy

type ExponentialBackoffRetryStrategy struct {
	// contains filtered or unexported fields
}

ExponentialBackoffRetryStrategy 指数退避重试

func NewExponentialBackoffRetryStrategy

func NewExponentialBackoffRetryStrategy(initialInterval, maxInterval time.Duration, maxRetries int32) (*ExponentialBackoffRetryStrategy, error)

func (*ExponentialBackoffRetryStrategy) Next

Example
// 注意,因为在例子里面我们设置初始的重试间隔是 1s,最大重试间隔是 5s
// 所以在前面四次,重试间隔都是在增长的,每次变为原来的2倍。
// 在触及到了最大重试间隔之后,就一直以最大重试间隔来重试。
retry, err := NewExponentialBackoffRetryStrategy(time.Second, time.Second*5, 10)
if err != nil {
	fmt.Println(err)
	return
}
interval, ok := retry.Next()
for ok {
	fmt.Println(interval)
	interval, ok = retry.Next()
}
Output:

1s
2s
4s
5s
5s
5s
5s
5s
5s
5s

type FixedIntervalRetryStrategy

type FixedIntervalRetryStrategy struct {
	// contains filtered or unexported fields
}

FixedIntervalRetryStrategy 等间隔重试

func NewFixedIntervalRetryStrategy

func NewFixedIntervalRetryStrategy(interval time.Duration, maxRetries int32) (*FixedIntervalRetryStrategy, error)

func (*FixedIntervalRetryStrategy) Next

Example
retry, err := NewFixedIntervalRetryStrategy(time.Second, 3)
if err != nil {
	fmt.Println(err)
	return
}
interval, ok := retry.Next()
for ok {
	fmt.Println(interval)
	interval, ok = retry.Next()
}
Output:

1s
1s
1s

type Strategy

type Strategy interface {
	// Next 返回下一次重试的间隔,如果不需要继续重试,那么第二参数返回 false
	Next() (time.Duration, bool)
}

Jump to

Keyboard shortcuts

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