-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.js
58 lines (48 loc) · 1.69 KB
/
mysql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// MySQL connection pool
var mysql = require('mysql'),
cfg = require('./cfg'),
logger = require('./logger'),
ctr = 0,
connTimeout = 1000,
pool = mysql.createPool({
connectionLimit: cfg['mysql.pool.connlimit'],
host: cfg['mysql.db.host'],
user: cfg['mysql.db.user'],
password: cfg['mysql.db.password'],
database: cfg['mysql.db.name'],
socketPath: cfg['mysql.sock']
}),
// To keep a track of opened connections and their Ids, we should track all connection requests
getConn = pool.getConnection.bind(pool);
pool.getConnection = function (cb) {
// Record this stack point, in case not released
var thisConnErr = new Error();
// Get a connection the standard way
getConn(function (err, conn) {
var release;
if (err) {
return cb(err);
}
conn.szStack = thisConnErr;
conn.szWarned = false;
conn.szInitTime = new Date();
clearTimeout(conn.szTimer);
conn.szTimer = setTimeout(function () {
conn.szWarned = true;
logger.warn('mysql-pool', 'Conn', conn.szId, 'not released after', connTimeout, 'ms', conn.szStack.stack);
}, connTimeout);
if (!conn.szId) {
conn.szId = ++ctr;
release = conn.release.bind(conn);
conn.release = function () {
clearTimeout(conn.szTimer);
if (conn.szWarned) {
logger.info('mysql-pool', 'Conn', conn.szId, 'was finally released after', new Date() - conn.szInitTime, 'ms');
}
release();
};
}
cb(null, conn);
});
};
module.exports = pool;