danwrong/restler — pull request #130 by bosky101 (2013-09-04 15:28:37+00:00)
Title: timeout event, and timeout option implemented. added docs & example

I was serially testing GET's on a million websites, and gradually came across my test app stalling with the same pattern.

It turned out that restler has/had no option for timeout. This is a show-stopper for large/production scenarios.

eg: The following code, stalls forever, without throwing an error or

rest.get('http://tianji.com').on('complete', function(){
console.log('never entered here');
});

infact it did not emit any known events either. namely error or abort.

rest.get('http://tianji.com').on('error' function(ms){
sys.puts('neither found an error');
}).on('abort' function(ms){
sys.puts('nor an abort');
}).on('complete',function(data,response){
sys.puts('never reached here either');
});

I also saw previous mentions requesting timeout feature for restler.
eg: https://github.com/danwrong/restler/issues/3 one of the oldest open bugs/issues.

So i went ahead, forked and fixed this.

My commit includes :
- added the 'timeout' event
- added the {timeout:Ms} option
- added documentation for timeout
- added an example for timeout

Here's an example.

rest.get('http://tianji.com',{timeout: 10000}).on('timeout' function(ms){
sys.puts('did not return within '+ms+' ms');
}).on('complete',function(data,response){
sys.puts('life goes on');
});

Will love to see this reviewed for a possible merge.

~Bosky @bhaskerkode