如何解决AWS AppSync变更超时问题?

huangapple go评论58阅读模式
英文:

How to solve the aws appsync mutation timeout issue?

问题

这是我用于从DynamoDB中删除项目的函数。我将要删除的对象列表传递给deleteExpiredFcm函数。从日志消息中,我知道变异存在某种冲突,导致超时问题。此外,我实现了分页以避免超时问题,因为对象列表可能包含数百甚至数千个项目。我漏掉了什么?

async function deleteExpiredFcm(expiredFcm) {
  console.log("删除过期的FCM...");
  const batchSize = 10; // 根据需要调整批次大小
  const totalBatches = Math.ceil(expiredFcm.length / batchSize);

  for (let i = 0; i < totalBatches; i++) {
    const batch = expiredFcm.slice(i * batchSize, (i + 1) * batchSize);

    const mutationPromises = batch.map(async (element) => {
      const mutationObj = deleteExpiredFcmObj;
      const variables = {
        input: {
          id: element.id,
        },
      };

      let retries = 3; // 重试次数
      let success = false;

      while (retries > 0 && !success) {
        try {
          const { data } = await client.mutate({
            mutation: mutationObj,
            variables: variables,
          });
          console.log(
            "(变异): 删除数据 ----------->",
            data.deleteFcmNotification
          );
          success = true;
        } catch (error) {
          if (
            error.graphQLErrors.some(
              (err) => err.errorType === "ConflictUnhandled"
            )
          ) {
            console.log("发生冲突,正在重试...");
            retries--;
          } else {
            console.error("删除FCM时出错:", error);
            break;
          }
        }
      }
    });

    await Promise.all(mutationPromises); // 等待每个批次的完成
  }
}

编辑:
我在变异输入中添加了_version,所以现在它可以删除项目了。然而,我仍然遇到超时问题,
{
"errorMessage": "2023-06-13T12:56:25.895Z 7c81deb4-ccea-4cf0-ac59-f72c5e1becf9 任务在3.05秒后超时"
}

如何摆脱它?

英文:

I have a lambda that performs mutations to delete a bunch of items, but the timeout issue always occurs.

This is the function I use to delete items from the dynamoDB. I pass a list of objects to be removed into the deleteExpiredFcm function. From the log message, I know the mutation is having some kind of conflict that causes the timeout issue. Also, I implemented pagination to avoid the timeout issue, as the object list may consist of hundreds or even thousands of items. What am I missing?

async function deleteExpiredFcm(expiredFcm) {
console.log(&quot;Deleting expired FCM...&quot;);
const batchSize = 10; // Adjust the batch size as needed
const totalBatches = Math.ceil(expiredFcm.length / batchSize);
for (let i = 0; i &lt; totalBatches; i++) {
const batch = expiredFcm.slice(i * batchSize, (i + 1) * batchSize);
const mutationPromises = batch.map(async (element) =&gt; {
const mutationObj = deleteExpiredFcmObj;
const variables = {
input: {
id: element.id,
},
};
let retries = 3; // Number of retries
let success = false;
while (retries &gt; 0 &amp;&amp; !success) {
try {
const { data } = await client.mutate({
mutation: mutationObj,
variables: variables,
});
console.log(
&quot;(Mutation): Deleting Data -----------&gt;&quot;,
data.deleteFcmNotification
);
success = true;
} catch (error) {
if (
error.graphQLErrors.some(
(err) =&gt; err.errorType === &quot;ConflictUnhandled&quot;
)
) {
console.log(&quot;Conflict occurred, retrying...&quot;);
retries--;
} else {
console.error(&quot;Error deleting FCM:&quot;, error);
break;
}
}
}
});
await Promise.all(mutationPromises); // Await the completion of each batch
}
}

Edit:
I added _version to the mutation input, so it deletes items now. However I'm still getting the timeout issue,
{
"errorMessage": "2023-06-13T12:56:25.895Z 7c81deb4-ccea-4cf0-ac59-f72c5e1becf9 Task timed out after 3.05 seconds"
}

How can I get rid of it?

答案1

得分: 0

为解决GraphQL错误,我在变异输入中添加了 _version。

const variables = {
    input: {
        id: element.id,
        _version: element._version,
    },
};

为解决超时问题,我只是增加了Lambda通用配置中的超时限制。这暂时解决了问题。请告诉我是否有更适当的方法来解决超时问题。

英文:

To solve the GraphQL error, I added _version to the mutation input.

const variables = {
input: {
id: element.id,
_version: element._version,
},
};

To solve the timeout issue, I simply increased the timeout limit in the lambda general configuration. This solves the issue for now. Please let me know if there is a more proper way to solve the timeout issue.

huangapple
  • 本文由 发表于 2023年6月13日 04:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定