disable pool for sqlite, re-use a connection to improve the performance.

pull/227/head
LouisLam 3 years ago
parent 86826fb826
commit 61e758d872

@ -3,6 +3,7 @@ const { sleep } = require("../src/util");
const { R } = require("redbean-node");
const { setSetting, setting } = require("./util-server");
const knex = require("knex");
const sqlite3 = require("@louislam/sqlite3");
class Database {
@ -10,24 +11,32 @@ class Database {
static path = "./data/kuma.db";
static latestVersion = 6;
static noReject = true;
static sqliteInstance = null;
static async connect() {
if (! this.sqliteInstance) {
this.sqliteInstance = new sqlite3.Database(Database.path);
}
const Dialect = require("knex/lib/dialects/sqlite3/index.js");
Dialect.prototype._driver = () => require("@louislam/sqlite3");
Dialect.prototype._driver = () => sqlite3;
// Disable Pool by overriding acquireConnection()
Dialect.prototype.acquireConnection = async () => {
return this.sqliteInstance;
}
Dialect.prototype.releaseConnection = async () => { }
R.setup(knex({
const knexInstance = knex({
client: Dialect,
connection: {
filename: Database.path,
},
useNullAsDefault: true,
pool: {
min: 1,
max: 1,
acquireTimeoutMillis: 3600 * 1000,
idleTimeoutMillis: 3600 * 1000
}
}));
});
R.setup(knexInstance);
if (process.env.SQL_LOG === "1") {
R.debug(true);
@ -121,27 +130,10 @@ class Database {
* @returns {Promise<void>}
*/
static async close() {
const listener = (reason, p) => {
Database.noReject = false;
};
process.addListener("unhandledRejection", listener);
console.log("Closing DB")
while (true) {
Database.noReject = true;
await R.close()
await sleep(2000)
if (Database.noReject) {
break;
} else {
console.log("Waiting to close the db")
}
if (this.sqliteInstance) {
this.sqliteInstance.close();
}
console.log("SQLite closed")
process.removeListener("unhandledRejection", listener);
console.log("Stopped database");
}
}

@ -811,11 +811,10 @@ async function shutdownFunction(signal) {
}
await sleep(2000);
await Database.close();
console.log("Stopped DB")
}
function finalFunction() {
console.log("Graceful Shutdown Done")
console.log("Graceful shutdown successfully!");
}
gracefulShutdown(server, {

Loading…
Cancel
Save