-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
30 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,21 @@ | ||
package services | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/timeutil" | ||
) | ||
|
||
// NewTicker returns a new timeutil.Ticker configured to: | ||
// - fire the first tick immediately | ||
// - apply jitter to each period via timeutil.WithJitter | ||
func NewTicker(d time.Duration) *timeutil.Ticker { | ||
first := true | ||
return timeutil.NewTicker(func() time.Duration { | ||
if first { | ||
first = false | ||
return 0 | ||
} | ||
return timeutil.WithJitter(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package timeutil | ||
|
||
import ( | ||
"math" | ||
mrand "math/rand" | ||
"time" | ||
) | ||
|
||
// WithJitter adds +/- 10% to a duration. | ||
// TODO better name? JitterDuration; ApplyJitter | ||
// TODO customizable percentage? | ||
func WithJitter(d time.Duration) time.Duration { | ||
// #nosec | ||
if d == 0 { | ||
return 0 | ||
} | ||
// ensure non-zero arg to Intn to avoid panic | ||
max := math.Max(float64(d.Abs())/5.0, 1.) | ||
// #nosec - non critical randomness | ||
jitter := mrand.Intn(int(max)) / 2 | ||
return time.Duration(int(d) + jitter) | ||
} |
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 @@ | ||
package timeutil | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestWithJitter(t *testing.T) { | ||
for _, tt := range []struct { | ||
dur time.Duration | ||
from, to time.Duration | ||
}{ | ||
{0, 0, 0}, | ||
{time.Second, 900 * time.Millisecond, 1100 * time.Millisecond}, | ||
{time.Minute, 54 * time.Second, 66 * time.Second}, | ||
{24 * time.Hour, 21*time.Hour + 36*time.Minute, 26*time.Hour + 24*time.Minute}, | ||
} { | ||
t.Run(tt.dur.String(), func(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
got := WithJitter(tt.dur) | ||
t.Logf("%d: %s", i, got) | ||
if got < tt.from || got > tt.to { | ||
t.Errorf("expected duration %s with jitter to be between (%s, %s) but got: %s", tt.dur, tt.from, tt.to, got) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,41 @@ | ||
package timeutil | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Ticker is like time.Ticker, but with a variable period. | ||
type Ticker struct { | ||
C <-chan time.Time | ||
stop chan struct{} | ||
} | ||
|
||
// NewTicker returns a started Ticker which calls nextDur for each period. | ||
func NewTicker(nextDur func() time.Duration) *Ticker { | ||
c := make(chan time.Time) // unbuffered so we block and delay if not being handled | ||
t := Ticker{C: c, stop: make(chan struct{})} | ||
go t.run(c, nextDur) | ||
return &t | ||
} | ||
|
||
func (t *Ticker) run(c chan<- time.Time, nextDur func() time.Duration) { | ||
var timer *time.Timer | ||
defer timer.Stop() | ||
for { | ||
timer = time.NewTimer(nextDur()) | ||
select { | ||
case <-t.stop: | ||
return | ||
|
||
case <-timer.C: | ||
timer.Stop() | ||
select { | ||
case <-t.stop: | ||
return | ||
case c <- time.Now(): | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (t *Ticker) Stop() { close(t.stop) } |
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