Node.js 服务器连接超时2min限制问题解决

在公司使用node.js的时候出现了一个怪异的问题,服务器打包超过2min以后,node.js的代码会执行两次,而且在客户端还收不到返回值。

百度这个问题,查了两天都没有很好的结果。最后只能翻墙,求助google,果然Google第一个结果就让我很好的解决了问题,早知道就google了。为照顾墙内人民,我觉得还是应该把原文贴出来。

其实解决这个问题非常简单,只要加上一行代码:

res.connection.setTimeout(0);希望对大家有所帮助

原文如下:

Preventing server timeout in node.js

 

.entry-meta

11

.entry-header

Update 2015-10-09.  Not sure whypeople keep hitting this, but they do.  Apparently node.js docs don’t do agood job explaining that there is a two minute time limit hard coded inthere?  I updated the link below to the latest master branch.

Update 2013-10-08. This is an old post butcontinues to get page views, so clearly it is still a problem. The feature isnow documented (see link below) and this post is still correct.

Original post, 2011-03-30

This is something I spent an hour or sotrying to track down today, so I thought I’d write it up in the hopes thatsomeone else is spared the trouble.

First of all, I have both web client andserver written in node.js. My server is designed such that it first checks forcached versions of URLs, and if the file doesn’t exist, then it hits thedatabase and creates the file. This second step can take a long time, and so Iwanted to write a utility script that I could trigger manually to update thecache of files whenever the database changes.

So I wrote the script using javascript andnode, but was getting a strange error in which the client would die if theserver took longer than two minutes to complete the request. No amount ofabusing the code on the client would change this, even though the node.jssource code seemed to indicate that no timeout was ever being set on the clientsocket, and most questions on the internet were about how to limit the timeout,not set it to forever.

Turns out the suspicious setTimeout( 2 * 60 * 1000 ) in http.js at line 986 was indeedthe culprit. I originally ignored that line, as it was only setting the timeoutfor the server-side socket. But then, after editing that line in the code andrecompiling (grasping at straws), re-running the client using the recompilednode and still getting exactly 2 minutes for the socket to die, it suddenly hitme that my server was timing out, not my client!

So with a single undocumented call insideof the handler in question, I had no more troubles:

1

2

res.writeHead(200, { 'Content-Type': 'application/json' });

res.connection.setTimeout(0); // this could take a while

Note the second line above. While the 0.4.4 API docs don’t statethis fact, the http response object exposes the socket as the connection object(I found this on the mailing list in this thread http://groups.google.com/group/nodejs/browse_thread/thread/376d600fb87f498a).So res.connection gives a hook to the Socket’s setTimeout function, and settingthat to zero drops the default 2 minute limit.

November 2012 I’m still doing this innode.js 0.6.8+ 0.8.x, setTimeout isstill part of net,and the http server is still by default using a 2 minute timeout. And github isstill awesome.

August 2014 update. Yes, still there: 2 minute timeout.Really this isn’t a bug that needs fixing, because who wants a server to goaway for two minutes in these days of attention deficit disorder web surfing. ButI wish it was documented in the docs. Apparently this behavior will changesoon: https://github.com/joyent/node/issues/4704

And with the release of 0.10.x, but it isnow documented. See server settimeout and response settimeout.

When I modify my own code to use 0.10.x, Iwill put up a new post.
No actually, apparently I never got around to putting up a new post onusing the now documented timeout functions.

October 2015. Still there, but now you can findthe offending hard-coded two minutes in the node source code here.The link above is still reasonably accurate pointer into the documentation ofthis feature and how to control it.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值