修復 too many connection 問題

This commit is contained in:
2025-09-21 02:46:16 +08:00
parent a36ab3c98d
commit 808d5bb52c
36 changed files with 5582 additions and 249 deletions

View File

@@ -5,6 +5,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { db } from './database';
import { dbMonitor } from './database-monitor';
import { dbShutdownManager } from './database-shutdown-manager';
// 連線池狀態追蹤
let connectionCount = 0;
@@ -135,22 +136,24 @@ export function startConnectionMonitoring() {
console.log('🔍 資料庫連線監控已啟動');
}
// 優雅的關閉
// 優雅的關閉(使用新的關閉管理器)
export async function gracefulShutdown() {
console.log('🔄 正在優雅關閉資料庫連線...');
try {
dbMonitor.stopMonitoring();
await db.close();
console.log('✅ 資料庫連線已關閉');
} catch (error) {
console.error('❌ 關閉資料庫連線時發生錯誤:', error);
}
console.log('🔄 使用資料庫關閉管理器進行優雅關閉...');
await dbShutdownManager.gracefulShutdown();
}
// 處理程序退出事件
if (typeof process !== 'undefined') {
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
process.on('exit', gracefulShutdown);
// 強制關閉
export function forceShutdown() {
console.log('🚨 強制關閉資料庫連線...');
dbShutdownManager.forceShutdown();
}
// 獲取關閉狀態
export function getShutdownStatus() {
return dbShutdownManager.getShutdownStatus();
}
// 測試關閉機制
export async function testShutdown() {
return await dbShutdownManager.testShutdown();
}