/
pools.js
130 lines (116 loc) · 3.67 KB
/
pools.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
var $npm = {
ConnectionParameters: require('pg/lib/connection-parameters'),
events: require('./events'),
utils: require('./utils')
};
var pools = {}; // global list of pools
function acquirePool(ctx, pg, db) {
var p, cp = new $npm.ConnectionParameters(ctx.cn),
ps = ctx.options.poolStrategy;
if ($npm.utils.isNull(ps)) {
ps = 'server'; // default pool strategy
}
var poolName = 'global';
switch (ps) {
case 'single':
// maintain a single pool regardless of the connection parameters
break;
case 'machine':
// create a separate pool for each machine hosting a database server
poolName = cp.host;
break;
case 'server':
// create a separate pool for each virtual server (machine + port)
poolName = cp.host + '/' + cp.port;
break;
case 'database':
// create a separate pool for every database being accessed
poolName = cp.host + '/' + cp.port + '/' + cp.database;
break;
case 'user':
// create a separate pool for each database user
poolName = cp.host + '/' + cp.port + '/' + cp.database + '/' + cp.user;
break;
default:
throw new TypeError('Invalid value for \'poolStrategy\' specified: ' + JSON.stringify(ps));
}
if (poolName in pools) {
p = pools[poolName];
db.$pool = p.pool;
p.dbs.push(db);
return p.pool;
}
p = new pg.Pool(ctx.cn);
p.on('error', onError);
pools[poolName] = {
pool: p, dbs: [db]
};
return p;
}
function shutDown() {
for (var p in pools) {
pools[p].dbs.forEach(db => {
db.$pool = null;
});
pools[p].pool.removeListener('error', onError);
pools[p].pool.end(); // Mr. Dead Pool :)
}
pools = {};
}
function onError(err) {
var ctx = err.client.$ctx;
$npm.events.error(ctx.options, err, {
cn: $npm.utils.getSafeConnection(ctx.cn),
dc: ctx.dc
});
}
// This is for tests and diagnostics:
function getAllPools() {
var res = {};
for (var p in pools) {
res[p] = pools[p].pool;
}
return res;
}
module.exports = {
acquirePool,
getAllPools,
shutDown
};
/**
* @typedef PoolStrategy
* @description
* ** This is a $[6.x] feature. **
*
* This type defines string values supported by property `poolStrategy` (see {@link module:pg-promise Initialization Options}).
*
* You can override the default (`server`) value with one of these, to select a different
* strategy for allocating new connection pools (see $[pg-pool]).
*
* @property {string} single
* Maintains a single pool regardless of the connection parameters.
*
* This singleton pattern makes sure that only one connection pool is used throughout the process.
*
* @property {string} machine
* Creates a separate pool for each machine hosting a database server.
*
* A new pool is created for each unique `host` parameter in the connection.
*
* @property {string} server
* **DEFAULT:** Creates a separate pool for each virtual server (machine + port).
*
* Each unique pair of `host` + `port` in the connection is allocated a separate connection pool.
*
* @property {string} database
* Creates a separate pool for every database being accessed.
*
* Each unique combination of `host` + `port` + `database` in the connection is allocated a separate connection pool.
*
* @property {string} user
* Creates a separate pool for each database user.
*
* Each unique combination of `host` + `port` + `database` + `user` in the connection is allocated a separate connection pool.
*
*/