node.js - 如何在 Cassandra + AWS Lambda 中处理超时

标签 node.js cassandra datastax-enterprise cassandra-driver

我有一个带有连接到 Cassandra 的 VPC 的 Lambda 函数。

我认为由于冷启动或其他问题,它根本无法连接到 Cassandra,Lambda 的超时时间为 10 秒,我也想为 Cassandra 添加超时时间,如果第一次连接未建立我将终止脚本并返回存在问题。

我正在为 Node js 使用 cassandra 驱动程序: https://github.com/datastax/nodejs-driver/

连接:

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], keyspace: 'keyspace' });

我无法使用 nodejs 的超时,然后检查连接,因为即使一切正常,Lambda 也不会在超时完成之前完成代码。

最佳答案

看起来您可以使用超时作为 Client 对象的可选参数 here

这应该是将此可选参数分配给您偏好的值的问题。您还应该在回调函数中寻找处理连接问题的方法。

const cassandra = require('cassandra-driver');
/* Documentation: github.com/datastax/nodejs-driver/blob/master/lib/client.js - line 120*/
const client = new cassandra.Client(
{ 
   contactPoints: ['127.0.0.1'],
   keyspace: 'keyspace',
   socketOptions:
   {
      connectTimeout: 2000
   }
});

创建客户端后,您应该能够在连接方法上指定(如果它不起作用)回调。

/* Documentation: github.com/datastax/nodejs-driver/blob/master/lib/client.js - line 320 */
client.connect(function (err) {
   if (err) return console.error(err); /* your attempt to connect is terminated here. */
   console.log('Connected to cluster with %d host(s): %j', 
   client.hosts.length, client.hosts.keys());
});

一旦您验证了您的 (err) 存在 - 您的连接尝试就基本终止了。您可以使用 AWS lambda 重试/终止/执行其他操作。

关于node.js - 如何在 Cassandra + AWS Lambda 中处理超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635641/

相关文章:

使用集合类型的 Cassandra 二级索引

javascript - 使用 Node.js + Express,无需模板引擎

javascript - Node 请求库 - Promise 重构

hadoop - Cassandra 从 Hadoop 写入/读取

java - 通过 Eclipse 连接到远程 Cassandra 数据库

cassandra - 为什么在Cassandra中默认值表属性dclocal_read_repair_chance = 0.1?

node.js - 在不同的 Node 进程之间进行通信的最佳方法是什么?

javascript - Grunt grunt-postcss Autoprefixer 不工作

datastax - 从 Datastax 数据中心删除种子节点

java - 如何在不停机的情况下在 Cassandra 中进行大量更新?