node.js:这个长轮询实现是否泄漏:

plicqrtu  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(375)

我正在评估node.js是否可以替换java web应用程序上当前的推送功能。我编写了一个简单的长轮询服务器,它就像客户端和java后端之间的中介。客户端请求订阅,然后java服务器通过调用node.js通知订阅的客户端。到目前为止,它似乎运行良好,但我收到以下消息,指出内存泄漏:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added.
Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:168:15)
    at EventEmitter.once (events.js:189:8)
    at route (C:\Users\Juan Pablo\pushserver.js:42:12)
    at Server.onRequest (C:\Users\Juan Pablo\pushserver.js:32:3)
    at Server.EventEmitter.emit (events.js:91:17)
    at HTTPParser.parser.onIncoming (http.js:1793:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23
)
    at Socket.socket.ondata (http.js:1690:22)
    at TCP.onread (net.js:402:27)

每当发出notify事件时,我有一行代码记录现有的侦听器。我已经让它运行了一段时间,它显示每个订阅的客户机只有一个侦听器(应该是这样),但是当我收到警告消息时,代码中没有这一行。代码是完全一样的,除了那一行。
这是推送服务器的代码(由于我还在学习node.js,所以有点初级):

var http = require('http');
var url = require("url");
var qs = require("querystring");
var events = require('events');
var util = require('util');

var emitter = new events.EventEmitter;

function onRequest(request, response)
{
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    request.setEncoding("utf8");

    if (request.method == 'POST')
    {
        var postData = "";
        request.addListener("data", function(postDataChunk)
        {
            postData += postDataChunk;
            console.log("Received POST data chunk '"+ postDataChunk + "'.");
        });

        request.addListener("end", function()
        {
            route(pathname, response, postData);
        });
    }
    else if (request.method=='GET')
    {
        var urlParts = url.parse(request.url, true);
        route(pathname, response, urlParts.query);
    }
}

function route(pathname, response, data)
{
    switch (pathname)
    {
        case "/subscription":

            emitter.once("event:notify", function(ids)
            {
                response.writeHead(200, {"Content-Type": "text/html", "Access-Control-Allow-Origin": "*"});
                response.write(JSON.stringify(ids));
                response.end();
            });

            break;

        case "/notification":

            //show how many listeners exist
            console.log(util.inspect(emitter.listeners('event:notify'));

            emitter.emit("event:notify", data.ids);

            response.writeHead(200, {"Content-Type": "text/html", "Access-Control-Allow-Origin": "*"});
            response.write(JSON.stringify(true));
            response.end();
            break;

        default:

            console.log("No request handler found for " + pathname);
            response.writeHead(404, {"Content-Type": "text/plain", "Access-Control-Allow-Origin": "*"});
            response.write("404 - Not found");
            response.end();
            break;
    }
}

http.createServer(onRequest).listen(8888);

console.log('Server running at http://127.0.0.1:8888/');

我的印象是使用emitter.once会在事件侦听器被使用后自动删除它,所以我不知道如果只有一个客户端连接,怎么会添加11个侦听器。我在想,如果客户端在等待通知时断开连接,那么关联的连接资源就不会被释放。
我想知道我是否必须手动处理断开连接,以及是否真的有泄漏。欢迎任何建议。谢谢。

h22fl7wq

h22fl7wq1#

如果有人感兴趣,上面的代码确实会泄漏。当客户端在发送通知之前断开连接时,就会发生泄漏。要解决此问题,必须在客户端突然断开连接时删除事件侦听器,例如:

case "/subscription":

        var notify = function(ids)
        {
            response.writeHead(200, {"Content-Type": "text/html", "Access-Control-Allow-Origin": "*"});
            response.write(JSON.stringify(ids));
            response.end();
        }

        emitter.once("event:notify", notify);

        //event will be removed when connection is closed
        request.on("close", function()
        {
            emitter.removeListener("event:notify", notify);
        });
        break;

相关问题