我有一个带有连接到 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/