Permalink
Browse files

Implement timeout handling for node 0.4.x

Since there is no way to reliably abort a request in node 0.2.x this
feature won't be ported back ...
  • Loading branch information...
1 parent fe27976 commit afa8c2f47980dc061119ed8a225c7f15200c7f0f @pfleidi committed May 28, 2011
Showing with 14 additions and 3 deletions.
  1. +14 −3 lib/wwwdude/node-versions/v0.4.x.js
@@ -23,7 +23,7 @@ exports.request = function (context) {
var url = context.url;
var headers = context.options.headers;
var payload = context.options.payload;
- var request;
+ var request, timeoutId;
var options = {
host: url.hostname,
@@ -40,19 +40,30 @@ exports.request = function (context) {
headers['Content-Length'] = Buffer.byteLength(payload);
}
+ function handlerWrapper(response) {
+ clearTimeout(timeoutId);
+ context.handler(response);
+ }
+
if (url.protocol === 'https:') {
- request = Https.request(options, context.handler);
+ request = Https.request(options, handlerWrapper);
} else {
- request = Http.request(options, context.handler);
+ request = Http.request(options, handlerWrapper);
}
request.on('error', function (err) {
+ clearTimeout(timeoutId);
context.respond('error', err);
});
if (payload) {
request.write(payload);
}
+ timeoutId = setTimeout(function () {
+ request.abort();
+ context.respond('error', new Error('HTTP Timeout was triggered!'));
+ }, context.timeout);
+
request.end();
};

0 comments on commit afa8c2f

Please sign in to comment.