diff options
author | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-01-09 00:07:55 +0100 |
commit | be898b44c3c057c7b70efb83f66b7bbbdd5e9276 (patch) | |
tree | 01c8983913bcae177b5e0e015de9a45abe8369a7 /vendor/github.com/nlopes/slack/backoff.go | |
parent | 4828c434439949f62a6e219ab9947c221a06b111 (diff) | |
download | matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.gz matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.bz2 matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.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 +} |