diff options
| author | Christine Dodrill <me@christine.website> | 2018-08-22 03:17:59 +0000 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2018-08-22 03:17:59 +0000 |
| commit | 6b7d6dcc49c6cbd83af70d97d01b700c8fb0c393 (patch) | |
| tree | 60bd319655e77afb0e3737cc9070d5111a41f22b /vendor/github.com/segmentio/backo-go | |
| parent | 5a8b8dc48f33c44fd41ac27c1fb4185de1d87d41 (diff) | |
| download | xesite-6b7d6dcc49c6cbd83af70d97d01b700c8fb0c393.tar.xz xesite-6b7d6dcc49c6cbd83af70d97d01b700c8fb0c393.zip | |
add analytics via segment again
Diffstat (limited to 'vendor/github.com/segmentio/backo-go')
| -rw-r--r-- | vendor/github.com/segmentio/backo-go/.gitmodules | 3 | ||||
| -rw-r--r-- | vendor/github.com/segmentio/backo-go/README.md | 80 | ||||
| -rw-r--r-- | vendor/github.com/segmentio/backo-go/backo.go | 83 |
3 files changed, 166 insertions, 0 deletions
diff --git a/vendor/github.com/segmentio/backo-go/.gitmodules b/vendor/github.com/segmentio/backo-go/.gitmodules new file mode 100644 index 0000000..36de929 --- /dev/null +++ b/vendor/github.com/segmentio/backo-go/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/github.com/bmizerany/assert"] + path = vendor/github.com/bmizerany/assert + url = https://github.com/bmizerany/assert diff --git a/vendor/github.com/segmentio/backo-go/README.md b/vendor/github.com/segmentio/backo-go/README.md new file mode 100644 index 0000000..1362bec --- /dev/null +++ b/vendor/github.com/segmentio/backo-go/README.md @@ -0,0 +1,80 @@ +Backo [](http://godoc.org/github.com/segmentio/backo-go) +----- + +Exponential backoff for Go (Go port of segmentio/backo). + + +Usage +----- + +```go +import "github.com/segmentio/backo-go" + +// Create a Backo instance. +backo := backo.NewBacko(milliseconds(100), 2, 1, milliseconds(10*1000)) +// OR with defaults. +backo := backo.DefaultBacko() + +// Use the ticker API. +ticker := b.NewTicker() +for { + timeout := time.After(5 * time.Minute) + select { + case <-ticker.C: + fmt.Println("ticked") + case <- timeout: + fmt.Println("timed out") + } +} + +// Or simply work with backoff intervals directly. +for i := 0; i < n; i++ { + // Sleep the current goroutine. + backo.Sleep(i) + // Retrieve the duration manually. + duration := backo.Duration(i) +} +``` + +License +------- + +``` +WWWWWW||WWWWWW + W W W||W W W + || + ( OO )__________ + / | \ + /o o| MIT \ + \___/||_||__||_|| * + || || || || + _||_|| _||_|| + (__|__|(__|__| + +The MIT License (MIT) + +Copyright (c) 2015 Segment, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` + + + + [1]: http://github.com/segmentio/backo-java + [2]: http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.segment.backo&a=backo&v=LATEST
\ No newline at end of file diff --git a/vendor/github.com/segmentio/backo-go/backo.go b/vendor/github.com/segmentio/backo-go/backo.go new file mode 100644 index 0000000..6f7b6d5 --- /dev/null +++ b/vendor/github.com/segmentio/backo-go/backo.go @@ -0,0 +1,83 @@ +package backo + +import ( + "math" + "math/rand" + "time" +) + +type Backo struct { + base time.Duration + factor uint8 + jitter float64 + cap time.Duration +} + +// Creates a backo instance with the given parameters +func NewBacko(base time.Duration, factor uint8, jitter float64, cap time.Duration) *Backo { + return &Backo{base, factor, jitter, cap} +} + +// Creates a backo instance with the following defaults: +// base: 100 milliseconds +// factor: 2 +// jitter: 0 +// cap: 10 seconds +func DefaultBacko() *Backo { + return NewBacko(time.Millisecond*100, 2, 0, time.Second*10) +} + +// Duration returns the backoff interval for the given attempt. +func (backo *Backo) Duration(attempt int) time.Duration { + duration := float64(backo.base) * math.Pow(float64(backo.factor), float64(attempt)) + + if backo.jitter != 0 { + random := rand.Float64() + deviation := math.Floor(random * backo.jitter * duration) + if (int(math.Floor(random*10)) & 1) == 0 { + duration = duration - deviation + } else { + duration = duration + deviation + } + } + + duration = math.Min(float64(duration), float64(backo.cap)) + return time.Duration(duration) +} + +// Sleep pauses the current goroutine for the backoff interval for the given attempt. +func (backo *Backo) Sleep(attempt int) { + duration := backo.Duration(attempt) + time.Sleep(duration) +} + +type Ticker struct { + done chan struct{} + C <-chan time.Time +} + +func (b *Backo) NewTicker() *Ticker { + c := make(chan time.Time, 1) + ticker := &Ticker{ + done: make(chan struct{}, 1), + C: c, + } + + go func() { + for i := 0; ; i++ { + select { + case t := <-time.After(b.Duration(i)): + c <- t + case <-ticker.done: + close(c) + return + } + } + }() + + return ticker +} + +func (t *Ticker) Stop() { + t.done <- struct{}{} +} |
