Skip to content

Batch by total request message size. #410

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-stream",
"version": "1.1.2",
"version": "1.1.3",
"description": "Create stream processors with AWS Lambda functions.",
"keywords": [
"aws",
Expand Down
14 changes: 12 additions & 2 deletions src/sinks/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import _ from 'highland';

import Connector from '../connectors/sqs';

import { toBatchUow, unBatchUow } from '../utils/batch';
import { batchWithSize, toBatchUow, unBatchUow } from '../utils/batch';
import { ratelimit } from '../utils/ratelimit';
import { rejectWithFault } from '../utils/faults';
import { debug as d } from '../utils/print';
import { storeClaimcheck } from './claimcheck';

export const sendToSqs = ({ // eslint-disable-line import/prefer-default-export
id: pipelineId,
debug = d('sqs'),
queueUrl = process.env.QUEUE_URL,
messageField = 'message',
batchSize = Number(process.env.SQS_BATCH_SIZE) || Number(process.env.BATCH_SIZE) || 10,
maxPublishRequestSize = Number(process.env.PUBLISH_MAX_REQ_SIZE) || Number(process.env.MAX_REQ_SIZE) || 256 * 1024,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the batchWithSize util has event specific logic, like claim checks
  • this sqs sink is used for non-event type messaging in egress gateways
  • it would be nice to iterate on the util to make it more generic
  • but maybe in this case a custom sqs sink is warranted

parallel = Number(process.env.SQS_PARALLEL) || Number(process.env.PARALLEL) || 8,
step = 'send',
...opt
Expand Down Expand Up @@ -46,7 +48,15 @@ export const sendToSqs = ({ // eslint-disable-line import/prefer-default-export
return (s) => s
.through(ratelimit(opt))

.batch(batchSize)
.consume(batchWithSize({
...opt,
batchSize,
maxRequestSize: maxPublishRequestSize,
requestEntryField: messageField,
claimcheckEventField: 'MessageBody',
debug,
}))
.through(storeClaimcheck(opt))
.map(toBatchUow)

.map(toInputParams)
Expand Down
8 changes: 5 additions & 3 deletions src/utils/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const compact = (rule) => {
export const batchWithSize = ({
claimCheckBucketName = process.env.CLAIMCHECK_BUCKET_NAME,
putClaimcheckRequest = 'putClaimcheckRequest',
// Detail handles EB, but others like SQS may need something else like 'MessageBody'
claimcheckEventField = 'Detail',
...opt
}) => {
let batched = [];
Expand All @@ -77,9 +79,9 @@ export const batchWithSize = ({
logMetrics([x], [size], opt);
if (claimCheckBucketName) {
// setup claim check
x[putClaimcheckRequest] = toPutClaimcheckRequest(JSON.parse(x[opt.requestEntryField].Detail), claimCheckBucketName);
x[opt.requestEntryField].Detail = JSON.stringify(toClaimcheckEvent(
JSON.parse(x[opt.requestEntryField].Detail),
x[putClaimcheckRequest] = toPutClaimcheckRequest(JSON.parse(x[opt.requestEntryField][claimcheckEventField]), claimCheckBucketName);
x[opt.requestEntryField][claimcheckEventField] = JSON.stringify(toClaimcheckEvent(
JSON.parse(x[opt.requestEntryField][claimcheckEventField]),
claimCheckBucketName,
));
size = Buffer.byteLength(JSON.stringify(x[opt.requestEntryField]));
Expand Down
80 changes: 79 additions & 1 deletion test/unit/sinks/sqs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe('sinks/sqs.js', () => {
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
},
}, {
message: {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
},
}];

_(uows)
Expand All @@ -26,7 +31,7 @@ describe('sinks/sqs.js', () => {
.tap((collected) => {
// console.log(JSON.stringify(collected, null, 2));

expect(collected.length).to.equal(1);
expect(collected.length).to.equal(2);
expect(collected[0]).to.deep.equal({
message: {
Id: '1',
Expand All @@ -36,6 +41,79 @@ describe('sinks/sqs.js', () => {
Entries: [{
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
}, {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
}],
},
sendMessageBatchResponse: {},
});
expect(collected[1]).to.deep.equal({
message: {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
},
inputParams: {
Entries: [{
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
}, {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
}],
},
sendMessageBatchResponse: {},
});
})
.done(done);
});

it('should split a batch due to size constraints', (done) => {
sinon.stub(Connector.prototype, 'sendMessageBatch').resolves({});

const uows = [{
message: {
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
},
}, {
message: {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
},
}];

_(uows)
.through(sendToSqs({
maxPublishRequestSize: 50,
}))
.collect()
.tap((collected) => {
// console.log(JSON.stringify(collected, null, 2));

expect(collected.length).to.equal(2);
expect(collected[0]).to.deep.equal({
message: {
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
},
inputParams: {
Entries: [{
Id: '1',
MessageBody: JSON.stringify({ f1: 'v1' }),
}],
},
sendMessageBatchResponse: {},
});
expect(collected[1]).to.deep.equal({
message: {
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
},
inputParams: {
Entries: [{
Id: '2',
MessageBody: JSON.stringify({ f1: 'v2' }),
}],
},
sendMessageBatchResponse: {},
Expand Down