Node.js 在调用AWS lambda处理程序的回调时遇到Neptune Gremlin连接问题
我正在使用gremlin@3.3.5适用于带有AWS Lambdas的Node.js 8.10应用程序。这个过程在一次调用中运行良好。这是我的示例代码Node.js 在调用AWS lambda处理程序的回调时遇到Neptune Gremlin连接问题,node.js,amazon-web-services,gremlin,aws-step-functions,amazon-neptune,Node.js,Amazon Web Services,Gremlin,Aws Step Functions,Amazon Neptune,我正在使用gremlin@3.3.5适用于带有AWS Lambdas的Node.js 8.10应用程序。这个过程在一次调用中运行良好。这是我的示例代码 const gremlin = require('gremlin'); const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection; const Graph = gremlin.structure.Graph; exports.handler = (event, co
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;
}
}
当我为单次调用运行此过程时,它似乎运行得很好,但当我尝试运行批处理操作(大约100000个请求/小时)时,我在CloudWatch日志度量中发现我的连接未成功关闭。我已经尝试了很多实现,比如callbackhaitforeventloopempty,但是它抓住了lambda。当我删除回调(或类似地返回)时,这个过程也可以很好地处理批处理操作。但是我确实希望从这个lambda返回数据,并将信息传递给我的step函数,以根据该信息触发另一个lambda。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,您不需要使用
回调
在做了一些研究之后,我发现问题在于gremlin包处理关闭连接事件的方式不支持无服务器体系结构。当触发驱动程序时。关闭()。当驱动程序被实例化时,它会创建客户机实例,客户机本身会创建连接实例,连接实例使用ws-library创建websocket实例。现在,ws.close()事件优雅地关闭所有事件,在调用回调之前不会等待调用事件,并且该事件保持打开和泄漏。因此,在显式调用连接实例上的dc.\u client.\u connection.ws.terminate()之后,然后dc.close()立即关闭连接。返回结果
工作,而回调(null,result)
不工作?不,两者都不工作。我相信它们也会做同样的事情。但是当我既不返回也不回调时,连接就会关闭。我也在使用async await。这个问题仍然存在。即使我已经在包dc中记录了连接变量。_client._connection.isOpen在连接打开时说true,然后在连接关闭时说false。您没有处理承诺,至少在您向我们显示的代码中没有。抱歉。我刚刚编辑了我的问题。这个问题仍然存在。您似乎没有在函数中添加“async”,这是打字错误吗?