@ydbjs/topic
Version:
YDB Topics client for publish-subscribe messaging. Provides at-least-once delivery, exactly-once publishing, FIFO guarantees, and scalable message processing for unstructured data.
33 lines • 1.36 kB
JavaScript
import { MAX_BATCH_SIZE } from "./constants.js";
export const _batch_messages = function batch_messages(messages) {
let batches = [];
// Build batch until size limit or no more messages
while (messages.length > 0) {
let batch = [];
let batchSize = 0n;
// Build batch until size limit or no more messages
while (messages.length > 0) {
let message = messages[0];
// Check if adding this message would exceed the batch size limit
if (batchSize + BigInt(message.data.length) > MAX_BATCH_SIZE) {
// If the batch already has messages, send it
if (batch.length > 0) {
break;
}
// If this is a single message exceeding the limit, we still need to send it
batch.push(messages.shift());
break;
}
// Add message to current batch
batch.push(messages.shift());
batchSize += BigInt(message.data.length);
}
// If the batch is not empty, add it to the batches array
// This ensures that we always send at least one message, even if it exceeds the batch size limit.
if (batch.length > 0) {
batches.push(batch);
}
}
return batches;
};
//# sourceMappingURL=_batch_messages.js.map