source: code/trunk/rate.go@ 823

Last change on this file since 823 was 804, checked in by koizumi.aoi, 2 years ago

Drunk as I like

Signed-off-by: Aoi K <koizumi.aoi@…>

File size: 604 bytes
Line 
1package suika
2
3import (
4 "math/rand"
5 "time"
6)
7
8// backoffer implements a simple exponential backoff.
9type backoffer struct {
10 min, max, jitter time.Duration
11 n int64
12}
13
14func newBackoffer(min, max, jitter time.Duration) *backoffer {
15 return &backoffer{min: min, max: max, jitter: jitter}
16}
17
18func (b *backoffer) Reset() {
19 b.n = 0
20}
21
22func (b *backoffer) Next() time.Duration {
23 if b.n == 0 {
24 b.n = 1
25 return 0
26 }
27
28 d := time.Duration(b.n) * b.min
29 if d > b.max {
30 d = b.max
31 } else {
32 b.n *= 2
33 }
34
35 if b.jitter != 0 {
36 d += time.Duration(rand.Int63n(int64(b.jitter)))
37 }
38
39 return d
40}
Note: See TracBrowser for help on using the repository browser.