lambda容器到期时,有没有一种关闭mongo连接的好方法? [英] Is there a good way to close mongo connections when a lambda container expires?

查看:78
本文介绍了lambda容器到期时,有没有一种关闭mongo连接的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS-lambda处理程序将dynamoDB流写入mongoDB.我想在处理程序外部定义db连接,以便基于我们预期的吞吐量,请求可以重用相同的连接.如果不正确处理连接,lambda容器的问题将过期.

I am using AWS-lambda handler to write a dynamoDB stream to mongoDB. I want to define the db connection outside of the handler so that the requests reuse the same connections, based on our expected throughput volume. The problem with the lambda containers will expire without disposing of the connections properly.

有人知道这个问题有什么好的解决方案吗?本质上可以归结为我想使用连接池而不最大化连接限制"

Does anybody know of any good solutions for this problem? Essentially boils down to "I want to use connection pools without maxing out the connection limit"

推荐答案

当我遇到此问题时,我发现了两种解决方案:

When I faced with this problem, I've found out two solutions:

  1. 最佳实践"-为每个Lambda调用创建/关闭MongoDB连接.好主意,如果您知道Lambdas不会经常被调用=>您的Lambdas容器会大声喊叫.
  2. 重用连接拉-对我来说,当您知道Lambda通常会进行足够的调用以保持(可能)使容器保持温暖时,这是正常的.在这种情况下,您应该将socketTimeoutMS选项(猫鼬)设置为足以使其在Lambdas调用之间保持的状态.对我来说是:

 { 
   reconnectTries: 30, 
   reconnectInterval: 500, 
   poolSize: 1, 
   socketTimeoutMS: 2000000, 
   keepAlive: true, 
 }

(连接将在超时后自动关闭)

(connection will be closed automatically after timeout)

**仅供参考:如果您使用DynamoDB流作为lambda的触发器,则任何预热都可能不适合您.

** FYI: Any warm-up may be not suitable for you, if you use DynamoDB streams as trigger for lambda.

**我已经在这里问过类似的问题: AWS Lambda(Node.js,v.8.10)&猫鼬:MongoNetworkError与数据库的连接超时

** I've already asked similar question here: AWS Lambda (Node.js, v. 8.10) & Mongoose: MongoNetworkError connection to DB timed out

这篇关于lambda容器到期时,有没有一种关闭mongo连接的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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