2016-06-28 85 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