5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-27 04:21:34 +00:00
matterbridge/vendor/github.com/slack-go/slack/internal/backoff/backoff.go

63 lines
1.4 KiB
Go
Raw Normal View History

2021-02-01 20:29:04 +00:00
package backoff
2016-09-05 14:34:37 +00:00
import (
"math/rand"
"time"
)
// This one was ripped from https://github.com/jpillora/backoff/blob/master/backoff.go
// Backoff is a time.Duration counter. It starts at Min. After every
// call to Duration() it is multiplied by Factor. It is capped at
// Max. It returns to Min on every call to Reset(). Used in
// conjunction with the time package.
2021-02-01 20:29:04 +00:00
type Backoff struct {
2016-09-05 14:34:37 +00:00
attempts int
2019-09-07 20:46:58 +00:00
// Initial value to scale out
Initial time.Duration
// Jitter value randomizes an additional delay between 0 and Jitter
Jitter time.Duration
// Max maximum values of the backoff
Max time.Duration
2016-09-05 14:34:37 +00:00
}
// Returns the current value of the counter and then multiplies it
// Factor
2021-02-01 20:29:04 +00:00
func (b *Backoff) Duration() (dur time.Duration) {
2019-09-07 20:46:58 +00:00
// Zero-values are nonsensical, so we use
// them to apply defaults
2016-09-05 14:34:37 +00:00
if b.Max == 0 {
b.Max = 10 * time.Second
}
2019-09-07 20:46:58 +00:00
if b.Initial == 0 {
b.Initial = 100 * time.Millisecond
2016-09-05 14:34:37 +00:00
}
2019-09-07 20:46:58 +00:00
// calculate this duration
if dur = time.Duration(1 << uint(b.attempts)); dur > 0 {
dur = dur * b.Initial
} else {
dur = b.Max
2016-09-05 14:34:37 +00:00
}
2019-09-07 20:46:58 +00:00
if b.Jitter > 0 {
dur = dur + time.Duration(rand.Intn(int(b.Jitter)))
2016-09-05 14:34:37 +00:00
}
2019-09-07 20:46:58 +00:00
// bump attempts count
2016-09-05 14:34:37 +00:00
b.attempts++
2019-09-07 20:46:58 +00:00
return dur
2016-09-05 14:34:37 +00:00
}
//Resets the current value of the counter back to Min
2021-02-01 20:29:04 +00:00
func (b *Backoff) Reset() {
2016-09-05 14:34:37 +00:00
b.attempts = 0
}
2021-02-01 20:29:04 +00:00
// Attempts returns the number of attempts that we had done so far
func (b *Backoff) Attempts() int {
return b.attempts
}