【问题标题】:NODE.JS App gets stuck getting 2.5 million of records through and apiNODE.JS 应用程序在通过 api 获取 250 万条记录时卡住了
【发布时间】:2019-03-18 21:41:05
【问题描述】:

我的代码可以从数据库中检索数百万行,并通过 api 检索数百万行。

            let closedOrdersStartDate;
            preparedOrdersPromise = tickApiConnector.obtainToken().then(function () {
                return ordersController.getAllTrades(0, [], 0, accountIdsToRun);
            }).then(function (trades) {
                closedOrdersStartDate = new Date();
                return Promise.all([trades, fthsApiConnector.getData('closed_orders', '&sort=id', 10000, 0)]);
            }).then(function (tradesAndClosedOrderIds) {
                //stuck before getting there
                console.log('now processing orders time spent from starting getting closed_orders till now is: ' +
                  ((new Date().getTime() - closedOrdersStartDate.getTime())/ 1000) + ' seconds');
                return ordersController.processOrders(tradesAndClosedOrderIds[0], tradesAndClosedOrderIds[1]);
            });

应用在调用 getData() 函数后卡住了。

getData: async function (entityName, getParams = '', perRequest = 10000, skip = 0) {
  if(getParams.indexOf('&take=') !== -1) {
    return fthsApiRequest(entityName, getParams);
  }

  const totalCount = await fthsApiRequest(entityName, getParams + '&getCount');

  let result = [];
  let count = 0;
  while(count < totalCount) {
    result = result.concat(await fthsApiRequest(entityName, getParams + '&take=' + perRequest + '&skip=' + count));
    count += perRequest;
  }

  return result;
}

该函数一直执行到最后一个请求(我在日志中看到它),然后脚本变得不负责任。我认为这可能是内存泄漏,我已经以不同的方式重写了 getData() 函数,但服务器上仍然有足够的内存,脚本甚至不会消耗一点内存.在 getData() 的最后一次迭代被咆哮之后,我仍然会在一段时间内获得 100% 的 CPU 负载。之后,应用程序就永远卡住了。

我已尝试对其进行分析。并且有数千个未知代码的代码移动事件:0x2a5c24bfb4c0,我不确定这意味着什么,但可能有线索。这是V8.log

【问题讨论】:

    标签: javascript node.js memory-leaks profiling cpu


    【解决方案1】:

    可能的问题可能在块中:

    while(count < totalCount) { result = result.concat(await fthsApiRequest(entityName, getParams + '&take=' + perRequest + '&skip=' + count)); count += perRequest; }
    

    确保 api 给出响应。最后一句话:

    return result;
    

    在异步函数中更好用:

    return Promise.resolve(result);
    

    【讨论】:

    • Api 给出响应。正如我所说的,所有数据都已加载,但加载后会卡住。至于返回 Promise.resolve(result);我会试试看,但为什么你认为它应该有帮助?
    猜你喜欢
    • 2015-12-14
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多