Skip to content

Commit 662d61a

Browse files
ATTomatooATTomatoo
andauthored
🚑 移除认证队列机制并放宽消息记录规则 (#2091)
* 🚑 fix(auth_hook):直接调用 auth 并移除认证队列 * 🚑 fix(chat_message): 简化规则函数,移除不必要的命令检查和时间间隔逻辑 --------- Co-authored-by: ATTomatoo <1126160939@qq.com>
1 parent 8378921 commit 662d61a

2 files changed

Lines changed: 21 additions & 77 deletions

File tree

zhenxun/builtin_plugins/chat_history/chat_message.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import asyncio
22
import time
33

4-
from nonebot import get_driver, on_message
5-
from nonebot.adapters import Event
4+
from nonebot import on_message
65
from nonebot.plugin import PluginMetadata
76
from nonebot_plugin_alconna import UniMsg
87
from nonebot_plugin_apscheduler import scheduler
@@ -38,43 +37,8 @@
3837
)
3938

4039

41-
_COMMAND_STARTS = {str(item) for item in (get_driver().config.command_start or [])}
42-
_LAST_GROUP_SAVE: dict[str, float] = {}
43-
_LAST_USER_SAVE: dict[str, float] = {}
44-
_GROUP_MIN_INTERVAL = 0.5
45-
_USER_MIN_INTERVAL = 0.2
46-
47-
48-
def _is_command_like(text: str) -> bool:
49-
if not text:
50-
return False
51-
for start in _COMMAND_STARTS:
52-
if text.startswith(start):
53-
return True
54-
return False
55-
56-
57-
async def rule(event: Event, message: UniMsg, session: Uninfo) -> bool:
58-
if is_overloaded():
59-
return False
60-
if not Config.get_config("chat_history", "FLAG"):
61-
return False
62-
if not message:
63-
return False
64-
text = message.extract_plain_text().strip()
65-
if _is_command_like(text):
66-
return False
67-
entity = get_entity_ids(session)
68-
now = time.time()
69-
if entity.group_id:
70-
last_group = _LAST_GROUP_SAVE.get(entity.group_id, 0)
71-
if now - last_group < _GROUP_MIN_INTERVAL:
72-
return False
73-
if entity.user_id:
74-
last_user = _LAST_USER_SAVE.get(entity.user_id, 0)
75-
if now - last_user < _USER_MIN_INTERVAL:
76-
return False
77-
return True
40+
def rule(message: UniMsg) -> bool:
41+
return bool(Config.get_config("chat_history", "FLAG") and message)
7842

7943

8044
chat_history = on_message(rule=rule, priority=1, block=False)
@@ -89,10 +53,6 @@ async def rule(event: Event, message: UniMsg, session: Uninfo) -> bool:
8953
async def _(message: UniMsg, session: Uninfo):
9054
entity = get_entity_ids(session)
9155
now = time.time()
92-
if entity.group_id:
93-
_LAST_GROUP_SAVE[entity.group_id] = now
94-
if entity.user_id:
95-
_LAST_USER_SAVE[entity.user_id] = now
9656
if is_overloaded():
9757
return
9858
try:

zhenxun/builtin_plugins/hooks/auth_hook.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@
1111

1212
from zhenxun.services.cache.runtime_cache import is_cache_ready
1313
from zhenxun.services.log import logger
14-
from zhenxun.services.message_load import is_overloaded, signal_overload
14+
from zhenxun.services.message_load import is_overloaded
1515
from zhenxun.utils.utils import get_entity_ids
1616

1717
from .auth.config import LOGGER_COMMAND
18-
from .auth.exception import SkipPluginException
1918
from .auth_checker import (
2019
LimitManager,
2120
_get_event_cache,
2221
auth,
23-
auth_ban_fast,
24-
auth_precheck,
2522
route_precheck,
2623
)
2724

2825
_SKIP_AUTH_PLUGINS = {"chat_history", "chat_message"}
2926
_BOT_CONNECT_TS: float | None = None
3027
_AUTH_QUEUE_MAXSIZE = 200
31-
_AUTH_QUEUE_HIGH_WATER = 160
32-
_AUTH_OVERLOAD_WINDOW = 5.0
3328
_AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg]] = asyncio.Queue(
3429
maxsize=_AUTH_QUEUE_MAXSIZE
3530
)
@@ -107,41 +102,30 @@ async def _auth_preprocessor(
107102
):
108103
if event.get_type() == "message" and not is_cache_ready():
109104
raise IgnoredException("cache not ready ignore")
110-
if _skip_auth_for_plugin(matcher):
111-
return
112105
start_time = time.time()
113106
entity = get_entity_ids(session)
114-
event_cache = _get_event_cache(event, session, entity)
107+
_get_event_cache(event, session, entity)
108+
115109
if await route_precheck(matcher, event, session, message):
116110
return
117-
try:
118-
await auth_ban_fast(matcher, event, bot, session)
119-
except SkipPluginException as exc:
120-
logger.info(str(exc), LOGGER_COMMAND, session=session)
121-
raise IgnoredException("ban fast ignore") from exc
122-
try:
123-
await auth_precheck(matcher, event, bot, session, message)
124-
except SkipPluginException as exc:
125-
logger.info(str(exc), LOGGER_COMMAND, session=session)
126-
raise IgnoredException("precheck ignore") from exc
127-
128-
if event_cache is not None and event_cache.get("route_skip") is True:
129-
if not is_overloaded():
130-
logger.debug("route miss skip auth task", LOGGER_COMMAND)
111+
if _skip_auth_for_plugin(matcher):
131112
return
132113

133114
try:
134-
_AUTH_QUEUE.put_nowait((matcher, event, bot, session, message))
135-
except asyncio.QueueFull:
136-
signal_overload(_AUTH_OVERLOAD_WINDOW)
137-
now = time.monotonic()
138-
global _LAST_DROP_LOG
139-
if now - _LAST_DROP_LOG > 1.0:
140-
_LAST_DROP_LOG = now
141-
logger.warning("auth queue full, skip auth task", LOGGER_COMMAND)
142-
return
143-
if _AUTH_QUEUE.qsize() >= _AUTH_QUEUE_HIGH_WATER:
144-
signal_overload(_AUTH_OVERLOAD_WINDOW)
115+
await auth(
116+
matcher,
117+
event,
118+
bot,
119+
session,
120+
message,
121+
skip_ban=False,
122+
)
123+
except IgnoredException:
124+
raise
125+
except Exception as exc:
126+
logger.error("auth check failed", LOGGER_COMMAND, e=exc)
127+
raise IgnoredException("auth failed") from exc
128+
145129
now = time.monotonic()
146130
last_log = getattr(_auth_preprocessor, "_last_log", 0.0)
147131
if now - last_log > 1.0 and not is_overloaded():

0 commit comments

Comments
 (0)