本文介绍了超时错误后,AWS Lambda函数停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的lambda函数,该函数异步进行API调用,然后返回数据. 99%的时间效果很好.只要API花费的时间超过了lambda配置的超时时间,它就会产生预期的错误.现在的问题是,当我以后再调用lambda函数时,它会永久性地给我超时错误.

I have a simple lambda function that asynchronously makes an API calls and then returns data. 99% of the time this works great. When ever the API takes longer then the lambda configured timeout, it gives an error as expected. Now the issue is that when I make any subsequent calls to the lambda function it permanently gives me the timeout error.

 "errorMessage": "2016-05-14T22:52:07.247Z {session} Task timed out after 3.00 seconds"

为了测试这种情况,我将lambda超时设置为3秒,并有一种方法可以在lambda中触发这两个函数.

In order to test that this was the case I set the lambda timeout to 3 seconds and have a way to trigger these two functions within the lambda.

JavaScript

function now() {
    return response.tell('success');
}

function wait() {
    setTimeout(function() { return response.tell('success'); }, 4000);
}

当我调用now函数时,没有任何问题.当我调用wait函数时,我收到超时错误,然后随后对now的任何调用都会给我同样的错误.

When I call the now function there are no problems. When I call the wait function I get the timeout error and then any subsequent calls to now give me the same error.

这是预期的行为吗?我认为随后对lambda函数的任何调用都应该起作用.我知道我总是可以增加配置超时时间,但宁愿不这样做.

Is this an expected behavior? I would think that any subsequent calls to the lambda function should work. I understand I can always increase the configuration timeout, but would rather not.

推荐答案

您应该查看功能句柄如何与特定的 context.callbackWaitsForEmptyEventLoop

You should look for how your function handle works with a specific context.callbackWaitsForEmptyEventLoop

如果该布尔类型为false,则将不会触发setTimeout,因为您可能早些时候已回答/处理过lambda调用.但是,如果callbackWaitsForEmptyEventLoop的值是true-那么您的代码将执行您想要的操作.

If that boolean-type is false, the setTimeout won't be ever fired, because you might've answered/handled the lambda invocation earlier.But if the value of callbackWaitsForEmptyEventLoop is true - then your code will do what you are looking for.

此外-直接通过回调处理所有内容可能会更容易,而无需手写"超时,更改配置超时等等……

Also - it's probably easier to handle everything via callbacks directly, without the need for "hand-written" timeouts, changing configuration timeouts and so on...

例如

function doneFactory(cb) { // closure factory returning a callback function which knows about res (response)
  return function(err, res) {
    if (err) {
      return cb(JSON.stringify(err));
    }
    return cb(null, res);
  };
}

// you're going to call this Lambda function from your code
exports.handle = function(event, context, handleCallback) {

  // allows for using callbacks as finish/error-handlers
  context.callbackWaitsForEmptyEventLoop = false;

  doSomeAsyncWork(event, context, doneFactory(handleCallback));
};

这篇关于超时错误后,AWS Lambda函数停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:24