简体   繁体   English

在 lambda 中发送大量 SQS 消息而不会超时

[英]Send a lot of SQS messages in a lambda without getting timed out

I have an array, lets say with a length of 9999. Now I want to send a message to SQS with the contents of each object in the array.我有一个数组,假设长度为 9999。现在我想向 SQS 发送一条消息,其中包含数组中每个 object 的内容。

Here is how I want to send the messages:这是我要发送消息的方式:

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);

Now, the problem is that I trigger this function via API Gateway, which times out after 30 seconds.现在,问题是我通过 API 网关触发了这个 function,它在 30 秒后超时。 How can I avoid this?我怎样才能避免这种情况? Is there a better way to do this?有一个更好的方法吗?

Based on your response in the comments I suggest you do two things:根据您在评论中的回复,我建议您做两件事:

  1. Use the SendMessageBatch API to reduce the number of API calls and speed up the process as @Marc B suggests使用SendMessageBatch API 来减少 API 调用的次数并按照@Marc B 的建议加快进程
  2. Set up asynchronous invocation of the backend Lambda function . 设置后台异步调用 Lambda function This means the API Gateway will just send the Event to your Lambda and not wait for the response.这意味着 API 网关只会将事件发送到您的 Lambda 而不会等待响应。 That means the Lambda functions is free to run up to 15 minutes.这意味着 Lambda 函数最多可以免费运行 15 分钟。 If you have more messages than can be handled in that period of time, you may want to look into StepFunctions.如果您的消息多于该时间段内无法处理的消息,您可能需要查看 StepFunctions。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM