fix: reduce mysql connection pressure
This commit is contained in:
@@ -10,6 +10,7 @@ from typing import Optional, Dict, List
|
||||
from pathlib import Path
|
||||
from enum import Enum
|
||||
import os
|
||||
from db.chat_log_db import _get_pooled_conn, _return_conn
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
_DB_TYPE = os.getenv("DB_TYPE", "sqlite").lower()
|
||||
@@ -45,6 +46,19 @@ class TaskPriority(Enum):
|
||||
HIGH = "high"
|
||||
URGENT = "urgent"
|
||||
|
||||
|
||||
class _PooledMySQLConn:
|
||||
"""包装 pymysql 连接,close 时归还到共享连接池。"""
|
||||
|
||||
def __init__(self, conn):
|
||||
self._conn = conn
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._conn, name)
|
||||
|
||||
def close(self):
|
||||
_return_conn(self._conn)
|
||||
|
||||
class TaskManager:
|
||||
"""任务管理器 - SQLite 存储"""
|
||||
|
||||
@@ -139,17 +153,7 @@ class TaskManager:
|
||||
def _get_conn(self):
|
||||
"""获取数据库连接"""
|
||||
if _is_mysql():
|
||||
import pymysql
|
||||
return pymysql.connect(
|
||||
host=_MYSQL_HOST,
|
||||
port=_MYSQL_PORT,
|
||||
user=_MYSQL_USER,
|
||||
password=_MYSQL_PASSWORD,
|
||||
database=_MYSQL_DATABASE,
|
||||
charset="utf8mb4",
|
||||
cursorclass=pymysql.cursors.DictCursor,
|
||||
autocommit=False,
|
||||
)
|
||||
return _PooledMySQLConn(_get_pooled_conn())
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
Reference in New Issue
Block a user