2016-06-28 47 views
1

我在Node.js中编写了一个websocket模块。在Google云中的虚拟机上运行Debian Jessie。它通过PM2作为服务运行。 服务获取请求,发送响应并将一些数据发送到AWS SQS(查询服务)。Node.js AWS-SDK SQS内存泄漏

我对将消息发送到队列中的自定义类,它有它的内存泄漏:

var AWS = require("aws-sdk"); 
var logger = require("./Logger"); 

AWS.config.loadFromPath("/home/admin/.aws/cred.json"); 

function QueueService() { 
    this.sqs = new AWS.SQS(); 
    this.queue = "https://sqs.eu-central-1.amazonaws.com/4864251684/MyQueue"; 
} 

QueueService.prototype.sendItemToStatistics = function (message, reqJson, wsConnection, queue) { 
    try { 
     logger.log("silly", "QueueUrl", queue) 

     this.sqs.sendMessage({ 
      MessageBody: message, 
      QueueUrl: queue, 
      DelaySeconds: 0 
     }, 
     function (err, data) { 
      if (err) logger.log("error", "Error, while sending report to statistics:", err.stack) 
      else logger.log("debug", "Submitted to SQS") 
     }); 

    } catch (e) { 
     logger.log("error", "Failed to send a statistics report, stacktrace:", e.stack) 
    } 
} 

module.exports = new QueueService(); 

这是问题的一部分 - 发送项目的队列:

this.sqs.sendMessage({ 
       MessageBody: message, 
       QueueUrl: queue, 
       DelaySeconds: 0 
      }, 
      function (err, data) { 
       if (err) logger.log("error", "Error, while sending report to statistics:", err.stack) 
       else logger.log("debug", "Submitted to SQS") 
      }); 

约100 RPS时,服务RAM在4-5分钟内膨胀至约900mb。然后我杀了并重新开始这个过程以保持它的响应。 如果我对此发表评论,内存泄漏会停止,服务在相同条件下停留在60-70 MB RAM左右。

我认为SDK很好,并且我的实现出了问题。在这个问题上没有找到任何具体的信息。

任何人都遇到过这个问题?

回答

4

像往常一样,我觉得answear分钟后: 补充一点:

要求( 'HTTP')globalAgent.maxSockets = 要求( 'HTTPS')globalAgent.maxSockets = 100

在第一行,在代码的其余部分之前。这迫使节点重新使用套接字。

事实证明,maxSockets的默认值是无限的,所以它只是保持打开新的套接字,而不是重复使用旧套接字。

mhart所示here