2022-03-13 16:17:20 +08:00
|
|
|
package backoff
|
2022-03-13 16:16:58 +08:00
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// Backoff used for retry sleep backoff
|
|
|
|
type Backoff interface {
|
|
|
|
Next() bool
|
2022-03-13 16:17:20 +08:00
|
|
|
Reset()
|
2022-03-13 16:16:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConstantBackoff implements Backoff interface with constant sleep time
|
|
|
|
type ConstantBackoff struct {
|
|
|
|
Sleep time.Duration
|
|
|
|
Max int
|
|
|
|
|
|
|
|
tried int
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:17:20 +08:00
|
|
|
func (c *ConstantBackoff) Next() bool {
|
2022-03-13 16:16:58 +08:00
|
|
|
c.tried++
|
2022-03-13 16:18:39 +08:00
|
|
|
if c.tried > c.Max {
|
2022-03-13 16:16:58 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(c.Sleep)
|
|
|
|
return true
|
|
|
|
}
|
2022-03-13 16:17:20 +08:00
|
|
|
|
|
|
|
func (c *ConstantBackoff) Reset() {
|
|
|
|
c.tried = 0
|
|
|
|
}
|