2024-01-26 15:51:49 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
import mirai
|
|
|
|
|
|
2024-02-23 17:46:22 +08:00
|
|
|
|
from ..core import entities
|
2024-02-11 23:07:38 +08:00
|
|
|
|
from ..platform import adapter as msadapter
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QueryPool:
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""请求池,请求获得调度进入pipeline之前,保存在这里"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
query_id_counter: int = 0
|
|
|
|
|
|
|
|
|
|
pool_lock: asyncio.Lock
|
|
|
|
|
|
|
|
|
|
queries: list[entities.Query]
|
|
|
|
|
|
|
|
|
|
condition: asyncio.Condition
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.query_id_counter = 0
|
|
|
|
|
self.pool_lock = asyncio.Lock()
|
|
|
|
|
self.queries = []
|
|
|
|
|
self.condition = asyncio.Condition(self.pool_lock)
|
|
|
|
|
|
|
|
|
|
async def add_query(
|
|
|
|
|
self,
|
|
|
|
|
launcher_type: entities.LauncherTypes,
|
|
|
|
|
launcher_id: int,
|
|
|
|
|
sender_id: int,
|
|
|
|
|
message_event: mirai.MessageEvent,
|
2024-02-11 23:07:38 +08:00
|
|
|
|
message_chain: mirai.MessageChain,
|
|
|
|
|
adapter: msadapter.MessageSourceAdapter
|
2024-01-26 15:51:49 +08:00
|
|
|
|
) -> entities.Query:
|
|
|
|
|
async with self.condition:
|
|
|
|
|
query = entities.Query(
|
|
|
|
|
query_id=self.query_id_counter,
|
|
|
|
|
launcher_type=launcher_type,
|
|
|
|
|
launcher_id=launcher_id,
|
|
|
|
|
sender_id=sender_id,
|
|
|
|
|
message_event=message_event,
|
2024-02-01 15:48:26 +08:00
|
|
|
|
message_chain=message_chain,
|
|
|
|
|
resp_messages=[],
|
2024-05-14 23:08:49 +08:00
|
|
|
|
resp_message_chain=[],
|
2024-02-11 23:07:38 +08:00
|
|
|
|
adapter=adapter
|
2024-01-26 15:51:49 +08:00
|
|
|
|
)
|
|
|
|
|
self.queries.append(query)
|
|
|
|
|
self.query_id_counter += 1
|
|
|
|
|
self.condition.notify_all()
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self):
|
|
|
|
|
await self.pool_lock.acquire()
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
|
self.pool_lock.release()
|