Go第三方庫-重試(retry-go)


重試機制的簡單庫。

概要

http請求重試示例:

url := "http://example.com"
var body []byte

err := retry.Do(
	func() error {
		resp, err := http.Get(url)
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		body, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}

		return nil
	},
)

fmt.Println(body)

使用

func BackOffDelay

func BackOffDelay(n uint, _ error, config *Config) time.Duration

BackOffDelay是一種DelayType,它增加了連續重試之間的延時。

func Do

func Do(retryableFunc RetryableFunc, opts ...Option) error

func FixedDelay

func FixedDelay(_ uint, _ error, config *Config) time.Duration

FixedDelay是一種DelayType,它在所有迭代中保持相同的延時。

func IsRecoverable

func IsRecoverable(err error) bool

IsRecoverable檢查錯誤是否是unrecoverableError的實例。

func RandomDelay

func RandomDelay(_ uint, _ error, config *Config) time.Duration

RandomDelay是一種DelayType,它選擇一個隨機延遲到config.maxJitter。

func Unrecoverable

func Unrecoverable(err error) error

Unrecoverable在unrecoverableError結構中包裝錯誤。

type Config

type Config struct {
  
}

type DelayTypeFunc

type DelayTypeFunc func(n uint, err error, config *Config) time.Duration

調用DelayTypeFunc以返回在n次嘗試后可重試函數失敗后等待的下一個延遲。

func CombineDelay

func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc

CombineDelay是一種DelayType,它將所有指定的延遲組合成一個新的DelayTypeFunc。

type Error

type Error []error

Error類型表示重試中的錯誤列表。

func (Error) Error

func (e Error) Error() string

Error方法返回Error的字符串表示,是錯誤接口的實現。

func (Error) WrappedError

func (e Error) WrappedErrors() []error

WrappedErrors返回此錯誤正在包裝的錯誤列表。它是errwrap包中errwrap.Wrapper接口的實現,因此retry.Error可以與該庫一起使用。

type OnRetryFunc

type OnRetryFunc func(n uint, err error)

OnRetry函數的函數簽名n等於嘗試次數。

type Option

type Option func(*Config)

Option表示重試的選項。

func Attempts

func Attempts(attempts uint) Option

Attempts設置的重試次數默認為10。

func Context

func Context(ctx context.Context) Option

Context允許設置重試的context默認是BackgroundContext。

立即取消的例子(也許這不是最好的例子,但我希望它足以作為示例):

ctx, cancel := context.WithCancel(context.Background())
cancel()

retry.Do(
	func() error {
		...
	},
	retry.Context(ctx),
)

func Delay

func Delay(delay time.Duration) Option

Delay用於設置重試之間的延遲,設置默認為100毫秒。

func DelayType

func DelayType(delayType DelayTypeFunc) Option

DelayType設置重試之間延遲的類型默認為BackOff。

func LastErrorOnly

func LastErrorOnly(lastErrorOnly bool) Option

返回來自重試函數的直接最后一個錯誤,默認為false(返回所有內容的包裝錯誤)。

func MaxDelay

func MaxDelay(maxDelay time.Duration) Option

MaxDelay設置重試之間的最大延遲,不適用於默認值。

func MaxJitter

func MaxJitter(maxJitter time.Duration) Option

MaxJitter為RandomDelay設置重試之間的最大隨機抖動。

func OnRetry

func OnRetry(onRetry OnRetryFunc) Option

OnRetry函數每次重試都會被回調。

記錄每個重試示例:

retry.Do(
	func() error {
		return errors.New("some error")
	},
	retry.OnRetry(func(n uint, err error) {
		log.Printf("#%d: %s\n", n, err)
	}),
)

func RetryIf

func RetryIf(retryIf RetryIfFunc) Option

RetryIf控制是否應在出現錯誤后嘗試重試(假設還有重試剩余次數)。

示例,如果出現特殊錯誤,請跳過重試:

retry.Do(
	func() error {
		return errors.New("special error")
	},
	retry.RetryIf(func(err error) bool {
		if err.Error() == "special error" {
			return false
		}
		return true
	})
)

默認情況下,如果使用retry.Unrecoverable包裝錯誤,則RetryIf停止執行,因此上面的示例也可以縮短為:

retry.Do(
	func() error {
		return retry.Unrecoverable(errors.New("special error"))
	}
)

type RetryIfFunc

type RetryIfFunc func(error) bool

重試if函數的函數簽名。

type RetryableFunc

type RetryableFunc func() error

可重試函數的函數簽名。

參考

avast/retry-go: Simple golang library for retry mechanism (github.com)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM