简体   繁体   English

Node.js内存泄漏?

[英]Node.js memory leaks?

I have some code below that I'm using in my Express.js app to centralize some acl logic. 我在我的Express.js应用程序中使用了一些代码来集中一些acl逻辑。 If the function returns true or false explicitly the middleware can handle the next call. 如果函数显式返回truefalse则中间件可以处理next调用。 But if it doesn't return it's up to the authorize logic to execute next() whenever it's finished doing it's thing. 但是如果它没有返回,那么只要它完成了它就会执行next()的授权逻辑。

To avoid having to write out the error data, I want to just pass in an error() function that can be called, which just calls the next function internally. 为了避免写出错误数据,我想传入一个可以调用的error()函数,它只是在内部调用next函数。

Someone told me that this can lead to some kind of memory leaks since the next function is in it's own closure and referencing it from outside. 有人告诉我,这可能导致某种内存泄漏,因为next函数在它自己的闭包中并从外部引用它。 I see similar techniques being used in a lot of examples online, but I'm still quite new to Node.js so wondering if there is any truth to this? 我看到在网上的很多例子中使用了类似的技术,但我对Node.js还是很陌生,所以想知道这是否有任何道理?

this.router.use(function (req, res, next) {
    var err = {
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        },
        error = function () {
            next(err);
        },
        authorize = app.route.authorize(req, res, next, error);

    if (authorize === false) {
        next(err);
    }
    else if (authorize === true) {
        next();
    }
});

EDIT:Remove variables 编辑:删除变量

this.router.use(function (req, res, next) {
    var authorize = app.route.authorize(req, res, next, function () {
        next({
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        });
    });

    if (authorize === false) {
        next({
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        });
    }
    else if (authorize === true) {
        next();
    }
});

When you set up the middleware, the .use() method is called there once, the anonymous handler/middleware is written to memory once, and it's that same middleware function that gets called for each new request. 当您设置中间件时, .use()方法在那里被调用一次,匿名处理程序/中间件被写入内存一次,并且它是为每个新请求调用的相同中间件函数。

The err variable is instantiated every time the middleware runs, and it's a different object. 每次中间件运行时都会实例化err变量,它是一个不同的对象。 Had you put it outside and into the closure scope of .use() , it would then be the same object. 如果你把它放在外面并进入.use()的闭包范围,它就会是同一个对象。

It is then passed to next and next is very likely another middleware function that gets instantiated once and stays the same in memory which persists and grabs onto its closure access. 它然后被传递给nextnext很可能是另一个中间件函数,它被实例化一次并在内存中保持不变,它会持续存在并抓住它的闭包访问。

But then, when the next function finishes running, the object that err points to would lose its references -- it should be garbage-collected. 但是,当下next函数完成运行时, err指向的对象将丢失其引用 - 它应该被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM