|
@@ -27,6 +27,7 @@ type Fakelag struct {
|
27
|
27
|
window time.Duration
|
28
|
28
|
burstLimit uint
|
29
|
29
|
throttleMessagesPerWindow uint
|
|
30
|
+ cooldown time.Duration
|
30
|
31
|
nowFunc func() time.Time
|
31
|
32
|
sleepFunc func(time.Duration)
|
32
|
33
|
|
|
@@ -35,11 +36,12 @@ type Fakelag struct {
|
35
|
36
|
lastTouch time.Time
|
36
|
37
|
}
|
37
|
38
|
|
38
|
|
-func NewFakelag(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint) *Fakelag {
|
|
39
|
+func NewFakelag(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint, cooldown time.Duration) *Fakelag {
|
39
|
40
|
return &Fakelag{
|
40
|
41
|
window: window,
|
41
|
42
|
burstLimit: burstLimit,
|
42
|
43
|
throttleMessagesPerWindow: throttleMessagesPerWindow,
|
|
44
|
+ cooldown: cooldown,
|
43
|
45
|
nowFunc: time.Now,
|
44
|
46
|
sleepFunc: time.Sleep,
|
45
|
47
|
state: FakelagBursting,
|
|
@@ -59,8 +61,7 @@ func (fl *Fakelag) Touch() {
|
59
|
61
|
|
60
|
62
|
if fl.state == FakelagBursting {
|
61
|
63
|
// determine if the previous burst is over
|
62
|
|
- // (we could use 2*window instead)
|
63
|
|
- if elapsed > fl.window {
|
|
64
|
+ if elapsed > fl.cooldown {
|
64
|
65
|
fl.burstCount = 0
|
65
|
66
|
}
|
66
|
67
|
|
|
@@ -77,8 +78,8 @@ func (fl *Fakelag) Touch() {
|
77
|
78
|
}
|
78
|
79
|
|
79
|
80
|
if fl.state == FakelagThrottled {
|
80
|
|
- if elapsed > fl.window {
|
81
|
|
- // let them burst again (as above, we could use 2*window instead)
|
|
81
|
+ if elapsed > fl.cooldown {
|
|
82
|
+ // let them burst again
|
82
|
83
|
fl.state = FakelagBursting
|
83
|
84
|
return
|
84
|
85
|
}
|