refactor: 增加内存保护措施,精简启动任务

- 流式响应不再存储完整文本,仅记录长度用于 token 估算
- complete_response 文本累积增加 64KB 上限保护
- 通知缓冲区(email/webhook)增加溢出保护,超限丢弃旧通知
- 熔断器淘汰策略改进,全部 open/half-open 时按最旧失败时间淘汰
- 移除启动时清理任务和统计聚合回填,减少启动负担
- token 估算简化为基于长度的方法,避免持有完整文本
This commit is contained in:
fawney19
2026-03-10 16:08:11 +08:00
parent 6ec8df97e8
commit 7b0c80a0c4
5 changed files with 62 additions and 49 deletions

View File

@@ -55,6 +55,7 @@ class EmailNotificationPlugin(NotificationPlugin):
# 缓冲配置
self._buffer: list[Notification] = []
self._buffer_max_size = config.get("buffer_max_size", 500) if config else 500
self._lock = asyncio.Lock()
self._flush_task: asyncio.Task[None] | None = None
@@ -285,6 +286,12 @@ class EmailNotificationPlugin(NotificationPlugin):
"""
# 添加到缓冲区
async with self._lock:
# 缓冲区溢出保护:丢弃最旧的通知
if len(self._buffer) >= self._buffer_max_size:
drop_count = len(self._buffer) - self._buffer_max_size + 1
del self._buffer[:drop_count]
logger.warning("Email 通知缓冲区溢出,丢弃 {} 条旧通知", drop_count)
self._buffer.append(notification)
# 如果是严重通知,立即发送

View File

@@ -52,6 +52,7 @@ class WebhookNotificationPlugin(NotificationPlugin):
# 缓冲配置
self._buffer: list[Notification] = []
self._buffer_max_size = config.get("buffer_max_size", 500) if config else 500
self._lock = asyncio.Lock()
self._session: aiohttp.ClientSession | None = None
self._flush_task = None
@@ -199,6 +200,12 @@ class WebhookNotificationPlugin(NotificationPlugin):
"""
# 添加到缓冲区
async with self._lock:
# 缓冲区溢出保护:丢弃最旧的通知
if len(self._buffer) >= self._buffer_max_size:
drop_count = len(self._buffer) - self._buffer_max_size + 1
del self._buffer[:drop_count]
logger.warning("Webhook 通知缓冲区溢出,丢弃 {} 条旧通知", drop_count)
self._buffer.append(notification)
# 如果是严重通知,立即发送