node.js 0.4 发生Error: socket hang up

百度一下  发现是 系统bug ,修改lib下http.js . 参考下面:

https://github.com/joyent/node/commit/66570c196429f01786c79f177c643b07f678c32c 

I kinda solve the Socket Hang up problem.  At least ,now I don't see 
any error message. 

Here is what I've done: 

1) for http.request options, set Connection:keep-alive 
2) set Agent.maxSockets = 1024   (so more connection to play around 
with ) 
3) very critical: DO a timeout for the http.request. 

e.g. 
var responseHdr = function (clientResponse) { 
  if (clientResposne) { 

  } else { 
    clientRequest.abort(); 
 } 

 

}; 
var timeoutHdr = setTimeout(function() { 
                        clientRequest.emit('req-timeout'); 
                }, 5000); // timeout after 5 secs 

                //cfg.http_client_response_timeout 
                clientRequest.on("req-timeout", responseHdr); 

                clientRequest.on('error', function(e) { 
                        clearTimeout(timeoutHdr); 
                        console.error('Ok.. clientrequest error' + myCounter); 
                        next({err:JSON.stringify(e)}); 
                }); 

4) do the clientRequest.abort(); when timeout (see code above). 

What I found out is that, if you let the timeout socket sitting there, 
it will issue timeout hang up. 
Even you have your own timeout routine (as above) to handle timeout 
issue, the socket associated with the clientRequest is actually still 
alive.   So, in order to do that, you have to do 
clientRequest.abort()  when you manually handling timeout (as above). 

5) Better to set the timeout be some reasonable but NOT large number. 
Because, what I found out is that, even set maxSockets to 1024, if ONE 
socket is stucked, your node.js will stall and cannot connect to any 
new socket until that one is timeout or released.  (but if you have 
two node.js instance, the other one will not be affected).   So, 
better not set it to more than 10 secs.. my experience tells me. :) 

On May 26, 7:52 pm, murvinlai <murvin...@gmail.com> wrote:  

posted @ 2011-12-08 14:04  方子尚  阅读(3479)  评论(0编辑  收藏  举报