diff --git a/lib/Connection.js b/lib/Connection.js index b2b6a5f2c..1edb779fe 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -66,11 +66,14 @@ Connection.prototype.connect = function(cb) { // Node v0.10+ Switch socket into "old mode" (Streams2) this._socket.on("data",function() {}); + + this._socket.setTimeout(this.config.timeout); this._socket.pipe(this._protocol); this._protocol.pipe(this._socket); this._socket.on('error', this._handleNetworkError.bind(this)); + this._socket.on('timeout', this._handleTimeoutError.bind(this)); this._socket.on('connect', this._handleProtocolConnect.bind(this)); this._protocol.on('handshake', this._handleProtocolHandshake.bind(this)); this._protocol.on('unhandledError', this._handleProtocolError.bind(this)); @@ -195,6 +198,10 @@ Connection.prototype._handleNetworkError = function(err) { this._protocol.handleNetworkError(err); }; +Connection.prototype._handleTimeoutError = function() { + this._protocol.handleNetworkError(new Error('timeout')); +}; + Connection.prototype._handleProtocolError = function(err) { this.state = "protocol_error"; this.emit('error', err); diff --git a/lib/ConnectionConfig.js b/lib/ConnectionConfig.js index d6e0a5168..a89433494 100644 --- a/lib/ConnectionConfig.js +++ b/lib/ConnectionConfig.js @@ -10,6 +10,7 @@ function ConnectionConfig(options) { this.host = options.host || 'localhost'; this.port = options.port || 3306; + this.timeout = options.timeout || 3000; this.localAddress = options.localAddress; this.socketPath = options.socketPath; this.user = options.user || undefined;