Добавлен connManager для управления пулами соединений к базам Sqlite, попутный рефакторинг

This commit is contained in:
Book Pauk
2019-03-08 16:50:44 +07:00
parent c7abae10b7
commit a39626f867
7 changed files with 59 additions and 23 deletions

View File

@@ -14,7 +14,6 @@ module.exports = {
logDir: `${dataDir}/log`, logDir: `${dataDir}/log`,
publicDir: `${execDir}/public`, publicDir: `${execDir}/public`,
uploadDir: `${execDir}/public/upload`, uploadDir: `${execDir}/public/upload`,
dbFileName: 'db.sqlite',
loggingEnabled: true, loggingEnabled: true,
maxUploadFileSize: 50*1024*1024,//50Мб maxUploadFileSize: 50*1024*1024,//50Мб
@@ -23,6 +22,19 @@ module.exports = {
useExternalBookConverter: false, useExternalBookConverter: false,
db: [
{
poolName: 'app',
connCount: 20,
fileName: 'app.sqlite',
},
{
poolName: 'readerStorage',
connCount: 20,
fileName: 'reader-storage.sqlite',
}
],
servers: [ servers: [
{ {
serverName: '1', serverName: '1',

View File

@@ -1,6 +1,5 @@
class BaseController { class BaseController {
constructor(connPool, config) { constructor(config) {
this.connPool = connPool;
this.config = config; this.config = config;
} }
} }

View File

@@ -5,8 +5,8 @@ const workerState = require('../core/workerState');
//const _ = require('lodash'); //const _ = require('lodash');
class ReaderController extends BaseController { class ReaderController extends BaseController {
constructor(connPool, config) { constructor(config) {
super(connPool, config); super(config);
this.readerWorker = new ReaderWorker(config); this.readerWorker = new ReaderWorker(config);
} }

View File

@@ -4,20 +4,18 @@ const sqlite = require('sqlite');
const waitingDelay = 100; //ms const waitingDelay = 100; //ms
class SqliteConnectionPool { class SqliteConnectionPool {
constructor(connCount, config) { constructor() {
this.connCount = connCount; this.closed = true;
this.config = config;
} }
async init() { async open(connCount, dbFileName) {
const dbFileName = this.config.dataDir + '/' + this.config.dbFileName; if (!Number.isInteger(connCount) || connCount <= 0)
return;
this.connections = []; this.connections = [];
this.taken = new Set(); this.taken = new Set();
this.freed = new Set(); this.freed = new Set();
for (let i = 0; i < this.connCount; i++) { for (let i = 0; i < connCount; i++) {
let client = await sqlite.open(dbFileName); let client = await sqlite.open(dbFileName);
client.configure('busyTimeout', 10000); //ms client.configure('busyTimeout', 10000); //ms
@@ -29,6 +27,7 @@ class SqliteConnectionPool {
this.freed.add(i); this.freed.add(i);
this.connections[i] = client; this.connections[i] = client;
} }
this.closed = false;
} }
_setImmediate() { _setImmediate() {

View File

@@ -0,0 +1,26 @@
const SqliteConnectionPool = require('./SqliteConnectionPool');
class ConnManager {
constructor() {
this._pool = {};
}
async init(config) {
this.config = config;
for (const poolConfig of this.config.db) {
const dbFileName = this.config.dataDir + '/' + poolConfig.fileName;
const connPool = new SqliteConnectionPool();
await connPool.open(poolConfig.connCount, dbFileName);
this._pool[poolConfig.poolName] = connPool;
}
}
get pool() {
return this._pool;
}
}
const connManager = new ConnManager();
module.exports = connManager;

View File

@@ -11,7 +11,7 @@ const path = require('path');
const express = require('express'); const express = require('express');
const compression = require('compression'); const compression = require('compression');
const SqliteConnectionPool = require('./core/SqliteConnectionPool'); const connManager = require('./core/connManager');
async function init() { async function init() {
await fs.ensureDir(config.dataDir); await fs.ensureDir(config.dataDir);
@@ -35,9 +35,9 @@ async function main() {
log('Initializing'); log('Initializing');
await init(); await init();
log('Opening database'); log('Opening databases');
const connPool = new SqliteConnectionPool(20, config); await connManager.init(config);
await connPool.init(); log(`Opened databases: ${Object.keys(connManager.pool).join(', ')}`);
//servers //servers
for (let server of config.servers) { for (let server of config.servers) {
@@ -67,7 +67,7 @@ async function main() {
} }
})); }));
require('./routes').initRoutes(app, connPool, serverConfig); require('./routes').initRoutes(app, serverConfig);
if (devModule) { if (devModule) {
devModule.logErrors(app); devModule.logErrors(app);

View File

@@ -2,10 +2,10 @@ const c = require('./controllers');
const utils = require('./core/utils'); const utils = require('./core/utils');
const multer = require('multer'); const multer = require('multer');
function initRoutes(app, connPool, config) { function initRoutes(app, config) {
const misc = new c.MiscController(connPool, config); const misc = new c.MiscController(config);
const reader = new c.ReaderController(connPool, config); const reader = new c.ReaderController(config);
const worker = new c.WorkerController(connPool, config); const worker = new c.WorkerController(config);
//access //access
const [aAll, aNormal, aSite, aReader, aOmnireader] = // eslint-disable-line no-unused-vars const [aAll, aNormal, aSite, aReader, aOmnireader] = // eslint-disable-line no-unused-vars