mirror of
https://github.com/nonebot/nonebot2.git
synced 2026-04-20 08:16:23 +00:00
♻️ add default timeout
This commit is contained in:
@@ -46,6 +46,7 @@ from nonebot.internal.driver import (
|
|||||||
Timeout,
|
Timeout,
|
||||||
TimeoutTypes,
|
TimeoutTypes,
|
||||||
)
|
)
|
||||||
|
from nonebot.log import logger
|
||||||
from nonebot.utils import UNSET, UnsetType, exclude_unset
|
from nonebot.utils import UNSET, UnsetType, exclude_unset
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -328,6 +329,12 @@ class Mixin(HTTPClientMixin, WebSocketClientMixin):
|
|||||||
if setup.ping_interval is not UNSET:
|
if setup.ping_interval is not UNSET:
|
||||||
autoping = setup.ping_interval is not None
|
autoping = setup.ping_interval is not None
|
||||||
|
|
||||||
|
if isinstance(setup.timeout, Timeout) and setup.timeout.ping is not UNSET:
|
||||||
|
logger.warning(
|
||||||
|
"aiohttp driver does not expose a separate ping timeout; "
|
||||||
|
"the configured ping timeout will be ignored."
|
||||||
|
)
|
||||||
|
|
||||||
async with aiohttp.ClientSession(version=version, trust_env=True) as session:
|
async with aiohttp.ClientSession(version=version, trust_env=True) as session:
|
||||||
async with session.ws_connect(
|
async with session.ws_connect(
|
||||||
setup.url,
|
setup.url,
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ class Mixin(WebSocketClientMixin):
|
|||||||
{
|
{
|
||||||
"open_timeout": open_timeout,
|
"open_timeout": open_timeout,
|
||||||
"close_timeout": DEFAULT_TIMEOUT.close,
|
"close_timeout": DEFAULT_TIMEOUT.close,
|
||||||
|
"ping_timeout": DEFAULT_TIMEOUT.ping,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class Timeout:
|
|||||||
ping: float | None | UnsetType = UNSET
|
ping: float | None | UnsetType = UNSET
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_TIMEOUT = Timeout(total=None, connect=5.0, read=30.0, close=10.0)
|
DEFAULT_TIMEOUT = Timeout(total=None, connect=5.0, read=30.0, close=10.0, ping=20.0)
|
||||||
|
|
||||||
|
|
||||||
RawURL: TypeAlias = tuple[bytes, bytes, int | None, bytes]
|
RawURL: TypeAlias = tuple[bytes, bytes, int | None, bytes]
|
||||||
|
|||||||
@@ -878,6 +878,49 @@ async def test_websocket_client_timeout(driver: Driver, server_url: URL):
|
|||||||
await anyio.sleep(1)
|
await anyio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"driver",
|
||||||
|
[
|
||||||
|
pytest.param("nonebot.drivers.websockets:Driver", id="websockets"),
|
||||||
|
pytest.param("nonebot.drivers.aiohttp:Driver", id="aiohttp"),
|
||||||
|
],
|
||||||
|
indirect=True,
|
||||||
|
)
|
||||||
|
async def test_websocket_client_ping_timeout(driver: Driver, server_url: URL):
|
||||||
|
"""WebSocket connections work with different ping_timeout settings."""
|
||||||
|
assert isinstance(driver, WebSocketClientMixin)
|
||||||
|
|
||||||
|
ws_url = server_url.with_scheme("ws")
|
||||||
|
|
||||||
|
# ping timeout not set (UNSET), falls back to DEFAULT_TIMEOUT.ping
|
||||||
|
request = Request("GET", ws_url, timeout=Timeout())
|
||||||
|
async with driver.websocket(request) as ws:
|
||||||
|
await ws.send("quit")
|
||||||
|
with pytest.raises(WebSocketClosed):
|
||||||
|
await ws.receive()
|
||||||
|
|
||||||
|
await anyio.sleep(1)
|
||||||
|
|
||||||
|
# ping timeout explicitly set to None (disable ping timeout)
|
||||||
|
request = Request("GET", ws_url, timeout=Timeout(ping=None))
|
||||||
|
async with driver.websocket(request) as ws:
|
||||||
|
await ws.send("quit")
|
||||||
|
with pytest.raises(WebSocketClosed):
|
||||||
|
await ws.receive()
|
||||||
|
|
||||||
|
await anyio.sleep(1)
|
||||||
|
|
||||||
|
# ping timeout set to a float value
|
||||||
|
request = Request("GET", ws_url, timeout=Timeout(ping=20.0))
|
||||||
|
async with driver.websocket(request) as ws:
|
||||||
|
await ws.send("quit")
|
||||||
|
with pytest.raises(WebSocketClosed):
|
||||||
|
await ws.receive()
|
||||||
|
|
||||||
|
await anyio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"driver",
|
"driver",
|
||||||
|
|||||||
Reference in New Issue
Block a user