1 Star 0 Fork 0

powerpaas/machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
retry.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
Fabio Rapposelli 提交于 2015-02-16 12:36 . Updated Godeps for govcloudair.
package backoff
import "time"
// Retry the function f until it does not return error or BackOff stops.
// f is guaranteed to be run at least once.
// It is the caller's responsibility to reset b after Retry returns.
//
// Retry sleeps the goroutine for the duration returned by BackOff after a
// failed operation returns.
//
// Usage:
// operation := func() error {
// // An operation that may fail
// }
//
// err := backoff.Retry(operation, backoff.NewExponentialBackOff())
// if err != nil {
// // Operation has failed.
// }
//
// // Operation is successfull.
//
func Retry(f func() error, b BackOff) error { return RetryNotify(f, b, nil) }
// RetryNotify calls notify function with the error and wait duration for each failed attempt before sleep.
func RetryNotify(f func() error, b BackOff, notify func(err error, wait time.Duration)) error {
var err error
var next time.Duration
b.Reset()
for {
if err = f(); err == nil {
return nil
}
if next = b.NextBackOff(); next == Stop {
return err
}
if notify != nil {
notify(err, next)
}
time.Sleep(next)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.2.0-rc2

搜索帮助

Cb406eda 1850385 E526c682 1850385