mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-24 05:30:08 +08:00
bugfix:修复sqlite部分场景下锁竞争问题;新增插件恶意触发配置文件 (#2144)
* bugfix:修复sqlite部分场景下锁竞争问题;新增插件恶意触发配置文件 * 文件没同步完
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from nonebot import on_message
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot_plugin_alconna import UniMsg
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
@@ -12,7 +8,12 @@ from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||
from zhenxun.models.chat_history import ChatHistory
|
||||
from zhenxun.services.db_context import with_db_timeout
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.message_load import is_overloaded, should_pause_tasks
|
||||
from zhenxun.services.low_priority_writer import (
|
||||
LowPriorityWriterConfig,
|
||||
append_low_priority_record,
|
||||
register_low_priority_writer,
|
||||
)
|
||||
from zhenxun.services.message_load import is_overloaded
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.utils import get_entity_ids
|
||||
|
||||
@@ -44,23 +45,45 @@ def rule(message: UniMsg) -> bool:
|
||||
|
||||
chat_history = on_message(rule=rule, priority=1, block=False)
|
||||
|
||||
_HISTORY_QUEUE: asyncio.Queue[ChatHistory] = asyncio.Queue(maxsize=5000)
|
||||
_DROP_COUNT = 0
|
||||
_LAST_DROP_LOG = 0.0
|
||||
_DROP_LOG_INTERVAL = 10.0
|
||||
_WRITER_NAME = "chat_history"
|
||||
_FLUSH_BATCH_SIZE = 200
|
||||
_FLUSH_MAX_PER_TICK = 1000
|
||||
_FLUSH_DB_TIMEOUT = 5.0
|
||||
|
||||
|
||||
async def _write_chat_history_batch(batch: list[ChatHistory], reason: str) -> None:
|
||||
await with_db_timeout(
|
||||
ChatHistory.bulk_create(batch, _FLUSH_BATCH_SIZE),
|
||||
timeout=_FLUSH_DB_TIMEOUT,
|
||||
operation=f"ChatHistory.bulk_create[{len(batch)}]",
|
||||
source=f"chat_history:{reason}",
|
||||
)
|
||||
|
||||
|
||||
register_low_priority_writer(
|
||||
LowPriorityWriterConfig(
|
||||
name=_WRITER_NAME,
|
||||
write_batch=_write_chat_history_batch,
|
||||
batch_size=_FLUSH_BATCH_SIZE,
|
||||
trigger_size=_FLUSH_BATCH_SIZE,
|
||||
max_retain=5000,
|
||||
flush_interval_seconds=60.0,
|
||||
max_items_per_cycle=_FLUSH_MAX_PER_TICK,
|
||||
backoff_base_seconds=30.0,
|
||||
backoff_max_seconds=600.0,
|
||||
log_command="chat_history",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@chat_history.handle()
|
||||
async def _(message: UniMsg, session: Uninfo):
|
||||
entity = get_entity_ids(session)
|
||||
now = time.time()
|
||||
if is_overloaded():
|
||||
return
|
||||
try:
|
||||
_HISTORY_QUEUE.put_nowait(
|
||||
await append_low_priority_record(
|
||||
_WRITER_NAME,
|
||||
ChatHistory(
|
||||
user_id=entity.user_id,
|
||||
group_id=entity.group_id,
|
||||
@@ -68,50 +91,7 @@ async def _(message: UniMsg, session: Uninfo):
|
||||
plain_text=message.extract_plain_text(),
|
||||
bot_id=session.self_id,
|
||||
platform=session.platform,
|
||||
)
|
||||
),
|
||||
)
|
||||
except asyncio.QueueFull:
|
||||
global _DROP_COUNT, _LAST_DROP_LOG
|
||||
_DROP_COUNT += 1
|
||||
if now - _LAST_DROP_LOG > _DROP_LOG_INTERVAL:
|
||||
_LAST_DROP_LOG = now
|
||||
logger.debug(
|
||||
f"chat_history queue full, dropped {_DROP_COUNT} items",
|
||||
"chat_history",
|
||||
)
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"interval",
|
||||
minutes=1,
|
||||
max_instances=1,
|
||||
coalesce=True,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
if should_pause_tasks():
|
||||
return
|
||||
flushed = 0
|
||||
while flushed < _FLUSH_MAX_PER_TICK:
|
||||
message_list: list[ChatHistory] = []
|
||||
limit = min(_FLUSH_BATCH_SIZE, _FLUSH_MAX_PER_TICK - flushed)
|
||||
for _ in range(limit):
|
||||
try:
|
||||
message_list.append(_HISTORY_QUEUE.get_nowait())
|
||||
except asyncio.QueueEmpty:
|
||||
break
|
||||
if not message_list:
|
||||
break
|
||||
await with_db_timeout(
|
||||
ChatHistory.bulk_create(message_list, _FLUSH_BATCH_SIZE),
|
||||
timeout=_FLUSH_DB_TIMEOUT,
|
||||
operation=f"ChatHistory.bulk_create[{len(message_list)}]",
|
||||
source="chat_history",
|
||||
)
|
||||
flushed += len(message_list)
|
||||
if flushed:
|
||||
backlog = _HISTORY_QUEUE.qsize()
|
||||
suffix = f",剩余队列 {backlog} 条" if backlog else ""
|
||||
logger.debug(f"批量添加聊天记录 {flushed} 条{suffix}", "定时任务")
|
||||
except Exception as e:
|
||||
logger.warning("存储聊天记录失败", "chat_history", e=e)
|
||||
|
||||
Reference in New Issue
Block a user