From 02ed26fd12e49b7a7015e3d8bf420ea06c05bcc1 Mon Sep 17 00:00:00 2001 From: crystalik Date: Sun, 19 Jan 2014 21:38:09 +0700 Subject: [PATCH 1/2] added timeout for connection added timeout for connection --- lib/Connection.js | 7 +++++++ 1 file changed, 7 insertions(+) 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); From 1281a9a149785130b06fbfa5ca6d21027d24693b Mon Sep 17 00:00:00 2001 From: crystalik Date: Sun, 19 Jan 2014 21:39:42 +0700 Subject: [PATCH 2/2] timeout option in config --- lib/ConnectionConfig.js | 1 + 1 file changed, 1 insertion(+) 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;