mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-01 01:25:07 +08:00
🐛 Remove expiry check in nonebot.adapter.mirai.bot.SessionManager
This commit is contained in:
parent
2836f50e74
commit
371cfd4a0e
@ -1,5 +1,5 @@
|
|||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
from typing import Any, Dict, List, NoReturn, Optional, Tuple, Union
|
from typing import Any, Dict, List, NoReturn, Optional, Tuple, Union
|
||||||
@ -9,7 +9,9 @@ from loguru import logger
|
|||||||
|
|
||||||
from nonebot.adapters import Bot as BaseBot
|
from nonebot.adapters import Bot as BaseBot
|
||||||
from nonebot.config import Config
|
from nonebot.config import Config
|
||||||
from nonebot.drivers import Driver, ReverseDriver, HTTPConnection, HTTPResponse, WebSocket, ForwardDriver, WebSocketSetup
|
from nonebot.drivers import (Driver, ForwardDriver, HTTPConnection,
|
||||||
|
HTTPResponse, ReverseDriver, WebSocket,
|
||||||
|
WebSocketSetup)
|
||||||
from nonebot.exception import ApiNotAvailable
|
from nonebot.exception import ApiNotAvailable
|
||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
|
|
||||||
@ -21,8 +23,7 @@ from .utils import Log, argument_validation, catch_network_error, process_event
|
|||||||
|
|
||||||
class SessionManager:
|
class SessionManager:
|
||||||
"""Bot会话管理器, 提供API主动调用接口"""
|
"""Bot会话管理器, 提供API主动调用接口"""
|
||||||
sessions: Dict[int, Tuple[str, datetime, httpx.AsyncClient]] = {}
|
sessions: Dict[int, Tuple[str, httpx.AsyncClient]] = {}
|
||||||
session_expiry: timedelta = timedelta(minutes=15)
|
|
||||||
|
|
||||||
def __init__(self, session_key: str, client: httpx.AsyncClient):
|
def __init__(self, session_key: str, client: httpx.AsyncClient):
|
||||||
self.session_key, self.client = session_key, client
|
self.session_key, self.client = session_key, client
|
||||||
@ -126,19 +127,15 @@ class SessionManager:
|
|||||||
'qq': self_id
|
'qq': self_id
|
||||||
})
|
})
|
||||||
assert response.json()['code'] == 0
|
assert response.json()['code'] == 0
|
||||||
cls.sessions[self_id] = session_key, datetime.now(), client
|
cls.sessions[self_id] = session_key, client
|
||||||
|
|
||||||
return cls(session_key, client)
|
return cls(session_key, client)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls,
|
def get(cls, self_id: int):
|
||||||
self_id: int,
|
|
||||||
check_expire: bool = True) -> Optional["SessionManager"]:
|
|
||||||
if self_id not in cls.sessions:
|
if self_id not in cls.sessions:
|
||||||
return None
|
return None
|
||||||
key, time, client = cls.sessions[self_id]
|
key, client = cls.sessions[self_id]
|
||||||
if check_expire and (datetime.now() - time > cls.session_expiry):
|
|
||||||
return None
|
|
||||||
return cls(key, client)
|
return cls(key, client)
|
||||||
|
|
||||||
|
|
||||||
@ -165,6 +162,9 @@ class Bot(BaseBot):
|
|||||||
def api(self) -> SessionManager:
|
def api(self) -> SessionManager:
|
||||||
"""返回该Bot对象的会话管理实例以提供API主动调用"""
|
"""返回该Bot对象的会话管理实例以提供API主动调用"""
|
||||||
api = SessionManager.get(self_id=int(self.self_id))
|
api = SessionManager.get(self_id=int(self.self_id))
|
||||||
|
if api is None:
|
||||||
|
if isinstance(self.request, WebSocket):
|
||||||
|
asyncio.create_task(self.request.close(1000))
|
||||||
assert api is not None, 'SessionManager has not been initialized'
|
assert api is not None, 'SessionManager has not been initialized'
|
||||||
return api
|
return api
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user