-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarnPoolAdapter.js
More file actions
73 lines (65 loc) · 1.92 KB
/
TarnPoolAdapter.js
File metadata and controls
73 lines (65 loc) · 1.92 KB
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
/**
* TarnPoolAdapter — Adapts tarn.js Pool to the JSDBC ConnectionPool interface.
*
* Usage:
* import { Pool } from 'tarn';
* import { TarnPoolAdapter } from '@alt-javascript/jsdbc-core';
*
* const pool = TarnPoolAdapter.create(Pool, {
* create: () => ds.getConnection(),
* destroy: (conn) => conn.close(),
* validate: (conn) => !conn.isClosed(),
* min: 0,
* max: 10,
* });
*/
import ConnectionPool from './ConnectionPool.js';
export default class TarnPoolAdapter extends ConnectionPool {
/**
* @param {Object} tarnPool — an instance of tarn.Pool
*/
constructor(tarnPool) {
super();
this._pool = tarnPool;
}
/**
* Create a TarnPoolAdapter from tarn.js Pool class and config.
* @param {Function} TarnPoolClass — the tarn.Pool constructor
* @param {Object} config — tarn.js pool config (create, destroy, validate, min, max, etc.)
* @returns {TarnPoolAdapter}
*/
static create(TarnPoolClass, config) {
const pool = new TarnPoolClass({
create: config.create,
destroy: config.destroy,
validate: config.validate || (() => true),
min: config.min ?? 0,
max: config.max ?? 10,
acquireTimeoutMillis: config.acquireTimeoutMillis ?? 30000,
createTimeoutMillis: config.createTimeoutMillis ?? 30000,
destroyTimeoutMillis: config.destroyTimeoutMillis ?? 5000,
idleTimeoutMillis: config.idleTimeoutMillis ?? 30000,
reapIntervalMillis: config.reapIntervalMillis ?? 1000,
});
return new TarnPoolAdapter(pool);
}
async acquire() {
const acquire = this._pool.acquire();
return acquire.promise;
}
async release(connection) {
this._pool.release(connection);
}
async destroy() {
await this._pool.destroy();
}
get numUsed() {
return this._pool.numUsed();
}
get numFree() {
return this._pool.numFree();
}
get numPending() {
return this._pool.numPendingAcquires();
}
}