mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
性能优化 (#2126)
* 性能优化 * 代码改进 * 优化浏览器代际切换逻辑 * 统一缓存与生命周期 * 添加aiomysql依赖 * 优化插件路径处理逻辑,简化条件判断;在虚拟环境包管理器中添加编码和错误处理参数以增强稳定性 * 🚨 auto fix by pre-commit hooks * 优化Windows下的关闭逻辑 * 代码优化 * bugfix:修复配置重载问题 * bugfix:修复插件加载启动竞态问题 * 收敛事件入口和权限上下文 * 优化 Windows launcher 关闭重启兜底 --------- Co-authored-by: HibiKier <775757368@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
HibiKier
pre-commit-ci[bot]
parent
24c316cd2c
commit
5d92ccd3b0
@@ -1,5 +1,7 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
from nonebot import get_driver
|
||||
from nonebot.adapters import Bot, Event
|
||||
from nonebot.adapters.onebot.v11 import PokeNotifyEvent
|
||||
from nonebot.matcher import Matcher
|
||||
@@ -25,7 +27,35 @@ __plugin_meta__ = PluginMetadata(
|
||||
).to_dict(),
|
||||
)
|
||||
|
||||
TEMP_LIST = []
|
||||
STATS_BUFFER_FLUSH_SIZE = 5000
|
||||
STATS_BUFFER_MAX_RETAIN = 10000
|
||||
TEMP_LIST: list[Statistics] = []
|
||||
_STATS_FLUSH_LOCK = asyncio.Lock()
|
||||
driver = get_driver()
|
||||
|
||||
|
||||
async def _flush_statistics_buffer(reason: str) -> int:
|
||||
async with _STATS_FLUSH_LOCK:
|
||||
call_list = TEMP_LIST.copy()
|
||||
TEMP_LIST.clear()
|
||||
if not call_list:
|
||||
return 0
|
||||
try:
|
||||
await Statistics.bulk_create(call_list)
|
||||
except Exception as e:
|
||||
logger.error(f"{reason}批量添加调用记录失败", "定时任务", e=e)
|
||||
retain_count = max(STATS_BUFFER_MAX_RETAIN - len(TEMP_LIST), 0)
|
||||
if retain_count:
|
||||
TEMP_LIST[:0] = call_list[-retain_count:]
|
||||
return 0
|
||||
logger.debug(f"{reason}批量添加调用记录 {len(call_list)} 条", "定时任务")
|
||||
return len(call_list)
|
||||
|
||||
|
||||
async def _append_statistics(record: Statistics) -> None:
|
||||
TEMP_LIST.append(record)
|
||||
if len(TEMP_LIST) >= STATS_BUFFER_FLUSH_SIZE and not _STATS_FLUSH_LOCK.locked():
|
||||
await _flush_statistics_buffer("缓冲区触发")
|
||||
|
||||
|
||||
@run_postprocessor
|
||||
@@ -50,7 +80,7 @@ async def _(
|
||||
if plugin_type == PluginType.NORMAL:
|
||||
entity = get_entity_ids(session)
|
||||
logger.debug(f"提交调用记录: {matcher.plugin_name}...", session=session)
|
||||
TEMP_LIST.append(
|
||||
await _append_statistics(
|
||||
Statistics(
|
||||
user_id=entity.user_id,
|
||||
group_id=entity.group_id,
|
||||
@@ -66,10 +96,11 @@ async def _():
|
||||
try:
|
||||
if should_pause_tasks():
|
||||
return
|
||||
call_list = TEMP_LIST.copy()
|
||||
TEMP_LIST.clear()
|
||||
if call_list:
|
||||
await Statistics.bulk_create(call_list)
|
||||
logger.debug(f"批量添加调用记录 {len(call_list)} 条", "定时任务")
|
||||
await _flush_statistics_buffer("定时")
|
||||
except Exception as e:
|
||||
logger.error("定时批量添加调用记录", "定时任务", e=e)
|
||||
|
||||
|
||||
@driver.on_shutdown
|
||||
async def _flush_statistics_on_shutdown():
|
||||
await _flush_statistics_buffer("关闭")
|
||||
|
||||
Reference in New Issue
Block a user