Skip to content

Commit

Permalink
Change behavior of Uniform jitterer. Uniform now draws from a
Browse files Browse the repository at this point in the history
uniform distribution from [0, n).

N.B.:  breaking changes.
  • Loading branch information
lthibault committed Mar 13, 2020
1 parent 237c91a commit 86c9e1d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
2 changes: 2 additions & 0 deletions jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

// Jitter can compute a jitter
type Jitter interface {
// Jitter consumes an interval from a ticker and returns the final, jittered
// duration.
Jitter(time.Duration) time.Duration
}

Expand Down
25 changes: 5 additions & 20 deletions uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,16 @@ import (
// Uniform distribution
type Uniform struct {
Source *rand.Rand
Limit time.Duration

// If true, only a positive jitter will be added to the base duration
NonNegative bool
Min time.Duration
}

// Jitter the duration by drawing from a uniform distribution
func (u Uniform) Jitter(d time.Duration) time.Duration {
f := rand.Int63n
drawUniform := rand.Int63n
if u.Source != nil {
f = u.Source.Int63n
}

samp := time.Duration(f(int64(u.Limit)))

if u.NonNegative {
sign := rand.Intn
if u.Source != nil {
sign = u.Source.Intn
}

if sign(1) == 0 {
samp = -samp
}
drawUniform = u.Source.Int63n
}

return d + samp
d = time.Duration(drawUniform(int64(d)))
return max(d, u.Min)
}

0 comments on commit 86c9e1d

Please sign in to comment.