diff options
author | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
commit | 4a96a977c0e86e22edcda40730779279334685be (patch) | |
tree | fea5efa54d17af2d3cadb4d4d5a42f57e9bb7fbe /vendor/github.com/nlopes/slack/backoff.go | |
parent | 9a95293bdf74a4d02827018649b6ea0ffdef74ba (diff) | |
download | matterbridge-msglm-4a96a977c0e86e22edcda40730779279334685be.tar.gz matterbridge-msglm-4a96a977c0e86e22edcda40730779279334685be.tar.bz2 matterbridge-msglm-4a96a977c0e86e22edcda40730779279334685be.zip |
Update vendor (slack)
Diffstat (limited to 'vendor/github.com/nlopes/slack/backoff.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/backoff.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/vendor/github.com/nlopes/slack/backoff.go b/vendor/github.com/nlopes/slack/backoff.go new file mode 100644 index 00000000..e555a1ad --- /dev/null +++ b/vendor/github.com/nlopes/slack/backoff.go @@ -0,0 +1,57 @@ +package slack + +import ( + "math" + "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. +type backoff struct { + attempts int + //Factor is the multiplying factor for each increment step + Factor float64 + //Jitter eases contention by randomizing backoff steps + Jitter bool + //Min and Max are the minimum and maximum values of the counter + Min, Max time.Duration +} + +// Returns the current value of the counter and then multiplies it +// Factor +func (b *backoff) Duration() time.Duration { + //Zero-values are nonsensical, so we use + //them to apply defaults + if b.Min == 0 { + b.Min = 100 * time.Millisecond + } + if b.Max == 0 { + b.Max = 10 * time.Second + } + if b.Factor == 0 { + b.Factor = 2 + } + //calculate this duration + dur := float64(b.Min) * math.Pow(b.Factor, float64(b.attempts)) + if b.Jitter == true { + dur = rand.Float64()*(dur-float64(b.Min)) + float64(b.Min) + } + //cap! + if dur > float64(b.Max) { + return b.Max + } + //bump attempts count + b.attempts++ + //return as a time.Duration + return time.Duration(dur) +} + +//Resets the current value of the counter back to Min +func (b *backoff) Reset() { + b.attempts = 0 +} |