3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

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

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()

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.

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
}

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()

View File

@ -392,15 +392,18 @@ limits:
# fakelag: prevents clients from spamming commands too rapidly
fakelag:
# whether to enforce fakelag
enabled: true
enabled: false
# time unit for counting command rates
window: 1s
# clients can send this many commands without fakelag being imposed
# (resets after a period of `window` elapses without any commands)
burst-limit: 5
# once clients have exceeded their burst allowance, they can send only
# this many commands per `window`:
messages-per-window: 2
# client status resets to the default state if they go this long without
# sending any commands:
cooldown: 1s