Comparing version 0.2.4 to 0.2.5
@@ -10,2 +10,4 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var noop = function () {}; | ||
/** | ||
@@ -19,18 +21,22 @@ * @ Connection | ||
/** | ||
* 0 : 未连接 | ||
* 1 : 正在连接 | ||
* 2 : 连接成功 | ||
* 1 : 正常使用 | ||
* -1: 准备断开 | ||
*/ | ||
this._flag = 0; | ||
options.port = options.port || 3306; | ||
this._name = util.format('%s@%s:%d', options.user, options.host, options.port); | ||
this._conn = mysql.createConnection(options); | ||
this._flag = 1; | ||
this._fatalError = null; | ||
this._socketTimeout = isNaN(+options.sockettimeout) ? 60000 /* 1 min */ : options.sockettimeout; | ||
var _self = this; | ||
this._conn.on('error', function (e) { | ||
if (e && e.fatal && _self._flag > -1) { | ||
_self.close(); | ||
e = _self._error(e); | ||
if (e.fatal) { | ||
_self._fatalError = e; | ||
} | ||
_self.emit('error', _self._error(e)); | ||
_self.emit('error', e); | ||
}); | ||
@@ -40,2 +46,6 @@ }; | ||
Connection.prototype.connected = function () { | ||
return this._flag > 0; | ||
}; | ||
Connection.prototype._error = function (name, msg) { | ||
@@ -55,3 +65,2 @@ var e; | ||
Connection.prototype.close = function () { | ||
if (this._flag < 0) { | ||
@@ -62,17 +71,41 @@ return; | ||
this._flag = -1; | ||
this._conn.end(); | ||
var _self = this; | ||
var timer = setTimeout(function () { | ||
_self._conn.destroy(); | ||
}, 10); | ||
this._conn.end(function () { | ||
clearTimeout(timer); | ||
}); | ||
}; | ||
Connection.prototype.query = function (sql, timeout, callback) { | ||
var _self = this; | ||
if ((typeof sql) === 'object' && sql.params) { | ||
sql = this.format(sql.sql, sql.params); | ||
sql = _self.format(sql.sql, sql.params); | ||
} | ||
var _self = this; | ||
if (!timeout || timeout < 1) { | ||
return this._conn.query(sql, function (e, r) { | ||
if (e && e.fatal && _self._flag > -1) { | ||
_self.emit('error', e); | ||
} | ||
if (_self._fatalError) { | ||
return process.nextTick(function () { | ||
callback(_self._fatalError); | ||
}); | ||
} | ||
_self._conn._implyConnect(); | ||
_self._conn._socket.setTimeout(_self._socketTimeout); | ||
_self._conn._socket.removeAllListeners('timeout'); | ||
_self._conn._socket.once('timeout', function () { | ||
var e = _self._error('SocketTimeout', 'Mysql socket timeout after ' + _self._socketTimeout + ' ms'); | ||
e.fatal = true; | ||
callback(e); | ||
callback = noop; | ||
}); | ||
if (!timeout || isNaN(+timeout) || timeout < 1) { | ||
return _self._conn.query(sql, function (e, r) { | ||
callback(e ? _self._error(e) : null, r); | ||
callback = noop; | ||
}); | ||
@@ -82,5 +115,6 @@ } | ||
var timer = setTimeout(function () { | ||
callback(_self._error('QueryTimeout', 'Mysql query timeout after ' + timeout + ' ms')); | ||
_self.emit('timeout', sql); | ||
callback = function () {}; | ||
var e = _self._error('QueryTimeout', 'Mysql query timeout after ' + timeout + ' ms'); | ||
e.fatal = true; | ||
callback(e); | ||
callback = noop; | ||
}, timeout); | ||
@@ -92,6 +126,3 @@ | ||
callback(e ? _self._error(e) : null, r); | ||
if (e && e.fatal && _self._flag > -1) { | ||
_self.emit('error', e); | ||
} | ||
callback = noop; | ||
}); | ||
@@ -98,0 +129,0 @@ }; |
@@ -116,2 +116,3 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
} | ||
_wakeup(o); | ||
}; | ||
@@ -125,4 +126,3 @@ /* }}} */ | ||
if (e && e.fatal) { | ||
_remove(c, o); | ||
return; | ||
return _remove(c, o); | ||
} | ||
@@ -145,2 +145,3 @@ | ||
var _wakeup = function (o) { | ||
var m = Math.min(_options.maxconnections, 1 + _options.maxconnections + o._stack.length - conns.length); | ||
@@ -159,11 +160,15 @@ while (m && o._queue.size()) { | ||
if (!c) { | ||
if (!c || !c.connected()) { | ||
c = Connection.create(config); | ||
conns.push(c); | ||
['error', 'timeout'].forEach(function (k) { | ||
['error'].forEach(function (k) { | ||
c.once(k, function (e) { | ||
o.emit('error', e); | ||
_remove(c, o); | ||
}); | ||
}); | ||
conns.push(c); | ||
} | ||
execute(c, o, s); | ||
@@ -170,0 +175,0 @@ m--; |
{ | ||
"name": "easymysql", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"author": "Aleafs Zhang (zhangxc83@gmail.com)", | ||
@@ -10,3 +10,3 @@ "contributors": [], | ||
"dependencies": { | ||
"mysql-robin" : "=2.0.7", | ||
"mysql-robin" : "=2.0.8", | ||
"safequeue" : ">=0.0.2" | ||
@@ -13,0 +13,0 @@ }, |
@@ -152,2 +152,3 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var _me = Cluster.create({'maxconnections' : 1}); | ||
_me.on('error', function (e) {}); | ||
_me.addserver(_config); | ||
@@ -163,6 +164,5 @@ | ||
function afterBlock() { | ||
_me.query('SHOW DATABASES', 50, function (e, r) { | ||
_me.query('SHOW DATABASES', 10, function (e, r) { | ||
should.exist(e); | ||
e.name.should.eql('QueryTimeout'); | ||
blocker.open(); | ||
@@ -174,3 +174,3 @@ setTimeout(afterOpen, 20); | ||
function afterOpen() { | ||
_me.query('SHOW DATABASES', 200, function (e, r) { | ||
_me.query('SHOW DATABASES', 100, function (e, r) { | ||
should.not.exist(e); | ||
@@ -177,0 +177,0 @@ done(); |
@@ -57,6 +57,9 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
this._name = 'test'; | ||
this._flag = 1; | ||
var _self = this; | ||
}; | ||
util.inherits(Connection, Emitter); | ||
Connection.prototype.connect = function () { | ||
Connection.prototype.connected = function () { | ||
return this._flag > 0; | ||
}; | ||
@@ -68,2 +71,6 @@ | ||
Connection.prototype._setFlag = function (i) { | ||
this._flag = i; | ||
} | ||
Connection.prototype.query = function (sql, tmout, callback) { | ||
@@ -123,4 +130,16 @@ var n = __queries.push(sql); | ||
_me.__setFlag = function (i, flag) { | ||
var c = __Objects[i]; | ||
if (!(c instanceof Connection)) { | ||
return; | ||
} | ||
c._setFlag(flag); | ||
}; | ||
_me.__connectionNum = function () { | ||
return __Objects.length; | ||
}; | ||
return _me; | ||
}; | ||
@@ -47,12 +47,13 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
r.should.includeEql({'SLEEP(0.01)' : 0}); | ||
var now2 = Date.now(); | ||
_me.query('SELECT SLEEP(0.02)', 15, function (e, r) { | ||
e.should.have.property('name', 'QueryTimeout'); | ||
e.message.should.include(getAddress(Common.config)); | ||
(Date.now() - now2).should.below(20); | ||
_me.close(); | ||
done(); | ||
}); | ||
}); | ||
var now = Date.now(); | ||
_me.query('SELECT SLEEP(0.02)', 15, function (e, r) { | ||
e.should.have.property('name', 'QueryTimeout'); | ||
e.message.should.include(getAddress(Common.config)); | ||
(Date.now() - now).should.below(20); | ||
_me.close(); | ||
done(); | ||
}); | ||
}); | ||
@@ -64,3 +65,2 @@ | ||
var _me = Connection.create(config); | ||
var now = Date.now(); | ||
@@ -118,3 +118,3 @@ _me.query('SELECT SLEEP(0.01)', 0, function (e, r) { | ||
should.ok(error); | ||
error.should.have.property('code', 'PROTOCOL_ENQUEUE_AFTER_QUIT'); | ||
error.should.have.property('code', 'PROTOCOL_CONNECTION_LOST'); | ||
error.message.should.include(getAddress(_config)); | ||
@@ -141,3 +141,56 @@ _me.close(); | ||
it('should_connection_connected_api_works_fine', function (done) { | ||
var config = Common.extend(); | ||
var _me = Connection.create(config); | ||
should.ok(_me.connected()); | ||
_me.close(); | ||
_me.connected().should.be.false; | ||
done(); | ||
}); | ||
it('should_connected_api_works_fine_when_server_blocked', function (done) { | ||
var blocker = getBlocker(33063, function () { | ||
var _config = Common.extend({ | ||
'host' : 'localhost', 'port' : 33063 | ||
}); | ||
blocker.block(); | ||
var afterBlock = function () { | ||
var _me = Connection.create(_config); | ||
_me.query('SHOW DATABASES', 0, function (e, r) { | ||
should.exist(e); | ||
e.code.should.eql('ECONNREFUSED'); | ||
should.ok(e.fatal); | ||
}); | ||
_me.query('SHOW DATABASES', 100, function (e, r) { | ||
should.exist(e); | ||
e.code.should.eql('ECONNREFUSED'); | ||
should.ok(e.fatal); | ||
_me.close(); | ||
blocker.close(); | ||
done(); | ||
}); | ||
}; | ||
setTimeout(afterBlock, 10); | ||
}); | ||
}); | ||
it('should_socket_timeout_works_fine', function (done) { | ||
var _config = Common.extend({ | ||
sockettimeout : 20 | ||
}); | ||
var _me = Connection.create(_config); | ||
_me.query('SELECT SLEEP(1)', 'none', function (err, res) { | ||
should.exist(err); | ||
should.ok(err.fatal); | ||
// error is caused by socket timeout | ||
err.name.should.eql('SocketTimeout'); | ||
_me.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -30,3 +30,3 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var _me = Pool.create({ | ||
'maxconnections' : 4, | ||
'maxconnections' : 4, | ||
}); | ||
@@ -41,3 +41,2 @@ | ||
}); | ||
var num = 9; | ||
@@ -152,3 +151,60 @@ for (var i = 0; i < num; i++) { | ||
it('should_get_fatal_error_works_fine', function (done) { | ||
var _me = Pool.create({'maxconnections' : 1}); | ||
_me.query('SELECT fatal', 20, function (e, r) { | ||
should.exist(e); | ||
should.ok(e.fatal); | ||
}); | ||
_me.query('SHOW Variables like "READ_ONLY"', 0, function (e, r) { | ||
should.not.exist(e); | ||
done(); | ||
}); | ||
}); | ||
it('should_connecton_random_emit_error_works_fine', function (done) { | ||
var _me = Pool.create({'maxconnections' : 1}); | ||
_me.on('error', function (e) { | ||
should.exist(e); | ||
e.message.should.eql('myError'); | ||
}); | ||
_me.query('SHOW Variables like "READ_ONLY"', 20, function (e, r) { | ||
should.not.exist(e); | ||
}); | ||
var e = new Error('myError'); | ||
e.fatal = 1; | ||
Connection.__emitEvent(1, 'error', e); | ||
_me.query('SHOW Variables like "READ_ONLY"', 0, function (e, r) { | ||
should.not.exist(e); | ||
done(); | ||
}); | ||
}); | ||
it('when_all_connection_flag_is_unuseable_pool_works_fine', function (done) { | ||
var _me = Pool.create({'maxconnections' : 2}); | ||
var _r = []; | ||
for (var i = 0; i < 5; i ++) { | ||
_me.query('SHOW Variables like "READ_ONLY"', 10, function (e, r) { | ||
_r.push(e || r); | ||
}); | ||
} | ||
var conns = Connection.__connectionNum(); | ||
setTimeout(function () { | ||
[1, 2].forEach(function (i) { | ||
Connection.__setFlag(i, -1); | ||
}); | ||
}, 5); | ||
setTimeout(function () { | ||
_me.query('SHOW Variables like "READ_ONLY"', 10, function (e, r) { | ||
should.not.exist(e); | ||
done(); | ||
}); | ||
}, 10); | ||
}); | ||
}); | ||
37939
1176
Updatedmysql-robin@=2.0.8