default fakelag to off, add explicit cooldown config

This commit is contained in:
Shivaram Lingamneni
2018-03-28 13:18:08 -04:00
parent 1bf5e2a7c8
commit 36018174b0
5 changed files with 16 additions and 11 deletions
+1 -1
View File
@@ -158,7 +158,7 @@ func (client *Client) resetFakelag() {
return nil
}
return NewFakelag(flc.Window, flc.BurstLimit, flc.MessagesPerWindow)
return NewFakelag(flc.Window, flc.BurstLimit, flc.MessagesPerWindow, flc.Cooldown)
}()
client.stateMutex.Lock()
+1
View File
@@ -194,6 +194,7 @@ type FakelagConfig struct {
Window time.Duration
BurstLimit uint `yaml:"burst-limit"`
MessagesPerWindow uint `yaml:"messages-per-window"`
Cooldown time.Duration
}
// Config defines the overall configuration.
+6 -5
View File
@@ -27,6 +27,7 @@ type Fakelag struct {
window time.Duration
burstLimit uint
throttleMessagesPerWindow uint
cooldown time.Duration
nowFunc func() time.Time
sleepFunc func(time.Duration)
@@ -35,11 +36,12 @@ type Fakelag struct {
lastTouch time.Time
}
func NewFakelag(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint) *Fakelag {
func NewFakelag(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint, cooldown time.Duration) *Fakelag {
return &Fakelag{
window: window,
burstLimit: burstLimit,
throttleMessagesPerWindow: throttleMessagesPerWindow,
cooldown: cooldown,
nowFunc: time.Now,
sleepFunc: time.Sleep,
state: FakelagBursting,
@@ -59,8 +61,7 @@ func (fl *Fakelag) Touch() {
if fl.state == FakelagBursting {
// determine if the previous burst is over
// (we could use 2*window instead)
if elapsed > fl.window {
if elapsed > fl.cooldown {
fl.burstCount = 0
}
@@ -77,8 +78,8 @@ func (fl *Fakelag) Touch() {
}
if fl.state == FakelagThrottled {
if elapsed > fl.window {
// let them burst again (as above, we could use 2*window instead)
if elapsed > fl.cooldown {
// let them burst again
fl.state = FakelagBursting
return
}
+3 -3
View File
@@ -39,8 +39,8 @@ func (mt *mockTime) lastSleep() (slept bool, duration time.Duration) {
return
}
func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint) (*Fakelag, *mockTime) {
fl := NewFakelag(window, burstLimit, throttleMessagesPerWindow)
func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint, cooldown time.Duration) (*Fakelag, *mockTime) {
fl := NewFakelag(window, burstLimit, throttleMessagesPerWindow, cooldown)
mt := new(mockTime)
mt.now, _ = time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
mt.lastCheckedSleep = -1
@@ -51,7 +51,7 @@ func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessage
func TestFakelag(t *testing.T) {
window, _ := time.ParseDuration("1s")
fl, mt := newFakelagForTesting(window, 3, 2)
fl, mt := newFakelagForTesting(window, 3, 2, window)
fl.Touch()
slept, _ := mt.lastSleep()