Skip to content

Commit cb5ce97

Browse files
committed
fix: avoid grouping all events
We were grouping all events we were called for, and only looking at the non-`aws:sqs` events to warn about them. This can be done as we go, and then we only need to keep the SQS events in memory.
1 parent 1de7c27 commit cb5ce97

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

lambdas/functions/control-plane/src/lambda.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,23 @@ export async function scaleUpHandler(event: SQSEvent, context: Context): Promise
1414
setContext(context, 'lambda.ts');
1515
logger.logEventIfEnabled(event);
1616

17-
// Group the messages by their event source. We're only interested in
18-
// `aws:sqs`-originated messages.
19-
const groupedEvents = new Map<string, ActionRequestMessageSQS[]>();
20-
for (const { body, eventSource, messageId } of event.Records) {
21-
const group = groupedEvents.get(eventSource) || [];
22-
const payload = JSON.parse(body) as ActionRequestMessage;
17+
const sqsMessages: ActionRequestMessageSQS[] = [];
18+
const warnedEventSources = new Set<string>();
2319

24-
if (group.length === 0) {
25-
groupedEvents.set(eventSource, group);
26-
}
27-
28-
groupedEvents.get(eventSource)?.push({
29-
...payload,
30-
messageId,
31-
});
32-
}
20+
for (const { body, eventSource, messageId } of event.Records) {
21+
if (eventSource !== 'aws:sqs') {
22+
if (!warnedEventSources.has(eventSource)) {
23+
logger.warn('Ignoring non-sqs event source', { eventSource });
24+
warnedEventSources.add(eventSource);
25+
}
3326

34-
for (const [eventSource, messages] of groupedEvents.entries()) {
35-
if (eventSource === 'aws:sqs') {
3627
continue;
3728
}
3829

39-
logger.warn('Ignoring non-sqs event source', { eventSource, messages });
30+
const payload = JSON.parse(body) as ActionRequestMessage;
31+
sqsMessages.push({ ...payload, messageId });
4032
}
4133

42-
const sqsMessages = groupedEvents.get('aws:sqs') ?? [];
43-
4434
// Sort messages by their retry count, so that we retry the same messages if
4535
// there's a persistent failure. This should cause messages to be dropped
4636
// quicker than if we retried in an arbitrary order.

0 commit comments

Comments
 (0)