在公司使用node.js的时候出现了一个怪异的问题,服务器打包超过2min以后,node.js的代码会执行两次,而且在客户端还收不到返回值。
百度这个问题,查了两天都没有很好的结果。最后只能翻墙,求助google,果然Google第一个结果就让我很好的解决了问题,早知道就google了。为照顾墙内人民,我觉得还是应该把原文贴出来。
其实解决这个问题非常简单,只要加上一行代码:
res.connection.setTimeout(0);希望对大家有所帮助
原文如下:
Preventing server timeout in node.js
.entry-meta
.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 suspicioussetTimeout( 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 myserver 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 the0.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 threadhttp://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.js0.6.8+ 0.8.x, setTimeout isstill part ofnet,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 itisnow 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.
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。