@@ -76,6 +76,15 @@ Connection.prototype.connect = function(cb) {
76
76
this . _protocol . on ( 'unhandledError' , this . _handleProtocolError . bind ( this ) ) ;
77
77
this . _protocol . on ( 'drain' , this . _handleProtocolDrain . bind ( this ) ) ;
78
78
this . _protocol . on ( 'end' , this . _handleProtocolEnd . bind ( this ) ) ;
79
+
80
+ if ( this . config . connectTimeout ) {
81
+ var handleConnectTimeout = this . _handleConnectTimeout . bind ( this ) ;
82
+
83
+ this . _socket . setTimeout ( this . config . connectTimeout , handleConnectTimeout ) ;
84
+ this . _socket . once ( 'connect' , function ( ) {
85
+ this . setTimeout ( 0 , handleConnectTimeout ) ;
86
+ } ) ;
87
+ }
79
88
}
80
89
81
90
this . _protocol . handshake ( cb ) ;
@@ -191,6 +200,20 @@ Connection.prototype.format = function(sql, values) {
191
200
return SqlString . format ( sql , values , this . config . stringifyObjects , this . config . timezone ) ;
192
201
} ;
193
202
203
+ Connection . prototype . _handleConnectTimeout = function ( ) {
204
+ if ( this . _socket ) {
205
+ this . _socket . setTimeout ( 0 ) ;
206
+ this . _socket . destroy ( ) ;
207
+ }
208
+
209
+ var err = new Error ( 'connect ETIMEDOUT' ) ;
210
+ err . errorno = 'ETIMEDOUT' ;
211
+ err . code = 'ETIMEDOUT' ;
212
+ err . syscall = 'connect' ;
213
+
214
+ this . _handleNetworkError ( err ) ;
215
+ } ;
216
+
194
217
Connection . prototype . _handleNetworkError = function ( err ) {
195
218
this . _protocol . handleNetworkError ( err ) ;
196
219
} ;
0 commit comments