Skip to content

Commit

Permalink
feat: corporate with aiohttp 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
lumina37 committed Nov 17, 2024
1 parent bc6d313 commit 1dc4c28
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
36 changes: 11 additions & 25 deletions aiotieba/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TimeoutConfig:
http_keepalive (float, optional): http长连接的保持时间. Defaults to 30.0.
ws_send (float, optional): websocket发送数据的超时时间. Defaults to 3.0.
ws_read (float, optional): 从发送websocket数据到结束等待响应的超时时间. Defaults to 8.0.
ws_close (float, optional): 等待websocket终止连接的时间. Defaults to 10.0.
ws_keepalive (float, optional): websocket在长达ws_keepalive的时间内未发生IO则发送close信号关闭连接. Defaults to 300.0.
ws_heartbeat (float, optional): websocket心跳间隔. 为None则不发送心跳. Defaults to None.
dns_ttl (int, optional): dns的本地缓存超时时间. Defaults to 600.
Expand All @@ -55,38 +56,23 @@ class TimeoutConfig:
所有时间均以秒为单位
"""

http: aiohttp.ClientTimeout = dcs.field(default_factory=aiohttp.ClientTimeout)
http_acquire_conn: float = 4.0
http_read: float = 12.0
http_connect: float = 3.0
http_keepalive: float = 30.0
ws_send: float = 3.0
ws_read: float = 8.0
ws_close: float = 10.0
ws_keepalive: float = 300.0
ws_heartbeat: float | None = None
dns_ttl: int = 600

def __init__(
self,
http_acquire_conn: float = 4.0,
http_read: float = 12.0,
http_connect: float = 3.0,
http_keepalive: float = 30.0,
ws_send: float = 3.0,
ws_read: float = 8.0,
ws_keepalive: float = 300.0,
ws_heartbeat: float | None = None,
dns_ttl: int = 600,
) -> None:
self.http = aiohttp.ClientTimeout(connect=http_acquire_conn, sock_read=http_read, sock_connect=http_connect)
self.http_keepalive = http_keepalive
self.ws_send = ws_send
self.ws_read = ws_read
self.ws_keepalive = ws_keepalive
self.ws_heartbeat = ws_heartbeat
self.dns_ttl = dns_ttl

@property
def http_read(self) -> float:
return self.http.sock_read
def http_timeout(self) -> aiohttp.ClientTimeout:
return aiohttp.ClientTimeout(
connect=self.http_acquire_conn, sock_read=self.http_read, sock_connect=self.http_connect
)

@property
def http_connect(self) -> float:
return self.http.sock_connect
def ws_timeout(self) -> aiohttp.ClientWSTimeout:
return aiohttp.ClientWSTimeout(self.ws_read, self.ws_close)
2 changes: 1 addition & 1 deletion aiotieba/core/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def req2res(
# 获取TCP连接
try:
async with timeout(self.timeout.http_connect, self.connector._loop):
conn = await self.connector.connect(request, [], self.timeout.http)
conn = await self.connector.connect(request, [], self.timeout.http_timeout)
except asyncio.TimeoutError as exc:
raise aiohttp.ServerTimeoutError(f"Connection timeout to host {request.url}") from exc

Expand Down
10 changes: 5 additions & 5 deletions aiotieba/core/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Callable

import aiohttp
import aiohttp.client_ws
import yarl
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import algorithms
Expand Down Expand Up @@ -336,9 +337,9 @@ async def connect(self) -> None:
conn = response.connection
conn_proto = conn.protocol
transport = conn.transport
reader = aiohttp.FlowControlDataQueue(conn_proto, 1 << 16, loop=self.loop)
conn_proto.set_parser(aiohttp.http.WebSocketReader(reader, 4 * 1024 * 1024), reader)
writer = aiohttp.http.WebSocketWriter(conn_proto, transport, use_mask=True)
reader = aiohttp.client.WebSocketDataQueue(conn_proto, 1 << 16, loop=self.loop)
conn_proto.set_parser(aiohttp.client.WebSocketReader(reader, 4 * 1024 * 1024), reader)
writer = aiohttp.client.WebSocketWriter(conn_proto, transport, use_mask=True)
except BaseException:
response.close()
raise
Expand All @@ -348,11 +349,10 @@ async def connect(self) -> None:
writer,
'chat',
response,
self.net_core.timeout.ws_keepalive,
self.net_core.timeout.ws_timeout,
True,
True,
self.loop,
receive_timeout=self.net_core.timeout.ws_read,
heartbeat=self.net_core.timeout.ws_heartbeat,
)

Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "aiotieba"
version = "4.5.0a5"
version = "4.5.0a6"
description = "Asynchronous I/O Client for Baidu Tieba"
authors = [{ name = "lumina37", email = "[email protected]" }]
urls = { Repository = "https://github.com/lumina37/aiotieba/", Documentation = "https://aiotieba.cc/" }
Expand All @@ -21,9 +21,9 @@ classifiers = [
]
requires-python = ">=3.9,<3.14"
dependencies = [
"aiohttp>=3.10.0,<4;python_version>='3.9' and python_version<'3.12'",
"aiohttp>=3.10.0,<4;python_version=='3.12'",
"aiohttp>=3.10.5,<4;python_version>='3.13'",
"aiohttp>=3.11.0,<4;python_version>='3.9' and python_version<'3.12'",
"aiohttp>=3.11.0,<4;python_version=='3.12'",
"aiohttp>=3.11.0,<4;python_version>='3.13'",
"beautifulsoup4>=4.5.2,<5;python_version=='3.9'",
"beautifulsoup4>=4.7.1,<5;python_version>='3.10'",
"lxml>=4.6.0,<6;python_version=='3.9'",
Expand Down

0 comments on commit 1dc4c28

Please sign in to comment.