Skip to content

Commit 7a2b574

Browse files
author
Sylvestre Gug
committed
added pools and mysql pools implementation
1 parent d36d39f commit 7a2b574

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

lib/mysql.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import JSONStream from "JSONStream";
22
import {json} from "micro";
3-
import mysql, {createConnection} from "mysql2";
3+
import mysql, {createConnection, createPool} from "mysql2";
44
import {failedCheck} from "./errors.js";
55
import {notFound} from "./errors.js";
6+
import Pools from "./pools.js";
67

78
const {Types, ConnectionConfig} = mysql;
89

10+
export const pools = new Pools(({host, port, database, user, password, ssl}) =>
11+
createPool({
12+
host,
13+
port,
14+
database,
15+
user,
16+
password,
17+
ssl: ssl === "required" ? {} : false,
18+
connectTimeout: 25e3,
19+
connectionLimit: 30,
20+
decimalNumbers: true,
21+
})
22+
);
23+
924
export async function query(req, res, pool) {
1025
const {sql, params} = await json(req);
1126
const keepAlive = setInterval(() => res.write("\n"), 25e3);

lib/pools.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import LRU from "lru-cache";
2+
import * as Sentry from "@sentry/node";
3+
4+
const ttl = 1000 * 60 * 10; // 10m
5+
6+
export default class Pools {
7+
constructor(createPool) {
8+
this.createPool = createPool;
9+
this.cache = new LRU({
10+
max: 100,
11+
ttl,
12+
updateAgeOnGet: true,
13+
dispose(_key, pool) {
14+
pool.end();
15+
},
16+
});
17+
18+
let loop;
19+
(loop = () => {
20+
this.cache.purgeStale();
21+
this.timeout = setTimeout(loop, ttl / 2);
22+
})();
23+
}
24+
25+
async get(credentials) {
26+
const key = JSON.stringify(credentials);
27+
if (this.cache.has(key)) return this.cache.get(key);
28+
const pool = await this.createPool(credentials);
29+
30+
pool.on("error", (error) => {
31+
// We need to attach a handler otherwise the process could exit, but we
32+
// just don't care about these errors because the client will get cleaned
33+
// up already. For debugging purposes, we'll add a Sentry breadcrumb if
34+
// something else errors more loudly.
35+
Sentry.addBreadcrumb({
36+
message: error.message,
37+
category: "pool",
38+
level: "error",
39+
data: error,
40+
});
41+
});
42+
43+
this.cache.set(key, pool);
44+
return pool;
45+
}
46+
47+
del(credentials) {
48+
this.cache.del(JSON.stringify(credentials));
49+
}
50+
51+
end() {
52+
if (this.timeout) clearTimeout(this.timeout);
53+
for (const pool of this.cache.values()) pool.end();
54+
}
55+
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
},
2020
"dependencies": {
2121
"@databricks/sql": "https://github.com/observablehq/databricks-sql-nodejs",
22+
"@sentry/node": "^7.33.0",
2223
"JSONStream": "^1.3.5",
2324
"ajv": "^8.11.0",
25+
"lru-cache": "^7.14.1",
2426
"micro": "^9.3.4",
2527
"mssql": "^9.0.1",
2628
"mysql2": "^3.0.1",

yarn.lock

+50
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,41 @@
11911191
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.2.0.tgz#89ef99401cde6208cff98760b67663726ef26686"
11921192
integrity sha512-0nBr+VZNKm9tvNDZFstI3Pq1fCTEDK5OZTnVKNvBNAKgd0yIvmwsP4m61rEv7ZP+tOUjWJhROpxK5MsnlF911g==
11931193

1194+
1195+
version "7.33.0"
1196+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.33.0.tgz#7cba1670c041fae02794729b74e9fb9d1f519755"
1197+
integrity sha512-mrSgUnXjxHVi0cVea1lv7gC/Y66ya2a3atCHaPEij/+l+3APg5d0Ixt1zMx5YllMiZKf6wpxlZ0uwXcqdAAw+w==
1198+
dependencies:
1199+
"@sentry/types" "7.33.0"
1200+
"@sentry/utils" "7.33.0"
1201+
tslib "^1.9.3"
1202+
1203+
"@sentry/node@^7.33.0":
1204+
version "7.33.0"
1205+
resolved "https://registry.yarnpkg.com/@sentry/node/-/node-7.33.0.tgz#d5c7c7094543dd9819422dfc69952ed40416bfab"
1206+
integrity sha512-isQVF9LLSG4EZLHiSJ3chgK6f3ZBdGxm8fX6YGm8HWz07CubJddes3yBPLPRNXrRLd7X3SK8pPcK5oc3LIKqAw==
1207+
dependencies:
1208+
"@sentry/core" "7.33.0"
1209+
"@sentry/types" "7.33.0"
1210+
"@sentry/utils" "7.33.0"
1211+
cookie "^0.4.1"
1212+
https-proxy-agent "^5.0.0"
1213+
lru_map "^0.3.3"
1214+
tslib "^1.9.3"
1215+
1216+
1217+
version "7.33.0"
1218+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.33.0.tgz#7d4893a783360a868382e5194b50dbf034ba23c0"
1219+
integrity sha512-5kkmYjtBWSbPxfYGiXdZFPS6xpFBNlXvDqeX4NpCFXz6/LiEDn6tZ61kuCSFb8MZlyqyCX5WsP3aiI2FJfpGIA==
1220+
1221+
1222+
version "7.33.0"
1223+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.33.0.tgz#e6910139328b49b9cc21186521bdb10390dfd915"
1224+
integrity sha512-msp02GV1gOfaN5FjKjWxI00rtbYLXEE5cTGldhs/Dt9KI63dDk1nwPDkSLhg6joqRItAq0thlBh6un717HdWbg==
1225+
dependencies:
1226+
"@sentry/types" "7.33.0"
1227+
tslib "^1.9.3"
1228+
11941229
"@sideway/address@^4.1.3":
11951230
version "4.1.4"
11961231
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0"
@@ -1975,6 +2010,11 @@ convert-source-map@^1.7.0:
19752010
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.9.0.tgz#7faae62353fb4213366d0ca98358d22e8368b05f"
19762011
integrity sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==
19772012

2013+
cookie@^0.4.1:
2014+
version "0.4.2"
2015+
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
2016+
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
2017+
19782018
copy-descriptor@^0.1.0:
19792019
version "0.1.1"
19802020
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
@@ -3584,6 +3624,11 @@ lru-cache@^7.14.1:
35843624
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.14.1.tgz#8da8d2f5f59827edb388e63e459ac23d6d408fea"
35853625
integrity sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==
35863626

3627+
lru_map@^0.3.3:
3628+
version "0.3.3"
3629+
resolved "https://registry.yarnpkg.com/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd"
3630+
integrity sha512-Pn9cox5CsMYngeDbmChANltQl+5pi6XmTrraMSzhPmMBbmgcxmqWry0U3PGapCU1yB4/LqCcom7qhHZiF/jGfQ==
3631+
35873632
make-dir@^1.0.0:
35883633
version "1.3.0"
35893634
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c"
@@ -5235,6 +5280,11 @@ triple-beam@^1.3.0:
52355280
resolved "https://registry.yarnpkg.com/triple-beam/-/triple-beam-1.3.0.tgz#a595214c7298db8339eeeee083e4d10bd8cb8dd9"
52365281
integrity sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==
52375282

5283+
tslib@^1.9.3:
5284+
version "1.14.1"
5285+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
5286+
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
5287+
52385288
tslib@^2.0.1, tslib@^2.1.0, tslib@^2.2.0:
52395289
version "2.4.0"
52405290
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"

0 commit comments

Comments
 (0)