3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +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 nil
} }
return NewFakelag(flc.Window, flc.BurstLimit, flc.MessagesPerWindow) return NewFakelag(flc.Window, flc.BurstLimit, flc.MessagesPerWindow, flc.Cooldown)
}() }()
client.stateMutex.Lock() client.stateMutex.Lock()

View File

@ -194,6 +194,7 @@ type FakelagConfig struct {
Window time.Duration Window time.Duration
BurstLimit uint `yaml:"burst-limit"` BurstLimit uint `yaml:"burst-limit"`
MessagesPerWindow uint `yaml:"messages-per-window"` MessagesPerWindow uint `yaml:"messages-per-window"`
Cooldown time.Duration
} }
// Config defines the overall configuration. // Config defines the overall configuration.

View File

@ -27,6 +27,7 @@ type Fakelag struct {
window time.Duration window time.Duration
burstLimit uint burstLimit uint
throttleMessagesPerWindow uint throttleMessagesPerWindow uint
cooldown time.Duration
nowFunc func() time.Time nowFunc func() time.Time
sleepFunc func(time.Duration) sleepFunc func(time.Duration)
@ -35,11 +36,12 @@ type Fakelag struct {
lastTouch time.Time 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{ return &Fakelag{
window: window, window: window,
burstLimit: burstLimit, burstLimit: burstLimit,
throttleMessagesPerWindow: throttleMessagesPerWindow, throttleMessagesPerWindow: throttleMessagesPerWindow,
cooldown: cooldown,
nowFunc: time.Now, nowFunc: time.Now,
sleepFunc: time.Sleep, sleepFunc: time.Sleep,
state: FakelagBursting, state: FakelagBursting,
@ -59,8 +61,7 @@ func (fl *Fakelag) Touch() {
if fl.state == FakelagBursting { if fl.state == FakelagBursting {
// determine if the previous burst is over // determine if the previous burst is over
// (we could use 2*window instead) if elapsed > fl.cooldown {
if elapsed > fl.window {
fl.burstCount = 0 fl.burstCount = 0
} }
@ -77,8 +78,8 @@ func (fl *Fakelag) Touch() {
} }
if fl.state == FakelagThrottled { if fl.state == FakelagThrottled {
if elapsed > fl.window { if elapsed > fl.cooldown {
// let them burst again (as above, we could use 2*window instead) // let them burst again
fl.state = FakelagBursting fl.state = FakelagBursting
return return
} }

View File

@ -39,8 +39,8 @@ func (mt *mockTime) lastSleep() (slept bool, duration time.Duration) {
return return
} }
func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint) (*Fakelag, *mockTime) { func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint, cooldown time.Duration) (*Fakelag, *mockTime) {
fl := NewFakelag(window, burstLimit, throttleMessagesPerWindow) fl := NewFakelag(window, burstLimit, throttleMessagesPerWindow, cooldown)
mt := new(mockTime) 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.now, _ = time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
mt.lastCheckedSleep = -1 mt.lastCheckedSleep = -1
@ -51,7 +51,7 @@ func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessage
func TestFakelag(t *testing.T) { func TestFakelag(t *testing.T) {
window, _ := time.ParseDuration("1s") window, _ := time.ParseDuration("1s")
fl, mt := newFakelagForTesting(window, 3, 2) fl, mt := newFakelagForTesting(window, 3, 2, window)
fl.Touch() fl.Touch()
slept, _ := mt.lastSleep() slept, _ := mt.lastSleep()

View File

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