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

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

问题描述

我有一个 node.js 函数,我想将其部署为 AWS Lambda 函数.我注意到当我使用 redis Elasticache 时,我必须关闭我用 redis.createClient 打开的连接,否则 Lambda 函数会超时.我通过简单地调用客户端的 quit() 方法来做到这一点.如果我在发出 Lambda 回调之前执行此操作,则 Lambda 函数会按预期结束.如果我不这样做,则 Lambda 函数会在我设置的任何超时间隔处超时.在测试设置中,我有一个 Lambda 函数,它在缓存上运行并在 30 毫秒内终止.如果不调用 redis 客户端 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 context 属性 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.

有趣的是,调用 context.succeed(而不是 callback)的旧 Lambda 返回样式不会等待空事件循环,无论属性的设置.(这个要点有一些额外的细节.)

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天全站免登陆