From 21053fd3bf1218b5ded2fc9ce9a6e508689f8c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 15 Mar 2018 11:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=A7=BB=E9=99=A4=E9=85=8D=E7=BD=AE=E5=92=8C?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 5 +++++ index.js | 5 ++++- lib/method.js | 12 ++++++------ package.json | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/History.md b/History.md index d0e1556..88495a7 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +2.2.0 / 2018-03-15 +================== + * 连接池增加失败移除配置和恢复时间 + + 2.1.3 / 2018-03-14 ================== * 优化异常输出 diff --git a/index.js b/index.js index fe27909..fce04eb 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,10 @@ class Mysqli { //是否有从库 this.useSlaveDB = config.length > 1 - this.pool = mysql.createPoolCluster() + this.pool = mysql.createPoolCluster({ + removeNodeErrorCount: 1, // 连续失败立即从节点中移除, 并在10秒后尝试恢复 + restoreNodeTimeout: 10000 + }) config.forEach((item, i) => { let { host, port, user, charset, passwd: password, db: database } = item diff --git a/lib/method.js b/lib/method.js index 2bcfc10..5b5ec7e 100644 --- a/lib/method.js +++ b/lib/method.js @@ -23,23 +23,23 @@ class Method { } connect() { - this.defer = Promise.defer() + const defer = Promise.defer() this.pool.getConnection(this.slave, (err, conn) => { if (err) { - return this.defer.reject(new SqlErr(`MySQL connect ${err}`)) + return defer.reject(new SqlErr(`MySQL connect ${err}`)) } if (this.db) { conn.query('USE ' + this.db, err => { if (err) { - return this.defer.reject(new SqlErr('Select DB ' + err)) + return defer.reject(new SqlErr('Select DB ' + err)) } - this.defer.resolve(conn) + defer.resolve(conn) }) } else { - this.defer.resolve(conn) + defer.resolve(conn) } }) - return this.defer.promise + return defer.promise } listDb() { diff --git a/package.json b/package.json index 6891276..32216ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mysqli", - "version": "2.1.3", + "version": "2.2.0", "description": "MySQL tool", "main": "index.js", "dependencies": {