Добавлен 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`,
publicDir: `${execDir}/public`,
uploadDir: `${execDir}/public/upload`,
dbFileName: 'db.sqlite',
loggingEnabled: true,
maxUploadFileSize: 50*1024*1024,//50Мб
@@ -23,6 +22,19 @@ module.exports = {
useExternalBookConverter: false,
db: [
{
poolName: 'app',
connCount: 20,
fileName: 'app.sqlite',
},
{
poolName: 'readerStorage',
connCount: 20,
fileName: 'reader-storage.sqlite',
}
],
servers: [
{
serverName: '1',

View File

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

View File

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

View File

@@ -4,20 +4,18 @@ const sqlite = require('sqlite');
const waitingDelay = 100; //ms
class SqliteConnectionPool {
constructor(connCount, config) {
this.connCount = connCount;
this.config = config;
constructor() {
this.closed = true;
}
async init() {
const dbFileName = this.config.dataDir + '/' + this.config.dbFileName;
async open(connCount, dbFileName) {
if (!Number.isInteger(connCount) || connCount <= 0)
return;
this.connections = [];
this.taken = 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);
client.configure('busyTimeout', 10000); //ms
@@ -29,6 +27,7 @@ class SqliteConnectionPool {
this.freed.add(i);
this.connections[i] = client;
}
this.closed = false;
}
_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 compression = require('compression');
const SqliteConnectionPool = require('./core/SqliteConnectionPool');
const connManager = require('./core/connManager');
async function init() {
await fs.ensureDir(config.dataDir);
@@ -35,9 +35,9 @@ async function main() {
log('Initializing');
await init();
log('Opening database');
const connPool = new SqliteConnectionPool(20, config);
await connPool.init();
log('Opening databases');
await connManager.init(config);
log(`Opened databases: ${Object.keys(connManager.pool).join(', ')}`);
//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) {
devModule.logErrors(app);

View File

@@ -2,10 +2,10 @@ const c = require('./controllers');
const utils = require('./core/utils');
const multer = require('multer');
function initRoutes(app, connPool, config) {
const misc = new c.MiscController(connPool, config);
const reader = new c.ReaderController(connPool, config);
const worker = new c.WorkerController(connPool, config);
function initRoutes(app, config) {
const misc = new c.MiscController(config);
const reader = new c.ReaderController(config);
const worker = new c.WorkerController(config);
//access
const [aAll, aNormal, aSite, aReader, aOmnireader] = // eslint-disable-line no-unused-vars
@@ -60,7 +60,7 @@ function initRoutes(app, connPool, config) {
};
}
callbacks.push(callback);
switch (httpMethod) {
case 'GET' :
app.get(path, ...callbacks);