Skip to content

Commit 42f6998

Browse files
committed
simplify code (move out socket timeout part)
1 parent e216d30 commit 42f6998

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

index.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@ module.exports = function (req, time) {
1717
}, delays.connect);
1818
}
1919

20+
if (delays.socket !== undefined) {
21+
// Abort the request if there is no activity on the socket for more
22+
// than `delays.socket` milliseconds.
23+
req.setTimeout(delays.socket, function socketTimeoutHandler() {
24+
req.abort();
25+
var e = new Error('Socket timed out on request' + host);
26+
e.code = 'ESOCKETTIMEDOUT';
27+
req.emit('error', e);
28+
});
29+
}
30+
2031
// Clear the connection timeout timer once a socket is assigned to the
2132
// request and is connected.
2233
req.on('socket', function assign(socket) {
2334
// Socket may come from Agent pool and may be already connected.
2435
if (!(socket.connecting || socket._connecting)) {
25-
connect();
36+
clear();
2637
return;
2738
}
2839

29-
socket.once('connect', connect);
40+
socket.once('connect', clear);
3041
});
3142

3243
function clear() {
@@ -36,20 +47,5 @@ module.exports = function (req, time) {
3647
}
3748
}
3849

39-
function connect() {
40-
clear();
41-
42-
if (delays.socket !== undefined) {
43-
// Abort the request if there is no activity on the socket for more
44-
// than `delays.socket` milliseconds.
45-
req.setTimeout(delays.socket, function socketTimeoutHandler() {
46-
req.abort();
47-
var e = new Error('Socket timed out on request' + host);
48-
e.code = 'ESOCKETTIMEDOUT';
49-
req.emit('error', e);
50-
});
51-
}
52-
}
53-
5450
return req.on('error', clear);
5551
};

0 commit comments

Comments
 (0)