Skip to content

Commit

Permalink
开发更新
Browse files Browse the repository at this point in the history
  • Loading branch information
aleafs committed Nov 16, 2012
1 parent e206416 commit c7ae5cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
6 changes: 6 additions & 0 deletions DESIGN.md
@@ -0,0 +1,6 @@
## 设计思路

* lib/connection.js : 维护一个连接,不自己做重连,主动close或者被动close(error引起)都emit一个close事件;
* lib/pool.js : 维护单机mysql的连接池;一个连接用来做心跳,任何原因引起的close都会尝试重连;心跳之外采用动态大小的连接池处理query;
* lib/cluster.js : 主从切换判断,机器之间的流量分配策略;

65 changes: 22 additions & 43 deletions lib/connection.js
Expand Up @@ -65,56 +65,35 @@ Connection.prototype.connect = function (options) {
* @ 连接超时
*/
var tmout = options.timeout || 100;

/**
* @ 重连延迟
*/
var delay = 20;

/**
* onconnect()
*/
/* {{{ */
var onconnect = function (e) {

_self._flag = e ? 0 : 1;
_self.emit('state', _self._flag);

if (e) {
delay = Math.min(10000, 2 * delay);
setTimeout(_connect, delay);
_self.emit('error', _self._error(e));
var timer = setTimeout(function () {
// XXX: this make "Aborted_connects" ++ in mysql server
_self._conn._socket.end();
//
}, tmout);

_self._conn = mysql.createConnection(options);
_self._conn.connect(function (error) {
clearTimeout(timer);
timer = null;

if (error) {
_self._flag = 0;
process.nextTick(function () {
_self.emit('error', _self._error(error));
});
return;
}

delay = 20;

_self._flag = 1;
_self._conn.on('error', function (error) {
if (_self._flag < 0 || !error.fatal || 'PROTOCOL_CONNECTION_LOST' !== error.code) {
if (_self._flag < 0 || !error.fatal) {
return;
}
_connect();
});
};
/* }}} */

/* {{{ */
var _connect = function () {
var timer = setTimeout(function () {
// XXX: this make "Aborted_connects" ++ in mysql server
_self._conn._socket.end();
}, tmout);

_self._conn = mysql.createConnection(options);
_self._conn.connect(function (error) {
clearTimeout(timer);
timer = null;
onconnect(error);
});
};
/* }}} */

_connect();
_self._flag = 0;
_self.emit('close', _self._error(error));
});
});

};

Expand Down
2 changes: 1 addition & 1 deletion test/connection.test.js
Expand Up @@ -22,7 +22,7 @@ var getAddress = function (config) {
describe('mysql connection', function () {

/* {{{ should_reconnect_works_fine() */
it('should_reconnect_works_fine', function (done) {
xit('should_reconnect_works_fine', function (done) {

var blocker = getBlocker(33061);

Expand Down

0 comments on commit c7ae5cd

Please sign in to comment.