WebSocket 未打开:readyState 2 (CLOSING)

WebSocket is not open: readyState 2 (CLOSING)

冷启动时,我的 Lambda 函数在尝试初始化与 Neptune 的连接时会 return 此错误。之后,连接成功,没有错误。如果 Lambda 再次变冷,错误 returns.

    const dc = new DriverRemoteConnection(
      `wss://${process.env.NEPTUNE_ENDPOINT}:${process.env.NEPTUNE_PORT}/gremlin`,
      {}
    );
    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    g.V();
    dc.close();

使用 gremlin@^3.4.6 和节点 12.x。我发现针对 ws 包报告了一个类似的错误,该错误被作为实现错误排除。 https://github.com/websockets/ws/issues/1410

使用 Gremlin 时是否需要以某种方式提前验证连接?

编辑:这个问题似乎是从 gremlin@3.4.6 开始的。如果我降级到 gremlin@3.4.5,问题就会消失。

编辑 2:3.4.5 再次出现错误。 3.4.4 似乎更好。

您应该使用 terminal step 并等待要解决的承诺。

const gremlin = require('gremlin');
const { traversal } = gremlin.process.AnonymousTraversalSource;
const { DriverRemoteConnection } = gremlin.driver;

const rc = new DriverRemoteConnection(
  `wss://${process.env.NEPTUNE_ENDPOINT}:${process.env.NEPTUNE_PORT}/gremlin`)
const g = traversal().withRemote(rc);

async function run() {
  // await on Promise<Array>
  const result = await g.V().toList();
  console.log(result);
}

run();

这里有一些关于如何开始使用 JavaScript Gremlin 变体的代码示例:http://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript

使用 AWS Lambda 时,您不应在每次调用时关闭与数据库的连接,否则连续调用将失败。

DriverRemoteConnection.close() 是异步操作,所以你必须使用 await。类似于:

const dc = new DriverRemoteConnection(
  `wss://${process.env.NEPTUNE_ENDPOINT}:${process.env.NEPTUNE_PORT}/gremlin`,
  {}
);
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
await g.V().limit(1).next();
await dc.close();

这帮助我解决了同样的错误