🐛 Fix: 修正 http/websocket client timeout (#3923)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
StarHeart
2026-03-31 20:56:12 +08:00
committed by GitHub
parent cf8127ee4d
commit cbe6eee868
10 changed files with 443 additions and 72 deletions

View File

@@ -9,6 +9,7 @@ from .abstract import ReverseDriver as ReverseDriver
from .abstract import ReverseMixin as ReverseMixin
from .abstract import WebSocketClientMixin as WebSocketClientMixin
from .combine import combine_driver as combine_driver
from .model import DEFAULT_TIMEOUT as DEFAULT_TIMEOUT
from .model import URL as URL
from .model import ContentTypes as ContentTypes
from .model import Cookies as Cookies

View File

@@ -19,7 +19,13 @@ from nonebot.typing import (
T_BotDisconnectionHook,
T_DependencyCache,
)
from nonebot.utils import escape_tag, flatten_exception_group, run_coro_with_catch
from nonebot.utils import (
UNSET,
UnsetType,
escape_tag,
flatten_exception_group,
run_coro_with_catch,
)
from ._lifespan import LIFESPAN_FUNC, Lifespan
from .model import (
@@ -246,7 +252,7 @@ class HTTPClientSession(abc.ABC):
headers: HeaderTypes = None,
cookies: CookieTypes = None,
version: str | HTTPVersion = HTTPVersion.H11,
timeout: TimeoutTypes = None,
timeout: TimeoutTypes | UnsetType = UNSET,
proxy: str | None = None,
):
raise NotImplementedError

View File

@@ -9,14 +9,20 @@ import urllib.request
from multidict import CIMultiDict
from yarl import URL as URL
from nonebot.utils import UNSET, UnsetType
@dataclass
class Timeout:
"""Request 超时配置。"""
total: float | None = None
connect: float | None = None
read: float | None = None
total: float | None | UnsetType = UNSET
connect: float | None | UnsetType = UNSET
read: float | None | UnsetType = UNSET
close: float | None | UnsetType = UNSET
DEFAULT_TIMEOUT = Timeout(total=None, connect=5.0, read=30.0, close=10.0)
RawURL: TypeAlias = tuple[bytes, bytes, int | None, bytes]
@@ -68,7 +74,7 @@ class Request:
json: Any = None,
files: FilesTypes = None,
version: str | HTTPVersion = HTTPVersion.H11,
timeout: TimeoutTypes = None,
timeout: TimeoutTypes | UnsetType = UNSET,
proxy: str | None = None,
):
# method
@@ -80,7 +86,7 @@ class Request:
# http version
self.version: HTTPVersion = HTTPVersion(version)
# timeout
self.timeout: TimeoutTypes = timeout
self.timeout: TimeoutTypes | UnsetType = timeout
# proxy
self.proxy: str | None = proxy