Node.js AWS-SDK SQS内存泄漏

我在Node.js中写了一个websocket模块 在Google云中的虚拟机上运行Debian Jessie。 它通过PM2作为服务运行。 该服务获取请求,发送响应并将一些数据发送到AWS 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的情况下,服务内存在4-5分钟内膨胀到900mb左右。 然后我杀了并重新开始这个过程以保持它的响应。 如果我对此发表评论,内存泄漏将停止,服务在相同条件下停留在60-70 MB RAM左右。

我认为SDK是好的,我的执行有问题。 在这个问题上没有find任何特定的信息。

任何人经历过这个

像往常一样,我发现后面的answear分钟:添加这个:

require('http')。globalAgent.maxSockets = require('https')。globalAgent.maxSockets = 100

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

原来,maxSockets的默认值是无限的,所以它只是保持打开新的套接字,而不是重用旧套接字。

正如mhart 在这里所build议的mhart

Interesting Posts