【问题标题】:How to handle timeouts in Cassandra + AWS Lambda如何在 Cassandra + AWS Lambda 中处理超时
【发布时间】:2019-05-07 05:16:17
【问题描述】:

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

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

我正在为节点 js 使用 cassandra-driver: 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 在超时完成之前不会完成代码,即使一切正常。

【问题讨论】:

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


    【解决方案1】:

    您似乎可以使用超时作为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 重新尝试/杀死/执行其他操作。

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 2019-12-07
      • 2016-09-04
      • 1970-01-01
      • 2019-04-03
      • 2021-06-08
      • 2020-06-14
      • 2023-01-02
      相关资源
      最近更新 更多