在 lambda 中发送大量 SQS 消息而不会超时
Send a lot of SQS messages in a lambda without getting timed out
我有一个数组,假设长度为 9999。现在我想向 SQS 发送一条消息,其中包含数组中每个对象的内容。
这是我想要发送消息的方式:
const promises = tokens.map((token) => {
const params = {
Body: JSON.stringify(data),
};
return sqs.sendMessage({
MessageBody: JSON.stringify(params),
QueueUrl: 'my-queue-url',
}).promise();
});
await Promise.all(promises);
现在,问题是我通过 API 网关触发了此功能,该功能在 30 秒后超时。我怎样才能避免这种情况?有更好的方法吗?
根据您在评论中的回复,我建议您做两件事:
- 使用SendMessageBatch API to reduce the number of API calls and speed up the process as
- Set up asynchronous invocation of the backend Lambda function。这意味着 API 网关只会将事件发送到您的 Lambda 而不会等待响应。这意味着 Lambda 函数可以免费使用 运行 最多 15 分钟。如果您的消息多于该时间段内无法处理的消息,您可能需要查看 StepFunctions。
我有一个数组,假设长度为 9999。现在我想向 SQS 发送一条消息,其中包含数组中每个对象的内容。
这是我想要发送消息的方式:
const promises = tokens.map((token) => {
const params = {
Body: JSON.stringify(data),
};
return sqs.sendMessage({
MessageBody: JSON.stringify(params),
QueueUrl: 'my-queue-url',
}).promise();
});
await Promise.all(promises);
现在,问题是我通过 API 网关触发了此功能,该功能在 30 秒后超时。我怎样才能避免这种情况?有更好的方法吗?
根据您在评论中的回复,我建议您做两件事:
- 使用SendMessageBatch API to reduce the number of API calls and speed up the process as
- Set up asynchronous invocation of the backend Lambda function。这意味着 API 网关只会将事件发送到您的 Lambda 而不会等待响应。这意味着 Lambda 函数可以免费使用 运行 最多 15 分钟。如果您的消息多于该时间段内无法处理的消息,您可能需要查看 StepFunctions。