Добавлен 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

@@ -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;