簡體   English   中英

5個請求后,node.js http請求鎖定

[英]node.js http request lock-up after 5 requests

我有一些非常簡單的代碼,如下所示:

var http = require('http');

var options = {
    hostname: 'google.com',
    port: 80,
    path: '/'
};

function makeRequest() {
    http.get(options, function(res) {
        console.log('Got response code: ', res.statusCode);
        process.nextTick(makeRequest);
    }).on('error', function(err) {
            console.error('Got error: ', err);
            throw err;
        });
}

makeRequest();

5次請求后,它將鎖定並停止工作。 樣本輸出:

Got response code:  200
Got response code:  200
Got response code:  200
Got response code:  200
Got response code:  200
Got error:  { [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }

查看hyperquest README ,它詳細說明了這里發生的情況,@ substack為什么討厭它以及hyperquest如何避免它。

默認情況下,客戶端連接是池化的,具有2分鍾的空閑超時,默認的最大池大小為5。

如果不打算重用實例化代理,則應在使用實例后調用destroy()方法,並避免在池中保持空閑連接。 例:

var req = http.request({path: "/", host: "google.com",method: "HEAD"})
req.end();
req.on("response",function(res) {
    //do something
    // ....
    req.destroy();         
    //do other things
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM