-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from thrawn01/thrawn/develop
Election rewrite
- Loading branch information
Showing
9 changed files
with
834 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package holster | ||
|
||
import ( | ||
"math" | ||
"time" | ||
) | ||
|
||
var backoff = NewBackOff(time.Millisecond*300, time.Second*30, 2) | ||
|
||
type BackOffCounter struct { | ||
min, max time.Duration | ||
factor float64 | ||
attempt int | ||
} | ||
|
||
func NewBackOff(min, max time.Duration, factor float64) *BackOffCounter { | ||
return &BackOffCounter{ | ||
factor: factor, | ||
min: min, | ||
max: max, | ||
} | ||
} | ||
|
||
// Next returns the next back off duration based on the number of | ||
// times Next() was called. Each call to next returns the next factor | ||
// of back off. Call Reset() to reset the back off attempts to zero. | ||
func (b *BackOffCounter) Next() time.Duration { | ||
d := b.BackOff(b.attempt) | ||
b.attempt++ | ||
return d | ||
} | ||
|
||
// Reset sets the back off attempt counter to zero | ||
func (b *BackOffCounter) Reset() { | ||
b.attempt = 0 | ||
} | ||
|
||
// BackOff calculates the back depending on the attempts provided | ||
func (b *BackOffCounter) BackOff(attempt int) time.Duration { | ||
d := time.Duration(float64(b.min) * math.Pow(b.factor, float64(attempt))) | ||
if d > b.max { | ||
return b.max | ||
} | ||
if d < b.min { | ||
return b.min | ||
} | ||
return d | ||
} | ||
|
||
// BackOff is a convenience function which returns a back off duration | ||
// with a default 300 millisecond minimum and a 30 second maximum with | ||
// a factor of 2 for each attempt. | ||
func BackOff(attempt int) time.Duration { | ||
return backoff.BackOff(attempt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package holster_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/mailgun/holster" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBackoffFunc(t *testing.T) { | ||
d := holster.BackOff(0) | ||
assert.Equal(t, time.Millisecond*300, d) | ||
|
||
d = holster.BackOff(1) | ||
assert.Equal(t, time.Millisecond*600, d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This setup is for testing only, all communication with | ||
# etcd is done through the proxy. You have to create a proxy | ||
# with toxiproxy-cli before you can connect to etcd | ||
# | ||
# toxiproxy-cli create etcd --listen 0.0.0.0:2379 --upstream etcd:22379 | ||
|
||
version: '3.2' | ||
services: | ||
etcd: | ||
image: quay.io/coreos/etcd:v3.2 | ||
command: > | ||
/usr/local/bin/etcd | ||
-name etcd0 | ||
-advertise-client-urls http://localhost:2379 | ||
-listen-client-urls http://0.0.0.0:22379 | ||
-initial-advertise-peer-urls http://0.0.0.0:2381 | ||
-listen-peer-urls http://0.0.0.0:2381 | ||
-initial-cluster-token etcd-cluster-1 | ||
-initial-cluster etcd0=http://0.0.0.0:2381 | ||
-initial-cluster-state new | ||
-enable-v2=false | ||
ports: | ||
- "22379:22379" | ||
# proxy: | ||
# image: shopify/toxiproxy:latest | ||
# ports: | ||
# - "2379:2379" | ||
# - "8474:8474" |
Oops, something went wrong.