@@ -17,16 +17,27 @@ module.exports = function (req, time) {
17
17
} , delays . connect ) ;
18
18
}
19
19
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
+
20
31
// Clear the connection timeout timer once a socket is assigned to the
21
32
// request and is connected.
22
33
req . on ( 'socket' , function assign ( socket ) {
23
34
// Socket may come from Agent pool and may be already connected.
24
35
if ( ! ( socket . connecting || socket . _connecting ) ) {
25
- connect ( ) ;
36
+ clear ( ) ;
26
37
return ;
27
38
}
28
39
29
- socket . once ( 'connect' , connect ) ;
40
+ socket . once ( 'connect' , clear ) ;
30
41
} ) ;
31
42
32
43
function clear ( ) {
@@ -36,20 +47,5 @@ module.exports = function (req, time) {
36
47
}
37
48
}
38
49
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
-
54
50
return req . on ( 'error' , clear ) ;
55
51
} ;
0 commit comments