【问题标题】:Experience Neptune Gremlin connections problem on calling AWS lambda handlers` callback在调用 AWS lambda handlers` 回调时遇到 Neptune Gremlin 连接问题
【发布时间】:2019-02-07 00:06:47
【问题描述】:

我将 gremlin@3.3.5 用于带有 AWS Lambda 的 Node.js 8.10 应用程序。对于单次调用,该过程一切正常。这是我的示例代码。

const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;

exports.handler = (event, context, callback) => {
    dc = new DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin');

    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    try {
        const result = await g.V().limit(1).count().next();
        dc.close();
        callback(null, { result: result });
    } catch (exception) {
        callback('Error');
        throw error;
    }
}

当我为单次调用运行此进程时,它似乎一切正常,但是当我尝试运行批处理操作(例如 100,000 个请求/小时)时,我在 CloudWatch 日志指标中遇到了我的连接没有成功关闭。我已经尝试了一些这样的实现,比如 callbackWaitForEventLoopEmpty,但它抓住了 lambda。当我删除回调(或类似地返回)时,这个过程也适用于批处理操作。但我确实想从这个 lambda 返回数据,其中包含传递给我的 step 函数的信息,以根据该信息触发另一个 lambda。

【问题讨论】:

  • return result 有效,callback(null, result) 无效?
  • 不,两者都没有。我相信他们会做同样的事情。
  • 但是当我既不返回也不做回调时,连接就会关闭。

标签: node.js amazon-web-services gremlin aws-step-functions amazon-neptune


【解决方案1】:

在做了一些研究之后,我发现问题在于 gremlin 包如何处理关闭连接的事件不利于无服务器架构。当触发 driver.close() 时。当驱动程序被实例化时,它会创建客户端实例,它在自身内部创建连接实例,它使用 ws 库创建 websocket 实例。现在 ws.close() 事件优雅地关闭了所有事件,它不会在我的回调被调用之前等待事件被调用并且该事件保持打开并泄漏。因此,在连接实例上显式调用 dc._client._connection.ws.terminate() 然后 dc.close() 立即关闭连接。

【讨论】:

    【解决方案2】:

    g.V().limit(1).count().next() 是异步的。

    试试这个:

    exports.handler = async (event) => {
    
      try {
          dc = new DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin');    
          const graph = new Graph();
          const g = graph.traversal().withRemote(dc);
          const result = await g.V().limit(1).count().next();
          dc.close();
          return result;
      } catch (error) {
          throw error;
      }
    }
    

    由于您的 Lambda 运行时是 Node.js 8.10,您不需要使用 callback

    【讨论】:

    • 我也在使用异步等待。这个问题依然存在。
    • 即使我已经记录了连接变量,里面的包 dc._client._connection.isOpen 表示连接打开时为真,连接关闭时为假。
    • 你没有处理承诺,至少在你展示给我们的代码中没有。
    • 道歉。我刚刚编辑了我的问题。这个问题依然存在。
    • 你好像没有在函数中添加‘async’,是错字吗?
    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 2019-08-10
    • 2019-03-08
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多