[英]Why Does my AWS lambda function randomly fail when using private elasticache network calls as well as external API calls?
I am trying to write a caching function that returns cached elasticcache data or makes an api call to retrieve that data.我正在尝试编写一个缓存 function 来返回缓存的 elasticcache 数据或进行 api 调用以检索该数据。 However, the lambda function seems to be very unrealiable and timing out often.
但是,lambda function 好像很不靠谱,经常超时。
It seems that the issue is having redis calls as well as public api calls causes the issue.问题似乎是有 redis 电话以及公共 api 电话导致了这个问题。 I can confirm that I have setup aws correctly with a su.net with an inte.net gateway and a private su.net with a nat gateway.
我可以确认我已经使用带有 inte.net 网关的 su.net 和带有 nat 网关的私有 su.net 正确设置了 aws。 The function works, but lonly 10 % of the time.The remaining times exceution is stopped right before making the API call.
function 有效,但只有 10% 的时间。剩余时间的执行在拨打 API 之前停止。 I have also noticed that the api calls fail after creating the redis client.
我还注意到在创建 redis 客户端后 api 调用失败。 If I make the external api call prior to making the redis check it seems the function is a lot more reliable and doesn't time out.
如果我在进行 redis 检查之前进行外部 api 调用,那么 function 似乎更可靠并且不会超时。
Not sure what to do.不知道该怎么办。 Is it best practice to seperate these 2 tasks or am I doing something wrong?
将这两项任务分开是最佳做法还是我做错了什么?
let data = null;
module.exports.handler = async (event) => {
//context.callbackWaitsForEmptyEventLoop = false;
let client;
try {
client = new Redis(
6379,
"redis://---.---.ng.0001.use1.cache.amazonaws.com"
);
client.get(event.token, async (err, result) => {
if (err) {
console.error(err);
} else {
data = result;
await client.quit();
}
});
if (data && new Date().getTime() / 1000 - eval(data).timestamp < 30) {
res.send(`({
"address": "${token}",
"price": "${eval(data).price}",
"timestamp": "${eval(data).timestamp}"
})`);
} else {
getPrice(event); //fetch api data
}
```
There a lot of misunderstand in your code.您的代码中存在很多误解。 I'll try to guide you to fix it and understand how to do that correctly.
我将尝试指导您修复它并了解如何正确执行此操作。
To help you in this task, I completely rewrite your code solving these misundersand.为了帮助您完成这项任务,我完全重写了您的代码以解决这些误解。
const Redis = require('ioredis');
module.exports.handler = async (event, context, callback) => {
// prefer to use lambda env instead of put directly in the code
const client = new Redis(
"REDIS_PORT_ENV",
"REDIS_HOST_ENV"
);
const data = await client.get(event.token);
client.quit();
const parsedData = JSON.parse(data);
if (parsedDate && new Date().getTime() / 1000 - parsedData.timestamp < 30) {
callback(null, {
address: event.token,
price: parsedData.price,
timestamp: parsedData.timestamp
});
} else {
const dataFromApi = await getPrice(event);
callback(null, dataFromApi);
}
};
There another usage with lambdas that return an object instead of pass a object inside callback, but I think you get the idea and understood your mistakes. lambda 的另一种用法是返回 object 而不是传递 object 内部回调,但我认为你明白了你的想法并理解了你的错误。
Follow the docs about correctly usage of lambda: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-functions.html遵循有关正确使用 lambda 的文档: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-functions.html
To undestand more about async and sync in javascript: https://www.freecodecamp.org/news/synchronous-vs-asynchronous-in-javascript/要了解更多关于 javascript 中的异步和同步: https://www.freecodecamp.org/news/synchronous-vs-asynchronous-in-javascript/
JSON.parse x eval: JSON.parse vs. eval() JSON.parse x eval: JSON.parse 与 eval()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.