mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 09:50:21 +08:00
refactor: 增加内存保护措施,精简启动任务
- 流式响应不再存储完整文本,仅记录长度用于 token 估算 - complete_response 文本累积增加 64KB 上限保护 - 通知缓冲区(email/webhook)增加溢出保护,超限丢弃旧通知 - 熔断器淘汰策略改进,全部 open/half-open 时按最旧失败时间淘汰 - 移除启动时清理任务和统计聚合回填,减少启动负担 - token 估算简化为基于长度的方法,避免持有完整文本
This commit is contained in:
@@ -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)
|
||||
|
||||
# 如果是严重通知,立即发送
|
||||
|
||||
@@ -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)
|
||||
|
||||
# 如果是严重通知,立即发送
|
||||
|
||||
Reference in New Issue
Block a user