代码拉取完成,页面将自动刷新
package common
import "time"
// A Backoff waits on errors with exponential backoff (limited by maximum
// backoff). Resetting Backoff will reset the next sleep timer to the initial
// backoff duration.
type Backoff struct {
duration time.Duration
done <-chan struct{}
init time.Duration
max time.Duration
last time.Time
}
func NewBackoff(done <-chan struct{}, init, max time.Duration) *Backoff {
return &Backoff{
duration: init,
done: done,
init: init,
max: max,
}
}
func (b *Backoff) Reset() {
b.duration = b.init
}
func (b *Backoff) Wait() bool {
backoff := b.duration
b.duration *= 2
if b.duration > b.max {
b.duration = b.max
}
select {
case <-b.done:
return false
case <-time.After(backoff):
b.last = time.Now()
return true
}
}
func (b *Backoff) WaitOnError(err error) bool {
if err == nil {
b.Reset()
return true
}
return b.Wait()
}
func (b *Backoff) TryWaitOnError(failTS time.Time, err error) bool {
if err == nil {
b.Reset()
return true
}
if failTS.Before(b.last) {
return true
}
return b.Wait()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。