forked from IBM/node-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPool.js
246 lines (216 loc) · 7.06 KB
/
Pool.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const async = require('async');
const binary = require('node-pre-gyp');
const path = require('path');
const { Connection } = require('./Connection');
const bindingPath = binary.find(path.resolve(path.join(__dirname, '../package.json')));
const odbc = require(bindingPath);
const INITIAL_SIZE_DEFAULT = 10;
const INCREMENT_SIZE_DEFAULT = 10;
const MAX_SIZE_DEFAULT = null;
const SHRINK_DEFAULT = true;
const CONNECTION_TIMEOUT_DEFAULT = 0;
const LOGIN_TIMEOUT_DEFAULT = 0;
class Pool {
constructor(connectionString) {
this.isOpen = false;
this.freeConnections = [];
// connectionString is a...
if (typeof connectionString === 'string') {
this.connectionString = connectionString;
this.initialSize = INITIAL_SIZE_DEFAULT;
this.incrementSize = INCREMENT_SIZE_DEFAULT;
this.maxSize = MAX_SIZE_DEFAULT;
this.shrink = SHRINK_DEFAULT;
this.connectionTimeout = CONNECTION_TIMEOUT_DEFAULT;
this.loginTimeout = LOGIN_TIMEOUT_DEFAULT;
} else if (typeof connectionString === 'object') {
const configObject = connectionString;
if (!Object.prototype.hasOwnProperty.call(configObject, 'connectionString')) {
throw new TypeError('Pool configuration object must contain "connectionString" key');
}
this.connectionString = configObject.connectionString;
this.initialSize = configObject.initialSize || INITIAL_SIZE_DEFAULT;
this.incrementSize = configObject.incrementSize || INCREMENT_SIZE_DEFAULT;
this.maxSize = configObject.maxSize || MAX_SIZE_DEFAULT;
this.shrink = configObject.shrink || SHRINK_DEFAULT;
this.connectionTimeout = configObject.connectionTimeout || CONNECTION_TIMEOUT_DEFAULT;
this.loginTimeout = configObject.loginTimeout || LOGIN_TIMEOUT_DEFAULT;
} else {
throw TypeError('Pool constructor must passed a connection string or a configuration object');
}
}
// TODO: Documentation
// TODO: Does this need to be async?
// returns a open connection, ready to use.
// should overwrite the 'close' function of the connection, and rename it is 'nativeClose', so
// that that close can still be called.
async connect(callback = undefined) {
if (this.freeConnections.length < 2) {
await this.increasePoolSize(this.incrementSize);
}
let connection;
if (this.freeConnections.length > 0) {
connection = this.freeConnections.pop();
} else {
await this.increasePoolSize(this.incrementSize);
connection = this.freeConnections.pop();
}
connection.nativeClose = connection.close;
connection.close = async (closeCallback = undefined) => {
if (typeof closeCallback === 'undefined') {
return new Promise((resolve, reject) => {
connection.nativeClose((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
this.increasePoolSize(1);
});
}
connection.nativeClose(closeCallback);
this.increasePoolSize(1);
return undefined;
};
// promise...
if (typeof callback === 'undefined') {
return new Promise((resolve, reject) => {
if (connection == null) {
reject();
} else {
resolve(connection);
}
});
}
// ...or callback
return callback(null, connection);
}
async query(sql, params, cb) {
// determine the parameters passed
let callback = cb;
let parameters = params;
if (typeof callback === 'undefined') {
if (typeof parameters === 'function') {
callback = parameters;
parameters = null;
} else if (typeof parameters === 'undefined') {
parameters = null;
} // else parameters = params, check type in this.ODBCconnection.query
}
let connection;
if (this.freeConnections.length > 0) {
connection = this.freeConnections.pop();
} else {
await this.increasePoolSize(this.incrementSize);
connection = this.freeConnections.pop();
}
if (!connection) {
return callback(Error('Could not get a connection from the pool.'));
}
// promise...
if (typeof callback !== 'function') {
return new Promise((resolve, reject) => {
connection.query(sql, parameters, (error, result) => {
// after running, close the connection whether error or not
if (error) {
reject(error);
} else {
resolve(result);
}
connection.close();
});
});
}
// ...or callback
return connection.query(sql, parameters, (error, result) => {
// after running, close the connection whether error or not
process.nextTick(() => {
callback(error, result);
});
connection.close();
});
}
// close the pool and all of the connections
async close(callback = undefined) {
const connections = [...this.freeConnections];
this.freeConnections.length = 0;
this.isOpen = false;
if (typeof callback === 'undefined') {
return new Promise((resolve, reject) => {
async.each(connections, (connection, cb) => {
connection.close((error) => {
cb(error);
});
}, (error) => {
if (error) {
reject(error);
} else {
resolve(error);
}
});
});
}
async.each(this.freeConnections, (connection, cb) => {
connection.close((error) => {
cb(error);
});
}, error => callback(error));
}
async init(callback = undefined) {
if (!this.isOpen) {
this.isOpen = true;
// promise...
if (typeof callback === 'undefined') {
return new Promise(async (resolve, reject) => {
try {
await this.increasePoolSize(this.initialSize);
resolve(null);
} catch (error) {
reject(error);
}
});
}
// ...or callback
try {
await this.increasePoolSize(this.initialSize);
} catch (error) {
return callback(error);
}
return callback(null);
}
console.log('.init() was called, but the Pool was already initialized.');
return undefined;
}
// odbc.connect runs on an AsyncWorker, so this is truly non-blocking
async increasePoolSize(count) {
const connectionConfig = {
connectionString: this.connectionString,
connectionTimeout: this.connectionTimeout,
loginTimeout: this.loginTimeout,
};
return new Promise((resolve, reject) => {
const connectArray = [];
for (let i = 0; i < count; i += 1) {
connectArray.push((callback) => {
odbc.connect(connectionConfig, (error, connection) => {
if (!error) {
if (this.isOpen) {
this.freeConnections.push(new Connection(connection));
}
}
callback(error, connection);
});
});
}
async.race(connectArray, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
}
module.exports.Pool = Pool;