-
-
Notifications
You must be signed in to change notification settings - Fork 120
Message byte estimation and batching on @fedify/cfworkers #960
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -93,6 +93,13 @@ interface WrappedMessage { | |||||||||||||
| readonly __fedify_payload__: any; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * enqueueMany Limit settings. | ||||||||||||||
| */ | ||||||||||||||
| const MAX_BATCH_MESSAGES = 100; | ||||||||||||||
| const MAX_ESTIMATED_BATCH_BYTES = 240_000; | ||||||||||||||
| const ESTIMATED_METADATA_BYTES_PER_MESSAGE = 128; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Result from {@link WorkersMessageQueue.processMessage}. | ||||||||||||||
| * @since 2.0.0 | ||||||||||||||
|
|
@@ -339,16 +346,57 @@ export class WorkersMessageQueue implements MessageQueue { | |||||||||||||
| messages: readonly any[], | ||||||||||||||
| options?: MessageQueueEnqueueOptions, | ||||||||||||||
| ): Promise<void> { | ||||||||||||||
| const requests: MessageSendRequest[] = messages.map((msg) => ({ | ||||||||||||||
| body: { | ||||||||||||||
| const utf8Encoder = new TextEncoder(); | ||||||||||||||
|
|
||||||||||||||
| const estimateMessageBytes = (body: WrappedMessage): number => { | ||||||||||||||
| const serialized = JSON.stringify(body); | ||||||||||||||
|
|
||||||||||||||
| if (serialized === undefined) { | ||||||||||||||
| throw new TypeError("Queue message must be JSON-serializable."); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+352
to
+356
Contributor
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. Since
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| return utf8Encoder.encode(serialized).byteLength + | ||||||||||||||
| ESTIMATED_METADATA_BYTES_PER_MESSAGE; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| const delaySeconds = options?.delay?.total("seconds") ?? 0; | ||||||||||||||
|
|
||||||||||||||
| let batch: MessageSendRequest[] = []; | ||||||||||||||
| let estimatedBatchBytes = 0; | ||||||||||||||
|
|
||||||||||||||
| const flush = async (): Promise<void> => { | ||||||||||||||
| if (batch.length === 0) return; | ||||||||||||||
|
|
||||||||||||||
| await this.#queue.sendBatch(batch, { delaySeconds }); | ||||||||||||||
|
|
||||||||||||||
| batch = []; | ||||||||||||||
| estimatedBatchBytes = 0; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| for (const message of messages) { | ||||||||||||||
| const body = { | ||||||||||||||
| __fedify_ordering_key__: options?.orderingKey, | ||||||||||||||
| __fedify_payload__: msg, | ||||||||||||||
| } satisfies WrappedMessage, | ||||||||||||||
| contentType: "json", | ||||||||||||||
| })); | ||||||||||||||
| await this.#queue.sendBatch(requests, { | ||||||||||||||
| delaySeconds: options?.delay?.total("seconds") ?? 0, | ||||||||||||||
| }); | ||||||||||||||
| __fedify_payload__: message, | ||||||||||||||
|
Comment on lines
+376
to
+379
|
||||||||||||||
| } satisfies WrappedMessage; | ||||||||||||||
|
|
||||||||||||||
| const request = { | ||||||||||||||
| body, | ||||||||||||||
| contentType: "json", | ||||||||||||||
| } satisfies MessageSendRequest; | ||||||||||||||
|
|
||||||||||||||
| const messageBytes = estimateMessageBytes(body); | ||||||||||||||
| const exceedsBatchLimit = batch.length >= MAX_BATCH_MESSAGES || | ||||||||||||||
| estimatedBatchBytes + messageBytes > MAX_ESTIMATED_BATCH_BYTES; | ||||||||||||||
|
Comment on lines
+388
to
+389
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.
When one serialized wrapped message is over Cloudflare's 128 KB per-item limit but under this 240 KB batch threshold, Useful? React with 👍 / 👎. |
||||||||||||||
|
|
||||||||||||||
| if (batch.length > 0 && exceedsBatchLimit) { | ||||||||||||||
| await flush(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| batch.push(request); | ||||||||||||||
|
Comment on lines
+391
to
+395
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.
Fresh evidence: this version still only flushes when Useful? React with 👍 / 👎. |
||||||||||||||
| estimatedBatchBytes += messageBytes; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| await flush(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit is explicitly a bug fix for #958, but the patch only changes the implementation; the root
AGENTS.md“Bugfix process” requires both a regression test demonstrating the bug and aCHANGES.mdentry. Without tests covering the >100-message and >256 KB split cases, this batching fix can regress silently, and the required release note for the user-facing Cloudflare Queues failure is missing.Useful? React with 👍 / 👎.