Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backoff and Randomness additions for use in client utilities #16853

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/util/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
entry.lastUpdate = p.Clock.Now()
}

// Reset forces clearing of all backoff data for a given key.
func (p *Backoff) Reset(id string) {
p.Lock()
defer p.Unlock()
delete(p.perItemBackoff, id)
}

// Returns True if the elapsed time since eventTime is smaller than the current backoff window
func (p *Backoff) IsInBackOffSince(id string, eventTime time.Time) bool {
p.Lock()
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func TestSlowBackoff(t *testing.T) {
}
b.Next(id, tc.Now())
}

//Now confirm that the Reset cancels backoff.
b.Next(id, tc.Now())
b.Reset(id)
if b.Get(id) != 0 {
t.Errorf("Reset didn't clear the backoff.")
}

}

func TestBackoffReset(t *testing.T) {
Expand Down
25 changes: 14 additions & 11 deletions pkg/util/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,12 @@ var rng = struct {
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
}

// String generates a random alphanumeric string n characters long. This will
// panic if n is less than zero.
func String(n int) string {
if n < 0 {
panic("out-of-bounds value")
}
b := make([]rune, n)
// Intn generates an integer in range 0->max.
// By design this should panic if input is invalid, <= 0.
func Intn(max int) int {
rng.Lock()
defer rng.Unlock()
for i := range b {
b[i] = letters[rng.rand.Intn(numLetters)]
}
return string(b)
return rng.rand.Intn(max)
}

// Seed seeds the rng with the provided seed.
Expand All @@ -62,3 +55,13 @@ func Perm(n int) []int {
defer rng.Unlock()
return rng.rand.Perm(n)
}

// String generates a random alphanumeric string n characters long. This will
// panic if n is less than zero.
func String(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = letters[Intn(numLetters)]
}
return string(b)
}
21 changes: 21 additions & 0 deletions pkg/util/rand/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func TestString(t *testing.T) {
}
}

// Confirm that panic occurs on invalid input.
func TestRangePanic(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Panic didn't occur!")
}
}()
// Should result in an error...
Intn(0)
}

func TestIntn(t *testing.T) {
// 0 is invalid.
for _, max := range []int{1, 2, 10, 123} {
inrange := Intn(max)
if inrange < 0 || inrange > max {
t.Errorf("%v out of range (0,%v)", inrange, max)
}
}
}

func TestPerm(t *testing.T) {
Seed(5)
rand.Seed(5)
Expand Down