-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Batch move_warship intents to stay under MAX_INTENT_SIZE #4735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bighurdan-cell
wants to merge
5
commits into
openfrontio:main
Choose a base branch
from
bighurdan-cell:fix/move-warship-intent-batching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fbb056
Batch move_warship intents to stay under MAX_INTENT_SIZE
Hurdan1 0f4ffab
Bound unitIds in the schema and translate the too_much_data kick reason
Hurdan1 4b82404
Pace batched move_warship intents at a fixed interval
Hurdan1 72c74d4
Keep paced batches queued until the socket accepts them
Hurdan1 7768d8a
Only send intents once the server has acknowledged the join
Hurdan1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Sends queued work at a fixed minimum spacing. | ||
| * | ||
| * The server drops intents past its per-second budget, so a burst of batches | ||
| * has to be spread out rather than emitted all at once. | ||
| */ | ||
| export class PacedSender { | ||
| private readonly queue: (() => boolean)[] = []; | ||
| private timer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| constructor(private readonly intervalMs: number) {} | ||
|
|
||
| push(send: () => boolean): void { | ||
| this.queue.push(send); | ||
| if (this.timer === null) this.next(); | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.queue.length = 0; | ||
| if (this.timer !== null) { | ||
| clearTimeout(this.timer); | ||
| this.timer = null; | ||
| } | ||
| } | ||
|
|
||
| private next(): void { | ||
| const send = this.queue[0]; | ||
| if (send === undefined) { | ||
| this.timer = null; | ||
| return; | ||
| } | ||
| // Keep the item queued if it could not be sent, so a reconnect does not | ||
| // lose it. leaveGame() clears the queue when the game is over. | ||
| if (send()) this.queue.shift(); | ||
| this.timer = setTimeout(() => this.next(), this.intervalMs); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -534,10 +534,35 @@ export const CancelBoatIntentSchema = z.object({ | |
|
|
||
| export const MoveWarshipIntentSchema = z.object({ | ||
| type: z.literal("move_warship"), | ||
| unitIds: z.array(z.number().int()).nonempty(), | ||
| unitIds: z.array(z.number().int()).nonempty().max(1000), | ||
| tile: z.number(), | ||
| }); | ||
|
|
||
| // Client messages larger than this get the client kicked by ClientMsgRateLimiter. | ||
| export const MAX_INTENT_SIZE = 2000; | ||
|
|
||
| export function batchMoveWarshipUnitIds( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this funciton shouldn't be in Schemas.ts |
||
| unitIds: readonly number[], | ||
| tile: number, | ||
| ): number[][] { | ||
| const overhead = JSON.stringify({ | ||
| type: "intent", | ||
| intent: { type: "move_warship", unitIds: [], tile }, | ||
| } satisfies ClientIntentMessage).length; | ||
| const batches: number[][] = []; | ||
| let size = overhead; | ||
| for (const unitId of unitIds) { | ||
| const unitIdSize = String(unitId).length + 1; | ||
| if (batches.length === 0 || size + unitIdSize > MAX_INTENT_SIZE) { | ||
| batches.push([]); | ||
| size = overhead; | ||
| } | ||
| batches[batches.length - 1].push(unitId); | ||
| size += unitIdSize; | ||
| } | ||
| return batches; | ||
| } | ||
|
|
||
| export const DeleteUnitIntentSchema = z.object({ | ||
| type: z.literal("delete_unit"), | ||
| unitId: z.number(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { | ||
| batchMoveWarshipUnitIds, | ||
| ClientIntentMessage, | ||
| ClientMessageSchema, | ||
| MAX_INTENT_SIZE, | ||
| } from "../src/core/Schemas"; | ||
| import { replacer } from "../src/core/Util"; | ||
|
|
||
| const TILE = 250_000; | ||
|
|
||
| function frame(unitIds: number[], tile: number): string { | ||
| return JSON.stringify( | ||
| { | ||
| type: "intent", | ||
| intent: { type: "move_warship", unitIds, tile }, | ||
| } satisfies ClientIntentMessage, | ||
| replacer, | ||
| ); | ||
| } | ||
|
|
||
| describe("batchMoveWarshipUnitIds", () => { | ||
| test("sends a small fleet as a single intent", () => { | ||
| const unitIds = [10000, 10003, 10006]; | ||
| expect(batchMoveWarshipUnitIds(unitIds, TILE)).toEqual([unitIds]); | ||
| }); | ||
|
|
||
| test("returns no batches for an empty selection", () => { | ||
| expect(batchMoveWarshipUnitIds([], TILE)).toEqual([]); | ||
| }); | ||
|
|
||
| test.each([1, 400, 4000])( | ||
| "keeps every batch under the server cap (%i warships)", | ||
| (count) => { | ||
| const unitIds = Array.from({ length: count }, (_, i) => 900_000 + i * 3); | ||
| const batches = batchMoveWarshipUnitIds(unitIds, TILE); | ||
|
|
||
| for (const batch of batches) { | ||
| expect(batch.length).toBeGreaterThan(0); | ||
| expect( | ||
| Buffer.byteLength(frame(batch, TILE), "utf8"), | ||
| ).toBeLessThanOrEqual(MAX_INTENT_SIZE); | ||
| } | ||
| expect(batches.flat()).toEqual(unitIds); | ||
| }, | ||
| ); | ||
|
|
||
| test("every batch is a valid client intent message", () => { | ||
| const unitIds = Array.from({ length: 450 }, (_, i) => 10_000 + i * 3); | ||
|
|
||
| for (const batch of batchMoveWarshipUnitIds(unitIds, TILE)) { | ||
| expect( | ||
| ClientMessageSchema.safeParse(JSON.parse(frame(batch, TILE))).success, | ||
| ).toBe(true); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.