@juit/pgproxy-pool 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/connection.cjs +309 -0
- package/dist/connection.cjs.map +6 -0
- package/dist/connection.d.ts +112 -0
- package/dist/connection.mjs +273 -0
- package/dist/connection.mjs.map +6 -0
- package/dist/events.cjs +56 -0
- package/dist/events.cjs.map +6 -0
- package/dist/events.d.ts +16 -0
- package/dist/events.mjs +31 -0
- package/dist/events.mjs.map +6 -0
- package/dist/index.cjs +34 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.mjs +8 -0
- package/dist/index.mjs.map +6 -0
- package/dist/libpq.cjs +32 -0
- package/dist/libpq.cjs.map +6 -0
- package/dist/libpq.d.ts +6 -0
- package/dist/libpq.mjs +7 -0
- package/dist/libpq.mjs.map +6 -0
- package/dist/pool.cjs +486 -0
- package/dist/pool.cjs.map +6 -0
- package/dist/pool.d.ts +159 -0
- package/dist/pool.mjs +451 -0
- package/dist/pool.mjs.map +6 -0
- package/dist/queue.cjs +70 -0
- package/dist/queue.cjs.map +6 -0
- package/dist/queue.d.ts +7 -0
- package/dist/queue.mjs +45 -0
- package/dist/queue.mjs.map +6 -0
- package/package.json +47 -0
- package/src/connection.ts +494 -0
- package/src/events.ts +42 -0
- package/src/index.ts +17 -0
- package/src/libpq.ts +10 -0
- package/src/pool.ts +683 -0
- package/src/queue.ts +47 -0
package/dist/pool.mjs
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// pool.ts
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { Connection, convertOptions } from "./connection.mjs";
|
|
4
|
+
import { Emitter } from "./events.mjs";
|
|
5
|
+
function parseEnvNumber(variable, defaultValue) {
|
|
6
|
+
const string = process.env[variable];
|
|
7
|
+
if (string == null)
|
|
8
|
+
return defaultValue;
|
|
9
|
+
const value = parseFloat(string);
|
|
10
|
+
if (isNaN(value))
|
|
11
|
+
throw new Error(`Invalid value "${string}" for environment variable "${variable}"`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function parseEnvBoolean(variable, defaultValue) {
|
|
15
|
+
const string = process.env[variable];
|
|
16
|
+
if (string == null)
|
|
17
|
+
return defaultValue;
|
|
18
|
+
const value = string.toLowerCase();
|
|
19
|
+
if (value === "false")
|
|
20
|
+
return false;
|
|
21
|
+
if (value === "true")
|
|
22
|
+
return true;
|
|
23
|
+
throw new Error(`Invalid value "${string}" for environment variable "${variable}"`);
|
|
24
|
+
}
|
|
25
|
+
var ConnectionRequest = class {
|
|
26
|
+
_resolve;
|
|
27
|
+
_reject;
|
|
28
|
+
_promise;
|
|
29
|
+
_timeout;
|
|
30
|
+
_pending = true;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new {@link ConnectionRequest} with a timeout, after which the
|
|
33
|
+
* request will be automatically rejected.
|
|
34
|
+
*/
|
|
35
|
+
constructor(timeout) {
|
|
36
|
+
this._promise = new Promise((resolve, reject) => {
|
|
37
|
+
this._resolve = resolve;
|
|
38
|
+
this._reject = reject;
|
|
39
|
+
});
|
|
40
|
+
this._timeout = setTimeout(() => {
|
|
41
|
+
this.reject(new Error(`Timeout of ${timeout} ms reached acquiring connection`));
|
|
42
|
+
}, timeout).unref();
|
|
43
|
+
}
|
|
44
|
+
/** Return the {@link Promise} to the {@link Connection} */
|
|
45
|
+
get promise() {
|
|
46
|
+
return this._promise;
|
|
47
|
+
}
|
|
48
|
+
/** Determine whether this request is still pending or not */
|
|
49
|
+
get pending() {
|
|
50
|
+
return this._pending;
|
|
51
|
+
}
|
|
52
|
+
/** Resolve this instance's {@link Promise} with a {@link Connection} */
|
|
53
|
+
resolve(connection) {
|
|
54
|
+
clearTimeout(this._timeout);
|
|
55
|
+
if (this._pending)
|
|
56
|
+
this._resolve(connection);
|
|
57
|
+
this._pending = false;
|
|
58
|
+
}
|
|
59
|
+
/** Reject this instance's {@link Promise} with an {@link Error} */
|
|
60
|
+
reject(error) {
|
|
61
|
+
clearTimeout(this._timeout);
|
|
62
|
+
if (this._pending)
|
|
63
|
+
this._reject(error);
|
|
64
|
+
this._pending = false;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var ConnectionPool = class extends Emitter {
|
|
68
|
+
/** Borrowed connections mapped to their borrow timeout */
|
|
69
|
+
_borrowed = /* @__PURE__ */ new Map();
|
|
70
|
+
/** Array of all _available_ connections (that is, not borrowed out) */
|
|
71
|
+
_available = [];
|
|
72
|
+
/** Array of all pending {@link ConnectionRequest}s */
|
|
73
|
+
_pending = [];
|
|
74
|
+
/** All connections mapped to their evictor callback handler */
|
|
75
|
+
_connections = /* @__PURE__ */ new Map();
|
|
76
|
+
/** A {@link WeakMap} of connections already evicted by this pool */
|
|
77
|
+
_evicted = /* @__PURE__ */ new WeakSet();
|
|
78
|
+
/** The minimum number of connections to keep in the pool */
|
|
79
|
+
_minimumPoolSize;
|
|
80
|
+
/** The maximum number of connections to keep in the pool */
|
|
81
|
+
_maximumPoolSize;
|
|
82
|
+
/** The maximum number of idle connections that can be sitting in the pool */
|
|
83
|
+
_maximumIdleConnections;
|
|
84
|
+
/** The number of *milliseconds* after which an `acquire()` call will fail */
|
|
85
|
+
_acquireTimeoutMs;
|
|
86
|
+
/** The maximum number of *milliseconds* a connection can be borrowed for */
|
|
87
|
+
_borrowTimeoutMs;
|
|
88
|
+
/** The number of *milliseconds* to wait after the creation of a connection failed */
|
|
89
|
+
_retryIntervalMs;
|
|
90
|
+
/** Whether to validate connections on borrow or not */
|
|
91
|
+
_validateOnBorrow;
|
|
92
|
+
/** The {@link ConnectionOptions} converted into a string for `LibPQ` */
|
|
93
|
+
_connectionOptions;
|
|
94
|
+
/** Indicator on whether this {@link ConnectionPool} was started or not */
|
|
95
|
+
_started = false;
|
|
96
|
+
/** Indicator on whether this {@link ConnectionPool} is starting or not */
|
|
97
|
+
_starting = false;
|
|
98
|
+
constructor(logger, options = {}) {
|
|
99
|
+
super(logger);
|
|
100
|
+
const {
|
|
101
|
+
minimumPoolSize = parseEnvNumber("PGPOOLMINSIZE", 0),
|
|
102
|
+
maximumPoolSize = parseEnvNumber("PGPOOLMAXSIZE", minimumPoolSize + 20),
|
|
103
|
+
maximumIdleConnections = parseEnvNumber("PGPOOLIDLECONN", (maximumPoolSize + minimumPoolSize) / 2),
|
|
104
|
+
acquireTimeout = parseEnvNumber("PGPOOLACQUIRETIMEOUT", 30),
|
|
105
|
+
borrowTimeout = parseEnvNumber("PGPOOLBORROWTIMEOUT", 120),
|
|
106
|
+
retryInterval = parseEnvNumber("PGPOOLRETRYINTERVAL", 5),
|
|
107
|
+
validateOnBorrow = parseEnvBoolean("PGPOOLVALIDATEONBORROW", true),
|
|
108
|
+
...connectionOptions
|
|
109
|
+
} = options;
|
|
110
|
+
this._minimumPoolSize = Math.round(minimumPoolSize);
|
|
111
|
+
this._maximumPoolSize = Math.round(maximumPoolSize);
|
|
112
|
+
this._maximumIdleConnections = Math.ceil(maximumIdleConnections);
|
|
113
|
+
this._acquireTimeoutMs = Math.round(acquireTimeout * 1e3);
|
|
114
|
+
this._borrowTimeoutMs = Math.round(borrowTimeout * 1e3);
|
|
115
|
+
this._retryIntervalMs = Math.round(retryInterval * 1e3);
|
|
116
|
+
this._validateOnBorrow = validateOnBorrow;
|
|
117
|
+
assert(this._minimumPoolSize >= 0, `Invalid minimum pool size: ${this._minimumPoolSize}`);
|
|
118
|
+
assert(this._maximumPoolSize >= 1, `Invalid maximum pool size: ${this._maximumPoolSize}`);
|
|
119
|
+
assert(this._maximumIdleConnections >= 0, `Invalid maximum idle connections: ${this._maximumIdleConnections}`);
|
|
120
|
+
assert(this._acquireTimeoutMs > 0, `Invalid acquire timeout: ${this._acquireTimeoutMs} ms`);
|
|
121
|
+
assert(this._borrowTimeoutMs > 0, `Invalid borrow timeout: ${this._borrowTimeoutMs} ms`);
|
|
122
|
+
assert(this._retryIntervalMs > 0, `Invalid retry interval: ${this._retryIntervalMs} ms`);
|
|
123
|
+
assert(
|
|
124
|
+
this._minimumPoolSize <= this._maximumPoolSize,
|
|
125
|
+
`The minimum pool size ${this._minimumPoolSize} must less or equal to the maximum pool size ${this._maximumPoolSize}`
|
|
126
|
+
);
|
|
127
|
+
assert(
|
|
128
|
+
this._minimumPoolSize <= this._maximumIdleConnections,
|
|
129
|
+
`The minimum pool size ${this._minimumPoolSize} must less or equal to the maximum number of idle connections ${this._maximumIdleConnections}`
|
|
130
|
+
);
|
|
131
|
+
assert(
|
|
132
|
+
this._maximumIdleConnections <= this._maximumPoolSize,
|
|
133
|
+
`The maximum number of idle connections ${this._maximumIdleConnections} must less or equal to the maximum pool size ${this._maximumPoolSize}`
|
|
134
|
+
);
|
|
135
|
+
this._connectionOptions = convertOptions(connectionOptions);
|
|
136
|
+
this._logger = logger;
|
|
137
|
+
}
|
|
138
|
+
/** Statistical informations about a {@link ConnectionPool} */
|
|
139
|
+
get stats() {
|
|
140
|
+
const available = this._available.length;
|
|
141
|
+
const borrowed = this._borrowed.size;
|
|
142
|
+
const total = this._connections.size;
|
|
143
|
+
const connecting = total - (available + borrowed);
|
|
144
|
+
return { available, borrowed, connecting, total };
|
|
145
|
+
}
|
|
146
|
+
/** Returns a flag indicating whether this pool is running or not */
|
|
147
|
+
get running() {
|
|
148
|
+
return this._started || this._starting;
|
|
149
|
+
}
|
|
150
|
+
/** Returns the running configuration of this instance */
|
|
151
|
+
get configuration() {
|
|
152
|
+
return {
|
|
153
|
+
minimumPoolSize: this._minimumPoolSize,
|
|
154
|
+
maximumPoolSize: this._maximumPoolSize,
|
|
155
|
+
maximumIdleConnections: this._maximumIdleConnections,
|
|
156
|
+
acquireTimeout: this._acquireTimeoutMs / 1e3,
|
|
157
|
+
borrowTimeout: this._borrowTimeoutMs / 1e3,
|
|
158
|
+
retryInterval: this._retryIntervalMs / 1e3,
|
|
159
|
+
validateOnBorrow: this._validateOnBorrow
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/* ===== CONNECTION MANAGEMENT ============================================ */
|
|
163
|
+
/* These methods are protected, as they can be overridden to provide more
|
|
164
|
+
* specialized versions of connections */
|
|
165
|
+
/** Create a connection */
|
|
166
|
+
_create(logger, params) {
|
|
167
|
+
return new Connection(logger, params);
|
|
168
|
+
}
|
|
169
|
+
/** Validate a connection by issuing a super-simple statement */
|
|
170
|
+
async _validate(connection) {
|
|
171
|
+
if (!connection.connected)
|
|
172
|
+
return false;
|
|
173
|
+
if (!this._validateOnBorrow)
|
|
174
|
+
return true;
|
|
175
|
+
const start = process.hrtime.bigint();
|
|
176
|
+
try {
|
|
177
|
+
this._logger.debug(`Validating connection "${connection.id}"`);
|
|
178
|
+
const result = await connection.query("SELECT now()");
|
|
179
|
+
return result.rowCount === 1;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
this._logger.error(`Error validating connection "${connection.id}":`, error);
|
|
182
|
+
return false;
|
|
183
|
+
} finally {
|
|
184
|
+
const time = process.hrtime.bigint() - start;
|
|
185
|
+
const ms = Math.floor(Number(time) / 1e4) / 100;
|
|
186
|
+
this._logger.debug(`Connection "${connection.id}" validated in ${ms} ms`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Recycle a connection rolling back any running transaction */
|
|
190
|
+
async _recycle(connection) {
|
|
191
|
+
if (!connection.connected)
|
|
192
|
+
return false;
|
|
193
|
+
try {
|
|
194
|
+
const result = await connection.query("SELECT pg_current_xact_id_if_assigned() IS NOT NULL");
|
|
195
|
+
if (result.rows[0]?.[0] === "t") {
|
|
196
|
+
this._logger.warn(`Rolling back transaction recycling connection "${connection.id}"`);
|
|
197
|
+
await connection.query("ROLLBACK");
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
this._logger.error(`Error recycling connection "${connection.id}":`, error);
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/* ===== CONNECTION / POOL INTERACTION ==================================== */
|
|
206
|
+
/** Adopt a connection tying it to our events */
|
|
207
|
+
_adopt(connection) {
|
|
208
|
+
const destroyer = () => {
|
|
209
|
+
connection.off("destroyed", evictor);
|
|
210
|
+
this._evict(connection);
|
|
211
|
+
};
|
|
212
|
+
this.once("stopped", destroyer);
|
|
213
|
+
const evictor = (forced) => {
|
|
214
|
+
this.off("stopped", destroyer);
|
|
215
|
+
if (forced)
|
|
216
|
+
return;
|
|
217
|
+
this._evict(connection);
|
|
218
|
+
this._runCreateLoop();
|
|
219
|
+
};
|
|
220
|
+
connection.once("destroyed", evictor);
|
|
221
|
+
this._connections.set(connection, evictor);
|
|
222
|
+
return connection;
|
|
223
|
+
}
|
|
224
|
+
/** Destroy a connection, it will be wiped from this pool */
|
|
225
|
+
_evict(connection, aborted = false) {
|
|
226
|
+
const evictor = this._connections.get(connection);
|
|
227
|
+
if (!evictor) {
|
|
228
|
+
this._logger.warn(`Attempting to evict non adopted connection ${connection.id}`);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this._connections.delete(connection);
|
|
232
|
+
connection.off("destroyed", evictor);
|
|
233
|
+
evictor(true);
|
|
234
|
+
try {
|
|
235
|
+
this._logger.debug(`Destroying connection "${connection.id}"`);
|
|
236
|
+
clearTimeout(this._borrowed.get(connection));
|
|
237
|
+
this._borrowed.delete(connection);
|
|
238
|
+
const index = this._available.indexOf(connection);
|
|
239
|
+
if (index >= 0)
|
|
240
|
+
this._available.splice(index);
|
|
241
|
+
connection.destroy();
|
|
242
|
+
this._emit(aborted ? "connection_aborted" : "connection_destroyed", connection);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
this._logger.error(`Error destroying connection "${connection.id}"`);
|
|
245
|
+
} finally {
|
|
246
|
+
this._evicted.add(connection);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/* ===== RUN LOOPS ======================================================== */
|
|
250
|
+
/**
|
|
251
|
+
* Run the create connection loop.
|
|
252
|
+
*
|
|
253
|
+
* This loop simply creates connections, connects them, sets up the various
|
|
254
|
+
* event handler (on disconnect) and simply adds them to the available array.
|
|
255
|
+
*/
|
|
256
|
+
_runCreateLoop() {
|
|
257
|
+
if (!this._started)
|
|
258
|
+
return;
|
|
259
|
+
Promise.resolve().then(async () => {
|
|
260
|
+
while (this._started) {
|
|
261
|
+
const connections = this._connections.size;
|
|
262
|
+
const available = this._available.length;
|
|
263
|
+
const pending = this._pending.length;
|
|
264
|
+
if (available && connections >= this._minimumPoolSize || // enough available for minimum pool size
|
|
265
|
+
!pending && available >= this._maximumIdleConnections || // enough maximum idle connections
|
|
266
|
+
connections >= this._maximumPoolSize) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
let connection;
|
|
270
|
+
try {
|
|
271
|
+
connection = this._create(this._logger, this._connectionOptions);
|
|
272
|
+
this._adopt(connection);
|
|
273
|
+
} catch (error) {
|
|
274
|
+
const retry = `retrying in ${this._retryIntervalMs} ms`;
|
|
275
|
+
this._logger.error(`Error creating pooled connection, ${retry}:`, error);
|
|
276
|
+
await new Promise((resolve) => setTimeout(resolve, this._retryIntervalMs));
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
await connection.connect();
|
|
281
|
+
} catch (error) {
|
|
282
|
+
const retry = `retrying in ${this._retryIntervalMs} ms`;
|
|
283
|
+
this._logger.error(`Error connecting "${connection.id}", ${retry}:`, error);
|
|
284
|
+
this._evict(connection, true);
|
|
285
|
+
await new Promise((resolve) => setTimeout(resolve, this._retryIntervalMs));
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (this._started)
|
|
289
|
+
this._available.push(connection);
|
|
290
|
+
else
|
|
291
|
+
this._evict(connection, true);
|
|
292
|
+
this._runBorrowLoop();
|
|
293
|
+
this._emit("connection_created", connection);
|
|
294
|
+
}
|
|
295
|
+
}).catch(
|
|
296
|
+
/* coverage ignore next */
|
|
297
|
+
(error) => {
|
|
298
|
+
const retry = `retrying in ${this._retryIntervalMs} ms`;
|
|
299
|
+
this._logger.error(`Error in create loop, ${retry}:`, error);
|
|
300
|
+
setTimeout(() => this._runCreateLoop(), this._retryIntervalMs);
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Run the borrow connection loop.
|
|
306
|
+
*
|
|
307
|
+
* This loop looks at all the pending connection requests, and fullfills them
|
|
308
|
+
* with a connection from the available array. If no connections are available
|
|
309
|
+
* then it simply triggers the create loop.
|
|
310
|
+
*/
|
|
311
|
+
_runBorrowLoop() {
|
|
312
|
+
if (!this._started)
|
|
313
|
+
return;
|
|
314
|
+
Promise.resolve().then(async () => {
|
|
315
|
+
let request;
|
|
316
|
+
while (this._started && (request = this._pending.splice(0, 1)[0])) {
|
|
317
|
+
const connection = this._available.splice(0, 1)[0];
|
|
318
|
+
if (!connection) {
|
|
319
|
+
if (request.pending)
|
|
320
|
+
this._pending.unshift(request);
|
|
321
|
+
return this._runCreateLoop();
|
|
322
|
+
}
|
|
323
|
+
if (!request.pending) {
|
|
324
|
+
if (this._available.length >= this._maximumIdleConnections) {
|
|
325
|
+
this._evict(connection);
|
|
326
|
+
} else {
|
|
327
|
+
this._available.push(connection);
|
|
328
|
+
}
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const valid = await this._validate(connection);
|
|
332
|
+
if (!this._started) {
|
|
333
|
+
request.reject(new Error(`Pool stopped while validatin connection ${connection.id}`));
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (!valid) {
|
|
337
|
+
if (request.pending)
|
|
338
|
+
this._pending.unshift(request);
|
|
339
|
+
this._evict(connection);
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (!request.pending) {
|
|
343
|
+
this.release(connection);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const timeout = setTimeout(() => {
|
|
347
|
+
this._logger.error(`Connection "${connection.id}" borrowed for too long`);
|
|
348
|
+
this._evict(connection);
|
|
349
|
+
}, this._borrowTimeoutMs).unref();
|
|
350
|
+
this._borrowed.set(connection, timeout);
|
|
351
|
+
this._emit("connection_acquired", connection);
|
|
352
|
+
request.resolve(connection);
|
|
353
|
+
}
|
|
354
|
+
}).catch(
|
|
355
|
+
/* coverage ignore next */
|
|
356
|
+
(error) => {
|
|
357
|
+
const retry = `retrying in ${this._retryIntervalMs} ms`;
|
|
358
|
+
this._logger.error(`Error in borrow loop, ${retry}:`, error);
|
|
359
|
+
setTimeout(() => this._runBorrowLoop(), this._retryIntervalMs);
|
|
360
|
+
}
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
/* ===== CONNECTION LIFECYCLE ============================================= */
|
|
364
|
+
/** Acquire a {@link Connection} from this {@link ConnectionPool} */
|
|
365
|
+
acquire() {
|
|
366
|
+
assert(this._started, "Connection pool not started");
|
|
367
|
+
const deferred = new ConnectionRequest(this._acquireTimeoutMs);
|
|
368
|
+
this._pending.push(deferred);
|
|
369
|
+
this._runBorrowLoop();
|
|
370
|
+
return deferred.promise;
|
|
371
|
+
}
|
|
372
|
+
/** Release a {@link Connection} back to this {@link ConnectionPool} */
|
|
373
|
+
release(connection) {
|
|
374
|
+
if (this._evicted.has(connection))
|
|
375
|
+
return;
|
|
376
|
+
assert(this._connections.has(connection), `Connection "${connection.id}" not owned by this pool`);
|
|
377
|
+
Promise.resolve().then(async () => {
|
|
378
|
+
this._logger.debug(`Releasing connection "${connection.id}"`);
|
|
379
|
+
clearTimeout(this._borrowed.get(connection));
|
|
380
|
+
if (!connection.connected) {
|
|
381
|
+
this._logger.info(`Disconnected connection "${connection.id}" discarded`);
|
|
382
|
+
this._evict(connection);
|
|
383
|
+
} else if (this._available.length >= this._maximumIdleConnections) {
|
|
384
|
+
this._logger.info(`Extra connection "${connection.id}" discarded`);
|
|
385
|
+
this._evict(connection);
|
|
386
|
+
} else if (!await this._recycle(connection)) {
|
|
387
|
+
this._logger.info(`Non-validated connection "${connection.id}" discarded`);
|
|
388
|
+
this._evict(connection);
|
|
389
|
+
} else {
|
|
390
|
+
this._logger.debug(`Connection "${connection.id}" released`);
|
|
391
|
+
this._borrowed.delete(connection);
|
|
392
|
+
this._available.push(connection);
|
|
393
|
+
this._emit("connection_released", connection);
|
|
394
|
+
}
|
|
395
|
+
}).catch((error) => {
|
|
396
|
+
this._logger.error(`Error releasing connection "${connection.id}":`, error);
|
|
397
|
+
this._evict(connection);
|
|
398
|
+
}).finally(() => this._runBorrowLoop());
|
|
399
|
+
}
|
|
400
|
+
/* ===== POOL LIFECYCLE =================================================== */
|
|
401
|
+
/** Start this {@link ConnectionPool} validating an initial connection */
|
|
402
|
+
async start() {
|
|
403
|
+
if (this._started || this._starting)
|
|
404
|
+
return this;
|
|
405
|
+
this._logger.debug("Starting connection pool");
|
|
406
|
+
this._starting = true;
|
|
407
|
+
try {
|
|
408
|
+
const connection = this._create(this._logger, this._connectionOptions);
|
|
409
|
+
await connection.connect();
|
|
410
|
+
const valid = await this._validate(connection);
|
|
411
|
+
assert(valid, `Unable to validate initial connection "${connection.id}"`);
|
|
412
|
+
this._logger.debug(`Initial connection "${connection.id}" validated`);
|
|
413
|
+
this._adopt(connection);
|
|
414
|
+
this._started = true;
|
|
415
|
+
this._emit("started");
|
|
416
|
+
this._emit("connection_created", connection);
|
|
417
|
+
if (this._maximumIdleConnections > 0) {
|
|
418
|
+
this._available.push(connection);
|
|
419
|
+
} else {
|
|
420
|
+
this._evict(connection);
|
|
421
|
+
}
|
|
422
|
+
this._runCreateLoop();
|
|
423
|
+
return this;
|
|
424
|
+
} finally {
|
|
425
|
+
this._starting = false;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** Stop this {@link ConnectionPool} and disconnect all connections. */
|
|
429
|
+
stop() {
|
|
430
|
+
if (!this._started)
|
|
431
|
+
return;
|
|
432
|
+
this._started = false;
|
|
433
|
+
const connections = `${this._connections.size} connections`;
|
|
434
|
+
const requests = `${this._pending.length} pending requests`;
|
|
435
|
+
this._logger.info(`Stopping connection pool with ${connections} and ${requests}`);
|
|
436
|
+
for (const pending of this._pending) {
|
|
437
|
+
pending.reject(new Error("Connection pool stopped"));
|
|
438
|
+
}
|
|
439
|
+
this._available.splice(0, Number.MAX_SAFE_INTEGER);
|
|
440
|
+
this._borrowed.clear();
|
|
441
|
+
try {
|
|
442
|
+
this._emit("stopped");
|
|
443
|
+
} finally {
|
|
444
|
+
this._connections.clear();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
export {
|
|
449
|
+
ConnectionPool
|
|
450
|
+
};
|
|
451
|
+
//# sourceMappingURL=pool.mjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/pool.ts"],
|
|
4
|
+
"mappings": ";AAEA,OAAO,YAAY;AAEnB,SAAS,YAAY,sBAAsB;AAC3C,SAAS,eAAe;AAUxB,SAAS,eAAe,UAAkB,cAA8B;AACtE,QAAM,SAAS,QAAQ,IAAI,QAAQ;AACnC,MAAI,UAAU;AAAM,WAAO;AAC3B,QAAM,QAAQ,WAAW,MAAM;AAC/B,MAAI,MAAM,KAAK;AAAG,UAAM,IAAI,MAAM,kBAAkB,MAAM,+BAA+B,QAAQ,GAAG;AACpG,SAAO;AACT;AAGA,SAAS,gBAAgB,UAAkB,cAAgC;AACzE,QAAM,SAAS,QAAQ,IAAI,QAAQ;AACnC,MAAI,UAAU;AAAM,WAAO;AAC3B,QAAM,QAAQ,OAAO,YAAY;AACjC,MAAI,UAAU;AAAS,WAAO;AAC9B,MAAI,UAAU;AAAQ,WAAO;AAC7B,QAAM,IAAI,MAAM,kBAAkB,MAAM,+BAA+B,QAAQ,GAAG;AACpF;AAGA,IAAM,oBAAN,MAAwB;AAAA,EACd;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAoB;AAAA;AAAA;AAAA;AAAA;AAAA,EAM5B,YAAY,SAAiB;AAC3B,SAAK,WAAW,IAAI,QAAQ,CAA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AAAA,IACT,UAAE;AACA,WAAK,YAAY;AAAA,IACnB;AAAA,EACF;AAAA;AAAA,EAGA,OAAa;AACX,QAAI,CAAE,KAAK;AAAU;AACrB,SAAK,WAAW;AAEhB,UAAM,cAAc,GAAG,KAAK,aAAa,IAAI;AAC7C,UAAM,WAAW,GAAG,KAAK,SAAS,MAAM;AACxC,SAAK,QAAQ,KAAK,iCAAiC,WAAW,QAAQ,QAAQ,EAAE;AAGhF,eAAW,WAAW,KAAK,UAAU;AACnC,cAAQ,OAAO,IAAI,MAAM,yBAAyB,CAAC;AAAA,IACrD;AAGA,SAAK,WAAW,OAAO,GAAG,OAAO,gBAAgB;AACjD,SAAK,UAAU,MAAM;AAKrB,QAAI;AACF,WAAK,MAAM,SAAS;AAAA,IACtB,UAAE;AACA,WAAK,aAAa,MAAM;AAAA,IAC1B;AAAA,EACF;AACF;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/queue.cjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// queue.ts
|
|
21
|
+
var queue_exports = {};
|
|
22
|
+
__export(queue_exports, {
|
|
23
|
+
Queue: () => Queue
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(queue_exports);
|
|
26
|
+
var Executor = class {
|
|
27
|
+
constructor(_task, _resolve, _reject) {
|
|
28
|
+
this._task = _task;
|
|
29
|
+
this._resolve = _resolve;
|
|
30
|
+
this._reject = _reject;
|
|
31
|
+
}
|
|
32
|
+
execute() {
|
|
33
|
+
return Promise.resolve().then(async () => {
|
|
34
|
+
try {
|
|
35
|
+
const result = await this._task();
|
|
36
|
+
this._resolve(result);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
this._reject(error);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var Queue = class {
|
|
44
|
+
_queue = [];
|
|
45
|
+
_running = false;
|
|
46
|
+
_run() {
|
|
47
|
+
if (this._running)
|
|
48
|
+
return;
|
|
49
|
+
const next = this._queue.splice(0, 1)[0];
|
|
50
|
+
if (!next)
|
|
51
|
+
return;
|
|
52
|
+
this._running = true;
|
|
53
|
+
void next.execute().finally(() => {
|
|
54
|
+
this._running = false;
|
|
55
|
+
this._run();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
enqueue(task) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
const executor = new Executor(task, resolve, reject);
|
|
61
|
+
this._queue.push(executor);
|
|
62
|
+
process.nextTick(() => this._run());
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {
|
|
68
|
+
Queue
|
|
69
|
+
});
|
|
70
|
+
//# sourceMappingURL=queue.cjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/queue.ts"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,WAAN,MAAkB;AAAA,EAChB,YACY,OACA,UACA,SACV;AAHU;AACA;AACA;AAAA,EACT;AAAA,EAEH,UAAyB;AACvB,WAAO,QAAQ,QAAQ,EAAE,KAAK,YAAY;AACxC,UAAI;AACF,cAAM,SAAS,MAAM,KAAK,MAAM;AAChC,aAAK,SAAS,MAAM;AAAA,MACtB,SAAS,OAAO;AACd,aAAK,QAAQ,KAAK;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEO,IAAM,QAAN,MAAY;AAAA,EACT,SAA0B,CAAC;AAAA,EAC3B,WAAoB;AAAA,EAEpB,OAAa;AACnB,QAAI,KAAK;AAAU;AAEnB,UAAM,OAAO,KAAK,OAAO,OAAO,GAAG,CAAC,EAAE,CAAC;AACvC,QAAI,CAAE;AAAM;AAEZ,SAAK,WAAW;AAEhB,SAAK,KAAK,QAAQ,EAAE,QAAQ,MAAM;AAChC,WAAK,WAAW;AAChB,WAAK,KAAK;AAAA,IACZ,CAAC;AAAA,EACH;AAAA,EAEA,QAAW,MAA2B;AACpC,WAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,YAAM,WAAW,IAAI,SAAS,MAAM,SAAS,MAAM;AACnD,WAAK,OAAO,KAAK,QAAQ;AACzB,cAAQ,SAAS,MAAM,KAAK,KAAK,CAAC;AAAA,IACpC,CAAC;AAAA,EACH;AACF;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/queue.d.ts
ADDED
package/dist/queue.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// queue.ts
|
|
2
|
+
var Executor = class {
|
|
3
|
+
constructor(_task, _resolve, _reject) {
|
|
4
|
+
this._task = _task;
|
|
5
|
+
this._resolve = _resolve;
|
|
6
|
+
this._reject = _reject;
|
|
7
|
+
}
|
|
8
|
+
execute() {
|
|
9
|
+
return Promise.resolve().then(async () => {
|
|
10
|
+
try {
|
|
11
|
+
const result = await this._task();
|
|
12
|
+
this._resolve(result);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
this._reject(error);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var Queue = class {
|
|
20
|
+
_queue = [];
|
|
21
|
+
_running = false;
|
|
22
|
+
_run() {
|
|
23
|
+
if (this._running)
|
|
24
|
+
return;
|
|
25
|
+
const next = this._queue.splice(0, 1)[0];
|
|
26
|
+
if (!next)
|
|
27
|
+
return;
|
|
28
|
+
this._running = true;
|
|
29
|
+
void next.execute().finally(() => {
|
|
30
|
+
this._running = false;
|
|
31
|
+
this._run();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
enqueue(task) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const executor = new Executor(task, resolve, reject);
|
|
37
|
+
this._queue.push(executor);
|
|
38
|
+
process.nextTick(() => this._run());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
export {
|
|
43
|
+
Queue
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=queue.mjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/queue.ts"],
|
|
4
|
+
"mappings": ";AAEA,IAAM,WAAN,MAAkB;AAAA,EAChB,YACY,OACA,UACA,SACV;AAHU;AACA;AACA;AAAA,EACT;AAAA,EAEH,UAAyB;AACvB,WAAO,QAAQ,QAAQ,EAAE,KAAK,YAAY;AACxC,UAAI;AACF,cAAM,SAAS,MAAM,KAAK,MAAM;AAChC,aAAK,SAAS,MAAM;AAAA,MACtB,SAAS,OAAO;AACd,aAAK,QAAQ,KAAK;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEO,IAAM,QAAN,MAAY;AAAA,EACT,SAA0B,CAAC;AAAA,EAC3B,WAAoB;AAAA,EAEpB,OAAa;AACnB,QAAI,KAAK;AAAU;AAEnB,UAAM,OAAO,KAAK,OAAO,OAAO,GAAG,CAAC,EAAE,CAAC;AACvC,QAAI,CAAE;AAAM;AAEZ,SAAK,WAAW;AAEhB,SAAK,KAAK,QAAQ,EAAE,QAAQ,MAAM;AAChC,WAAK,WAAW;AAChB,WAAK,KAAK;AAAA,IACZ,CAAC;AAAA,EACH;AAAA,EAEA,QAAW,MAA2B;AACpC,WAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,YAAM,WAAW,IAAI,SAAS,MAAM,SAAS,MAAM;AACnD,WAAK,OAAO,KAAK,QAAQ;AACzB,cAAQ,SAAS,MAAM,KAAK,KAAK,CAAC;AAAA,IACpC,CAAC;AAAA,EACH;AACF;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@juit/pgproxy-pool",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "./dist/index.cjs",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"author": "Juit Developers <developers@juit.com>",
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@github.com/juitnow/juit-pgproxy.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"database",
|
|
27
|
+
"pg",
|
|
28
|
+
"pool",
|
|
29
|
+
"postgres",
|
|
30
|
+
"proxy"
|
|
31
|
+
],
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/juitnow/juit-pgproxy/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/juitnow/juit-pgproxy#readme",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"libpq": "^1.8.12"
|
|
38
|
+
},
|
|
39
|
+
"directories": {
|
|
40
|
+
"test": "test"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"*.md",
|
|
44
|
+
"dist/",
|
|
45
|
+
"src/"
|
|
46
|
+
]
|
|
47
|
+
}
|