Update socket rate limiting (#3447)

## Description:

On replays, there can be a burst of traffic from hashes, so instead just
have a 2MB limit per client for the entire game. Also the winner message
can be 100s of kb on a large game with many players, so now we don't
need to put a special case for that.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
This commit is contained in:
Evan
2026-03-16 20:26:56 -07:00
committed by GitHub
parent 194a288ce1
commit 5e7317a818
2 changed files with 53 additions and 55 deletions
+31 -25
View File
@@ -5,7 +5,6 @@ const CLIENT_A = "clientA" as any;
const CLIENT_B = "clientB" as any;
const SMALL = 100;
const LARGE = 501; // over MAX_INTENT_BYTES
describe("ClientMsgRateLimiter", () => {
describe("intent messages", () => {
@@ -14,11 +13,6 @@ describe("ClientMsgRateLimiter", () => {
expect(limiter.check(CLIENT_A, "intent", SMALL)).toBe("ok");
});
it("kicks on oversized intent", () => {
const limiter = new ClientMsgRateLimiter();
expect(limiter.check(CLIENT_A, "intent", LARGE)).toBe("kick");
});
it("limits when per-second count exceeded", () => {
const limiter = new ClientMsgRateLimiter();
for (let i = 0; i < 10; i++) {
@@ -36,34 +30,46 @@ describe("ClientMsgRateLimiter", () => {
});
});
describe("winner messages", () => {
it("allows first winner message", () => {
describe("non-intent messages", () => {
it("does not rate-limit non-intent messages", () => {
const limiter = new ClientMsgRateLimiter();
expect(limiter.check(CLIENT_A, "winner", 50000)).toBe("ok");
for (let i = 0; i < 20; i++) {
expect(limiter.check(CLIENT_A, "winner", 50)).toBe("ok");
}
});
it("allows up to 3 winner messages", () => {
it("does not rate-limit ping messages", () => {
const limiter = new ClientMsgRateLimiter();
expect(limiter.check(CLIENT_A, "winner", 50000)).toBe("ok");
expect(limiter.check(CLIENT_A, "winner", 50000)).toBe("ok");
expect(limiter.check(CLIENT_A, "winner", 50000)).toBe("ok");
expect(limiter.check(CLIENT_A, "winner", 50000)).toBe("kick");
});
it("winner does not consume intent rate limit", () => {
const limiter = new ClientMsgRateLimiter();
limiter.check(CLIENT_A, "winner", 50000);
expect(limiter.check(CLIENT_A, "intent", SMALL)).toBe("ok");
for (let i = 0; i < 20; i++) {
expect(limiter.check(CLIENT_A, "ping", 50)).toBe("ok");
}
});
});
describe("other messages", () => {
it("applies rate limiting to other message types", () => {
describe("total bytes limit", () => {
it("kicks when cumulative bytes reach 2MB", () => {
const limiter = new ClientMsgRateLimiter();
for (let i = 0; i < 10; i++) {
expect(limiter.check(CLIENT_A, "ping", 50)).toBe("ok");
const chunkSize = 512 * 1024; // 512KB
// Send 3 chunks = 1.5MB, should be ok
for (let i = 0; i < 3; i++) {
expect(limiter.check(CLIENT_A, "other", chunkSize)).toBe("ok");
}
expect(limiter.check(CLIENT_A, "ping", 50)).toBe("limit");
// 4th chunk pushes to 2MB, should kick
expect(limiter.check(CLIENT_A, "other", chunkSize)).toBe("kick");
});
it("byte tracking is per client", () => {
const limiter = new ClientMsgRateLimiter();
const almostFull = 2 * 1024 * 1024 - 1;
expect(limiter.check(CLIENT_A, "other", almostFull)).toBe("ok");
// CLIENT_B should still be fine
expect(limiter.check(CLIENT_B, "other", 100)).toBe("ok");
});
it("kicks on bytes regardless of message type", () => {
const limiter = new ClientMsgRateLimiter();
const twoMB = 2 * 1024 * 1024;
expect(limiter.check(CLIENT_A, "intent", twoMB)).toBe("kick");
});
});
});