在Lambda函数中关闭DAX客户端 [英] Closing DAX Client in Lambda Function

本文介绍了在Lambda函数中关闭DAX客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要部署为AWS Lambda函数的node.js函数。我已经注意到,当我使用redis Elasticache时,必须关闭使用redis.createClient打开的连接,否则Lambda函数会超时。我可以通过简单地调用客户端的quit()方法来实现。如果在发出Lambda回调之前执行此操作,则Lambda函数将按预期结束。如果不这样做,则Lambda函数会在我设置的任何超时间隔内超时。在测试设置中,我有一个Lambda函数,该函数在缓存上运行并在30毫秒内终止。如果没有调用redis client quit()方法,则相同的Lambda函数不会在1分钟超时之前终止。我不确定,但是我认为Lambda函数的行为方式是这样的,因为它不知道该函数是否完成了执行(即使在回调之后),因为redis连接仍然处于活动状态。

I have a node.js functon that I want to deploy as a AWS Lambda function. I have noticed that when I use redis Elasticache, I must close the connection I opened with redis.createClient or else the Lambda function times out. I do this by simply calling the client's quit() method. If I do this prior to issuing the Lambda callback, the Lambda function ends as expected. If I do not, the Lambda function times out at whatever timeout interval I set. In a test setting, I have a Lambda function that operated on the cache and terminates in 30 milliseconds. Without the call to the redis client quit() method, the same Lambda function won't terminate before a 1 minute timeout. I'm not sure, but I assume the Lambda function behaves this way because it doesn't know whether the function is done executing (even after the callback) since the redis connection is still active.

我并不关心这个问题,因为调用quit()方法很容易。我遇到的问题是当我尝试使用DynamoDB DAX客户端执行类似操作时。我可以让Lambda函数直接通过我的VPC端点访问DynamoDB,并且一切正常。如果使用DAX客户端,则实际上会对表进行了修改(我插入了一个项目,可以看到它在那里),但是Lambda函数总是超时。

I'm not all that concerned about this because it's easy enough to call the quit() method. The problem I am having is when I try to do something similar with a DynamoDB DAX client. I can have the Lambda function access DynamoDB directly through my VPC endpoint and everything works fine. If I use the DAX client, the modifications to the table actually take place (I inserted an item and can see that it is there), but the Lambda function always times out.

下面是一些示例代码:

const AmazonDaxClient = require('amazon-dax-client');
const AWS             = require('aws-sdk');
const config          = require('./config');

AWS.config.update(config.aws);
var ddbClient = new AWS.DynamoDB.DocumentClient(config.dynamodb);
var dax = new AmazonDaxClient(config.dax);
var daxClient = new AWS.DynamoDB.DocumentClient({service: dax });

如果我只是使用ddbClient,一切都会正常。如果我使用daxClient,那么所有功能也都可以工作(插入,删除,更新等),但是Lambda函数会超时。 daxClient是否有类似的quit()方法,它告诉DAX集群我已经完成,并且可以干净地关闭所有连接?我怀疑这是因为他们希望daxClient的行为与正常的ddbClient完全相同,并且ddbClient没有quit()方法。

If I just use the ddbClient, everything works. If I use the daxClient, everything also works (inserts, deletes, updates, etc.), but the Lambda function times out. Is there a similar quit() method for the daxClient that tells the DAX cluster I'm done and it can close all the connections cleanly? I suspect the problem with this is that they want the daxClient to behave exactly like a normal ddbClient, and there is not a quit() method for the ddbClient.

推荐答案

看起来像Lambda 上下文属性 callbackWaitsForEmptyEventLoop ,如此处 ,可能与您的情况有关。默认情况下,它设置为 true ,这意味着即使您的处理程序通过回调返回值,只要Node事件循环中仍然有任何内容,Lambda都会赢得

It looks like the Lambda context property callbackWaitsForEmptyEventLoop, as documented here, may be relevant to your case. By default it is set to true, which means that even if your handler returns a value via callback, as long as there is anything still in the Node event loop, the Lambda won't complete its execution.

将其设置为 false 将允许您的Lambda处理程序完成操作,即使其中的内容事件循环,但请注意,当Lambda不运行时,这些逻辑线程将冻结执行。此行为可能并不适合所有库,具体取决于它们的健壮性。

Setting it to false will allow your Lambda handler to complete even with things in the event loop, but be aware that these logical threads will freeze execution while the Lambda is not running. This behavior might not be appropriate for all libraries, depending on how robust they are.

有趣的是,旧的Lambda返回样式称为 context.succeed (而不是 callback )将不等待空的事件循环,无论该属性的设置如何。 (此要点进行了一些详细介绍。)

On an interesting side note, the old Lambda return style of calling context.succeed (rather than callback) will not wait for an empty event loop, regardless of the property's setting. (This gist goes into some extra detail.)

这篇关于在Lambda函数中关闭DAX客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆