update pay function

This commit is contained in:
2025-11-28 09:45:36 +08:00
parent 49656e6e88
commit 007de2d76d
2 changed files with 41 additions and 39 deletions

View File

@@ -161,29 +161,36 @@ class ClickHouseConnectionPool:
# 初始化核心连接(延迟初始化,首次使用时创建)
self._initialized = False
# 清理线程也延迟启动(避免 fork 前启动线程导致问题)
self._cleanup_thread_started = False
logger.info(f"ClickHouse 连接池配置完成: pool_size={pool_size}, max_overflow={max_overflow}, "
f"max_lifetime={max_connection_lifetime}s")
# 启动后台清理线程
self._start_cleanup_thread()
# 注册退出时清理
atexit.register(self.close_all)
def _start_cleanup_thread(self):
"""启动后台清理线程"""
def cleanup_worker():
while not self._cleanup_stop_event.wait(self.cleanup_interval):
if self._closed:
break
try:
self._cleanup_expired_connections()
except Exception as e:
logger.error(f"清理连接时出错: {e}")
"""启动后台清理线程(延迟启动,首次使用连接池时调用)"""
if self._cleanup_thread_started or self._closed:
return
self._cleanup_thread = _threading.Thread(target=cleanup_worker, daemon=True)
self._cleanup_thread.start()
logger.debug("后台清理线程已启动")
with self._lock:
if self._cleanup_thread_started or self._closed:
return
def cleanup_worker():
while not self._cleanup_stop_event.wait(self.cleanup_interval):
if self._closed:
break
try:
self._cleanup_expired_connections()
except Exception as e:
logger.error(f"清理连接时出错: {e}")
self._cleanup_thread = _threading.Thread(target=cleanup_worker, daemon=True, name="CH-Pool-Cleanup")
self._cleanup_thread.start()
self._cleanup_thread_started = True
logger.debug("后台清理线程已启动")
def _cleanup_expired_connections(self):
"""清理过期连接"""
@@ -241,6 +248,9 @@ class ClickHouseConnectionPool:
if self._initialized:
return
# 启动清理线程(延迟到首次使用时启动)
self._start_cleanup_thread()
# 只预创建 1 个连接,其余按需创建
init_count = min(1, self.pool_size)
for i in range(init_count):