request: add timeout.
This commit is contained in:
parent
b6e9019d56
commit
5519ecdfec
@ -196,6 +196,8 @@ function request(options, callback, stream) {
|
|||||||
if (res.socket)
|
if (res.socket)
|
||||||
res.socket.removeListener('end', done);
|
res.socket.removeListener('end', done);
|
||||||
|
|
||||||
|
stream.finish();
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
stream.emit('error', err);
|
stream.emit('error', err);
|
||||||
@ -315,8 +317,22 @@ function ReqStream(options) {
|
|||||||
this.type = null;
|
this.type = null;
|
||||||
this._redirects = 0;
|
this._redirects = 0;
|
||||||
this.maxRedirects = options.maxRedirects || 5;
|
this.maxRedirects = options.maxRedirects || 5;
|
||||||
|
this.timeout = options.timeout;
|
||||||
|
this._timeout = null;
|
||||||
|
|
||||||
|
this._init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReqStream.prototype._init = function _init() {
|
||||||
|
var self = this;
|
||||||
|
if (this.timeout) {
|
||||||
|
this._timeout = setTimeout(function() {
|
||||||
|
self.emit('error', new Error('Request timed out.'));
|
||||||
|
self.destroy();
|
||||||
|
}, this.timeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ReqStream.prototype.__proto__ = Stream.prototype;
|
ReqStream.prototype.__proto__ = Stream.prototype;
|
||||||
|
|
||||||
ReqStream.prototype.destroy = function destroy() {
|
ReqStream.prototype.destroy = function destroy() {
|
||||||
@ -337,6 +353,15 @@ ReqStream.prototype.destroy = function destroy() {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.finish();
|
||||||
|
};
|
||||||
|
|
||||||
|
ReqStream.prototype.finish = function finish() {
|
||||||
|
if (this._timeout != null) {
|
||||||
|
clearTimeout(this._timeout);
|
||||||
|
this._timeout = null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -1992,7 +1992,8 @@ Pool.prototype.getIP = function getIP(callback) {
|
|||||||
request({
|
request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
uri: 'http://icanhazip.com',
|
uri: 'http://icanhazip.com',
|
||||||
expect: 'text'
|
expect: 'text',
|
||||||
|
timeout: 3000
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
if (err)
|
if (err)
|
||||||
return self.getIP2(callback);
|
return self.getIP2(callback);
|
||||||
@ -2021,7 +2022,8 @@ Pool.prototype.getIP2 = function getIP2(callback) {
|
|||||||
request({
|
request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
uri: 'http://checkip.dyndns.org',
|
uri: 'http://checkip.dyndns.org',
|
||||||
expect: 'html'
|
expect: 'html',
|
||||||
|
timeout: 3000
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user