多个请求后,NodeJS无限期挂起 [英] NodeJS hangs indefinitely after multiple requests

查看:275
本文介绍了多个请求后,NodeJS无限期挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到postgres数据库(node-postgres模块v2.1.0)的nodeJS(v.0.10.23)代理,以及返回各种json数据的pgpool-II。

I have a nodeJS(v.0.10.23) proxy connecting to a postgres db (node-postgres module v2.1.0), along with pgpool-II which returns all sorts of json data.

在过去,这是处理连接错误的方式:

Back in the day, this is how connection errors were handled:

var after = function(callback) {
    return function(err, queryResult) {
        if(err) {
            response.writeHead(500, _header);
            console.log("ERROR: 500");
            console.log(err);
            return response.end(JSON.stringify({error: err}));
        }
        callback(queryResult)
    }
};

基本上,如果没有错误,它将消耗响应。

Basically what it does, is consuming the response if no errors are present.

可以在这里找到详细的解释:节点js-连接池的http.request()问题

An in depth explanation can be found here: Node js - http.request() problems with connection pooling

使用上述功能,我得到了类似的东西:

Using the function above, i got something like this:

pg.connect(_conString, after(function(err, client, done) {
  client.query(sql, after(function(result) {
  ...
  done();
} 

由于将函数传递到after()的回调中时上下文丢失,因此我失去了使用pg.connect()传递的固有done()方法的能力。

Since the context is lost when the function is passed into after()s callback, i'm loosing the ability to use the innate done() method passed by pg.connect().

删除后缀即可解决问题,但是随后,在适当的时间,并且有大量客户端在拉取数据,节点将挂起,直到重置为止。

Removing the after solves the issue, but then, in due time and with a fair amount of clients pulling the data, the node will hang until it is reset.

有没有其他的骗局总结各种异步响应?

Is there a different way of consuming various asynchronous responses?

还是一种将pg.connect上下文传递给回调的方法?

or perhaps a way to pass the pg.connect context into the callback?

推荐答案

当然,您正在丢失 done(),您永远不会在 after()函数中向回调函数传递第三个参数。

Well, of course you're losing done(), you never pass a third argument to your callback in your after() function.

function after(cb) {
    return function() {
        // you're using this function in a context where the arguments
        // passed in could be anything. the only constant is that the first
        // argument is the error, if any
        if (arguments[0]) {
            response.writeHead(500, _header);
            console.log("ERROR: 500");
            console.log(err);
            return response.end(JSON.stringify({error: arguments[0]}));
        }
        // apply the entire argument list to the callback, without modification
        cb.apply(cb, arguments);
    };
}

...这也解决了传递 queryResult 变量> client 。

... this also fixes the dubious convention of passing client through the queryResult variable.

这篇关于多个请求后,NodeJS无限期挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆