Browse Source

fix fakelag double-rate issue

Basically, fakelag was counting the time imposed by its own sleeps as though
the user had themselves paused for that amount of time. Therefore, if a user
sent a large number of consecutive commands, every other command would pause
for the expected throttle interval, but the subsequent command would be
processed instantly (you'd get two back-to-back commands). This resulted in
throttled users being able to send at double the expected rate.
tags/v0.12.0
Shivaram Lingamneni 6 years ago
parent
commit
f6d2dade4e
2 changed files with 8 additions and 5 deletions
  1. 4
    3
      irc/fakelag.go
  2. 4
    2
      irc/fakelag_test.go

+ 4
- 3
irc/fakelag.go View File

@@ -85,9 +85,10 @@ func (fl *Fakelag) Touch() {
85 85
 		}
86 86
 		// space them out by at least window/messagesperwindow
87 87
 		sleepDuration := time.Duration((int64(fl.window) / int64(fl.throttleMessagesPerWindow)) - int64(elapsed))
88
-		if sleepDuration < 0 {
89
-			sleepDuration = 0
88
+		if sleepDuration > 0 {
89
+			fl.sleepFunc(sleepDuration)
90
+			// the touch time should take into account the time we slept
91
+			fl.lastTouch = fl.nowFunc()
90 92
 		}
91
-		fl.sleepFunc(sleepDuration)
92 93
 	}
93 94
 }

+ 4
- 2
irc/fakelag_test.go View File

@@ -83,13 +83,15 @@ func TestFakelag(t *testing.T) {
83 83
 		t.Fatalf("incorrect sleep time: %v != %v", expected, duration)
84 84
 	}
85 85
 
86
+	// send another message without a pause; we should have to sleep for 500 msec
86 87
 	fl.Touch()
87 88
 	if fl.state != FakelagThrottled {
88 89
 		t.Fatalf("should be throttled")
89 90
 	}
90 91
 	slept, duration = mt.lastSleep()
91
-	if duration != interval {
92
-		t.Fatalf("incorrect sleep time: %v != %v", interval, duration)
92
+	expected, _ = time.ParseDuration("500ms")
93
+	if duration != expected {
94
+		t.Fatalf("incorrect sleep time: %v != %v", duration, expected)
93 95
 	}
94 96
 
95 97
 	mt.pause(interval * 6)

Loading…
Cancel
Save