🚑 移除认证队列机制并放宽消息记录规则 (#2091)

* 🚑 fix(auth_hook):直接调用 auth 并移除认证队列

* 🚑 fix(chat_message): 简化规则函数,移除不必要的命令检查和时间间隔逻辑

---------

Co-authored-by: ATTomatoo <1126160939@qq.com>
This commit is contained in:
ManyManyTomato
2026-02-04 10:46:44 +08:00
committed by GitHub
co-authored by ATTomatoo
parent 8378921c71
commit 662d61a672
2 changed files with 21 additions and 77 deletions
@@ -1,8 +1,7 @@
import asyncio import asyncio
import time import time
from nonebot import get_driver, on_message from nonebot import on_message
from nonebot.adapters import Event
from nonebot.plugin import PluginMetadata from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import UniMsg from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_apscheduler import scheduler from nonebot_plugin_apscheduler import scheduler
@@ -38,43 +37,8 @@ __plugin_meta__ = PluginMetadata(
) )
_COMMAND_STARTS = {str(item) for item in (get_driver().config.command_start or [])} def rule(message: UniMsg) -> bool:
_LAST_GROUP_SAVE: dict[str, float] = {} return bool(Config.get_config("chat_history", "FLAG") and message)
_LAST_USER_SAVE: dict[str, float] = {}
_GROUP_MIN_INTERVAL = 0.5
_USER_MIN_INTERVAL = 0.2
def _is_command_like(text: str) -> bool:
if not text:
return False
for start in _COMMAND_STARTS:
if text.startswith(start):
return True
return False
async def rule(event: Event, message: UniMsg, session: Uninfo) -> bool:
if is_overloaded():
return False
if not Config.get_config("chat_history", "FLAG"):
return False
if not message:
return False
text = message.extract_plain_text().strip()
if _is_command_like(text):
return False
entity = get_entity_ids(session)
now = time.time()
if entity.group_id:
last_group = _LAST_GROUP_SAVE.get(entity.group_id, 0)
if now - last_group < _GROUP_MIN_INTERVAL:
return False
if entity.user_id:
last_user = _LAST_USER_SAVE.get(entity.user_id, 0)
if now - last_user < _USER_MIN_INTERVAL:
return False
return True
chat_history = on_message(rule=rule, priority=1, block=False) chat_history = on_message(rule=rule, priority=1, block=False)
@@ -89,10 +53,6 @@ _DROP_LOG_INTERVAL = 10.0
async def _(message: UniMsg, session: Uninfo): async def _(message: UniMsg, session: Uninfo):
entity = get_entity_ids(session) entity = get_entity_ids(session)
now = time.time() now = time.time()
if entity.group_id:
_LAST_GROUP_SAVE[entity.group_id] = now
if entity.user_id:
_LAST_USER_SAVE[entity.user_id] = now
if is_overloaded(): if is_overloaded():
return return
try: try:
+18 -34
View File
@@ -11,25 +11,20 @@ from nonebot_plugin_uninfo import Uninfo
from zhenxun.services.cache.runtime_cache import is_cache_ready from zhenxun.services.cache.runtime_cache import is_cache_ready
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.services.message_load import is_overloaded, signal_overload from zhenxun.services.message_load import is_overloaded
from zhenxun.utils.utils import get_entity_ids from zhenxun.utils.utils import get_entity_ids
from .auth.config import LOGGER_COMMAND from .auth.config import LOGGER_COMMAND
from .auth.exception import SkipPluginException
from .auth_checker import ( from .auth_checker import (
LimitManager, LimitManager,
_get_event_cache, _get_event_cache,
auth, auth,
auth_ban_fast,
auth_precheck,
route_precheck, route_precheck,
) )
_SKIP_AUTH_PLUGINS = {"chat_history", "chat_message"} _SKIP_AUTH_PLUGINS = {"chat_history", "chat_message"}
_BOT_CONNECT_TS: float | None = None _BOT_CONNECT_TS: float | None = None
_AUTH_QUEUE_MAXSIZE = 200 _AUTH_QUEUE_MAXSIZE = 200
_AUTH_QUEUE_HIGH_WATER = 160
_AUTH_OVERLOAD_WINDOW = 5.0
_AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg]] = asyncio.Queue( _AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg]] = asyncio.Queue(
maxsize=_AUTH_QUEUE_MAXSIZE maxsize=_AUTH_QUEUE_MAXSIZE
) )
@@ -107,41 +102,30 @@ async def _auth_preprocessor(
): ):
if event.get_type() == "message" and not is_cache_ready(): if event.get_type() == "message" and not is_cache_ready():
raise IgnoredException("cache not ready ignore") raise IgnoredException("cache not ready ignore")
if _skip_auth_for_plugin(matcher):
return
start_time = time.time() start_time = time.time()
entity = get_entity_ids(session) entity = get_entity_ids(session)
event_cache = _get_event_cache(event, session, entity) _get_event_cache(event, session, entity)
if await route_precheck(matcher, event, session, message): if await route_precheck(matcher, event, session, message):
return return
try: if _skip_auth_for_plugin(matcher):
await auth_ban_fast(matcher, event, bot, session)
except SkipPluginException as exc:
logger.info(str(exc), LOGGER_COMMAND, session=session)
raise IgnoredException("ban fast ignore") from exc
try:
await auth_precheck(matcher, event, bot, session, message)
except SkipPluginException as exc:
logger.info(str(exc), LOGGER_COMMAND, session=session)
raise IgnoredException("precheck ignore") from exc
if event_cache is not None and event_cache.get("route_skip") is True:
if not is_overloaded():
logger.debug("route miss skip auth task", LOGGER_COMMAND)
return return
try: try:
_AUTH_QUEUE.put_nowait((matcher, event, bot, session, message)) await auth(
except asyncio.QueueFull: matcher,
signal_overload(_AUTH_OVERLOAD_WINDOW) event,
now = time.monotonic() bot,
global _LAST_DROP_LOG session,
if now - _LAST_DROP_LOG > 1.0: message,
_LAST_DROP_LOG = now skip_ban=False,
logger.warning("auth queue full, skip auth task", LOGGER_COMMAND) )
return except IgnoredException:
if _AUTH_QUEUE.qsize() >= _AUTH_QUEUE_HIGH_WATER: raise
signal_overload(_AUTH_OVERLOAD_WINDOW) except Exception as exc:
logger.error("auth check failed", LOGGER_COMMAND, e=exc)
raise IgnoredException("auth failed") from exc
now = time.monotonic() now = time.monotonic()
last_log = getattr(_auth_preprocessor, "_last_log", 0.0) last_log = getattr(_auth_preprocessor, "_last_log", 0.0)
if now - last_log > 1.0 and not is_overloaded(): if now - last_log > 1.0 and not is_overloaded():