219 lines
5.8 KiB
JavaScript
219 lines
5.8 KiB
JavaScript
const LibraryDatabase = require('./db');
|
|
|
|
/**
|
|
* 数据库连接池管理器
|
|
* 复用数据库连接,避免频繁创建/关闭
|
|
*/
|
|
class DatabasePool {
|
|
constructor() {
|
|
this.connections = new Map(); // libraryPath -> { db, lastUsed, refCount }
|
|
this.maxConnections = 1; // 最大连接数:1
|
|
this.maxIdleTime = 60000; // 60秒未使用则关闭(保持连接复用,减少创建/关闭)
|
|
this.cleanupInterval = null;
|
|
|
|
// 启动定期清理
|
|
this.startCleanup();
|
|
}
|
|
|
|
/**
|
|
* 获取数据库连接(复用或创建)
|
|
* 优化:严格限制最大1个活跃连接
|
|
*/
|
|
acquire(libraryPath) {
|
|
let conn = this.connections.get(libraryPath);
|
|
|
|
if (!conn) {
|
|
// 强制执行最大连接数限制:如果已有连接,先关闭所有空闲连接
|
|
if (this.connections.size >= this.maxConnections) {
|
|
for (const [path, existingConn] of this.connections.entries()) {
|
|
if (existingConn.refCount === 0) {
|
|
console.log(`[DBPool] Max connections reached, closing idle: ${path}`);
|
|
this.close(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 创建新连接
|
|
const db = new LibraryDatabase(libraryPath);
|
|
conn = {
|
|
db,
|
|
lastUsed: Date.now(),
|
|
refCount: 0
|
|
};
|
|
this.connections.set(libraryPath, conn);
|
|
console.log(`[DBPool] Created new connection for: ${libraryPath} (total: ${this.connections.size}/${this.maxConnections})`);
|
|
}
|
|
|
|
// 更新使用时间和引用计数
|
|
conn.lastUsed = Date.now();
|
|
conn.refCount++;
|
|
|
|
return conn.db;
|
|
}
|
|
|
|
/**
|
|
* 释放数据库连接(减少引用计数)
|
|
*/
|
|
release(libraryPath) {
|
|
const conn = this.connections.get(libraryPath);
|
|
if (conn) {
|
|
conn.refCount = Math.max(0, conn.refCount - 1);
|
|
conn.lastUsed = Date.now();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 强制关闭指定连接
|
|
*/
|
|
close(libraryPath) {
|
|
const conn = this.connections.get(libraryPath);
|
|
if (conn) {
|
|
try {
|
|
// 强制设置引用计数为0
|
|
conn.refCount = 0;
|
|
|
|
// 关闭数据库连接
|
|
if (conn.db && conn.db.db) {
|
|
// 执行 checkpoint 确保 WAL 写入主数据库
|
|
try {
|
|
conn.db.db.pragma('wal_checkpoint(TRUNCATE)');
|
|
} catch (e) {
|
|
console.warn(`[DBPool] WAL checkpoint warning:`, e.message);
|
|
}
|
|
|
|
// 关闭连接
|
|
conn.db.close();
|
|
}
|
|
|
|
this.connections.delete(libraryPath);
|
|
console.log(`[DBPool] Closed connection for: ${libraryPath}`);
|
|
} catch (error) {
|
|
console.error(`[DBPool] Error closing connection:`, error);
|
|
// 即使出错也删除引用
|
|
this.connections.delete(libraryPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关闭所有连接
|
|
*/
|
|
closeAll() {
|
|
console.log(`[DBPool] Closing all connections (${this.connections.size} total)`);
|
|
|
|
for (const [path, conn] of this.connections.entries()) {
|
|
try {
|
|
// 强制设置引用计数为0
|
|
conn.refCount = 0;
|
|
|
|
// 执行 WAL checkpoint
|
|
if (conn.db && conn.db.db) {
|
|
try {
|
|
conn.db.db.pragma('wal_checkpoint(TRUNCATE)');
|
|
} catch (e) {
|
|
console.warn(`[DBPool] WAL checkpoint warning for ${path}:`, e.message);
|
|
}
|
|
}
|
|
|
|
conn.db.close();
|
|
console.log(`[DBPool] Closed connection for: ${path}`);
|
|
} catch (error) {
|
|
console.error(`[DBPool] Error closing connection for ${path}:`, error);
|
|
}
|
|
}
|
|
this.connections.clear();
|
|
|
|
if (this.cleanupInterval) {
|
|
clearInterval(this.cleanupInterval);
|
|
this.cleanupInterval = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 强制关闭所有连接(用于紧急清理)
|
|
* 与 closeAll() 相同,但提供明确的语义
|
|
*/
|
|
forceCloseAll() {
|
|
console.log('[DBPool] Force closing all connections (emergency cleanup)');
|
|
this.closeAll();
|
|
}
|
|
|
|
/**
|
|
* 启动定期清理空闲连接
|
|
*/
|
|
startCleanup() {
|
|
// 更频繁的检查(每10秒)
|
|
this.cleanupInterval = setInterval(() => {
|
|
const now = Date.now();
|
|
const toClose = [];
|
|
|
|
for (const [path, conn] of this.connections.entries()) {
|
|
// 如果连接空闲超过阈值且没有引用,则关闭
|
|
if (conn.refCount === 0 && now - conn.lastUsed > this.maxIdleTime) {
|
|
toClose.push(path);
|
|
}
|
|
}
|
|
|
|
toClose.forEach(path => this.close(path));
|
|
|
|
if (toClose.length > 0) {
|
|
console.log(`[DBPool] Cleaned up ${toClose.length} idle connections`);
|
|
|
|
// 关闭连接后立即强制 GC
|
|
if (global.gc) {
|
|
for (let i = 0; i < 3; i++) {
|
|
global.gc();
|
|
}
|
|
console.log(`[DBPool] Forced GC after closing connections`);
|
|
}
|
|
}
|
|
}, 10000); // 每10秒检查一次(更频繁)
|
|
}
|
|
|
|
/**
|
|
* 获取连接池状态
|
|
*/
|
|
getStatus() {
|
|
const status = [];
|
|
for (const [path, conn] of this.connections.entries()) {
|
|
status.push({
|
|
path,
|
|
refCount: conn.refCount,
|
|
idleTime: Date.now() - conn.lastUsed
|
|
});
|
|
}
|
|
return {
|
|
connections: status,
|
|
totalConnections: this.connections.size,
|
|
maxConnections: this.maxConnections,
|
|
maxIdleTime: this.maxIdleTime
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 实现 clear() 方法以支持 CleanupManager 注册
|
|
*/
|
|
clear() {
|
|
// 清理空闲连接
|
|
const now = Date.now();
|
|
const toClose = [];
|
|
|
|
for (const [path, conn] of this.connections.entries()) {
|
|
if (conn.refCount === 0) {
|
|
toClose.push(path);
|
|
}
|
|
}
|
|
|
|
toClose.forEach(path => this.close(path));
|
|
|
|
if (toClose.length > 0) {
|
|
console.log(`[DBPool] Cleared ${toClose.length} idle connections`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 单例模式
|
|
const dbPool = new DatabasePool();
|
|
|
|
module.exports = dbPool;
|