Add MariaDB queue table. Fix Bugs
This commit is contained in:
+18
-2
@@ -4,6 +4,8 @@ import {} from "../types";
|
||||
import { SQL } from "bun";
|
||||
/** Reuse one client per distinct connection config to avoid exhausting MariaDB connections. */
|
||||
const clientCache = new Map();
|
||||
/** Cache key each client was created with */
|
||||
const clientKeys = new WeakMap();
|
||||
/**
|
||||
* Cache key covering everything that affects the connection, not just the
|
||||
* database name.
|
||||
@@ -24,8 +26,7 @@ export default function grabMariaDBClient({ config, }) {
|
||||
}
|
||||
// Prefer the process-wide client when it was created from the same config
|
||||
if (global.MARIADB_CLIENT &&
|
||||
global.CONFIG &&
|
||||
mariadbClientCacheKey(global.CONFIG) === cacheKey) {
|
||||
clientKeys.get(global.MARIADB_CLIENT) === cacheKey) {
|
||||
clientCache.set(cacheKey, global.MARIADB_CLIENT);
|
||||
return global.MARIADB_CLIENT;
|
||||
}
|
||||
@@ -53,6 +54,21 @@ export default function grabMariaDBClient({ config, }) {
|
||||
...config.db_config,
|
||||
adapter: "mariadb",
|
||||
});
|
||||
/**
|
||||
* A closed client can't run queries, so forget it on close: the next
|
||||
* grab (or `ensureInit`) creates a fresh one.
|
||||
*/
|
||||
const close = MariaDBClient.close.bind(MariaDBClient);
|
||||
MariaDBClient.close = (...args) => {
|
||||
if (clientCache.get(cacheKey) === MariaDBClient) {
|
||||
clientCache.delete(cacheKey);
|
||||
}
|
||||
if (global.MARIADB_CLIENT === MariaDBClient) {
|
||||
global.MARIADB_CLIENT = undefined;
|
||||
}
|
||||
return close(...args);
|
||||
};
|
||||
clientKeys.set(MariaDBClient, cacheKey);
|
||||
clientCache.set(cacheKey, MariaDBClient);
|
||||
return MariaDBClient;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user