mirror of
https://github.com/fawney19/Aether.git
synced 2026-01-03 00:02:28 +08:00
refactor: 统一 HTTP 客户端超时配置
将 HTTPClientPool 中硬编码的超时参数改为使用可配置的环境变量,提高系统的灵活性和可维护性。 - 添加 HTTP_READ_TIMEOUT 环境变量配置(默认 300 秒) - 统一所有 HTTP 客户端创建逻辑使用配置化超时 - 改进变量命名清晰性(config -> default_config 或 client_config)
This commit is contained in:
@@ -9,6 +9,7 @@ from urllib.parse import quote, urlparse
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
from src.config import config
|
||||||
from src.core.logger import logger
|
from src.core.logger import logger
|
||||||
|
|
||||||
|
|
||||||
@@ -83,10 +84,10 @@ class HTTPClientPool:
|
|||||||
http2=False, # 暂时禁用HTTP/2以提高兼容性
|
http2=False, # 暂时禁用HTTP/2以提高兼容性
|
||||||
verify=True, # 启用SSL验证
|
verify=True, # 启用SSL验证
|
||||||
timeout=httpx.Timeout(
|
timeout=httpx.Timeout(
|
||||||
connect=10.0, # 连接超时
|
connect=config.http_connect_timeout,
|
||||||
read=300.0, # 读取超时(5分钟,适合流式响应)
|
read=config.http_read_timeout,
|
||||||
write=60.0, # 写入超时(60秒,支持大请求体)
|
write=config.http_write_timeout,
|
||||||
pool=5.0, # 连接池超时
|
pool=config.http_pool_timeout,
|
||||||
),
|
),
|
||||||
limits=httpx.Limits(
|
limits=httpx.Limits(
|
||||||
max_connections=100, # 最大连接数
|
max_connections=100, # 最大连接数
|
||||||
@@ -111,15 +112,20 @@ class HTTPClientPool:
|
|||||||
"""
|
"""
|
||||||
if name not in cls._clients:
|
if name not in cls._clients:
|
||||||
# 合并默认配置和自定义配置
|
# 合并默认配置和自定义配置
|
||||||
config = {
|
default_config = {
|
||||||
"http2": False,
|
"http2": False,
|
||||||
"verify": True,
|
"verify": True,
|
||||||
"timeout": httpx.Timeout(10.0, read=300.0),
|
"timeout": httpx.Timeout(
|
||||||
|
connect=config.http_connect_timeout,
|
||||||
|
read=config.http_read_timeout,
|
||||||
|
write=config.http_write_timeout,
|
||||||
|
pool=config.http_pool_timeout,
|
||||||
|
),
|
||||||
"follow_redirects": True,
|
"follow_redirects": True,
|
||||||
}
|
}
|
||||||
config.update(kwargs)
|
default_config.update(kwargs)
|
||||||
|
|
||||||
cls._clients[name] = httpx.AsyncClient(**config)
|
cls._clients[name] = httpx.AsyncClient(**default_config)
|
||||||
logger.debug(f"创建命名HTTP客户端: {name}")
|
logger.debug(f"创建命名HTTP客户端: {name}")
|
||||||
|
|
||||||
return cls._clients[name]
|
return cls._clients[name]
|
||||||
@@ -151,14 +157,19 @@ class HTTPClientPool:
|
|||||||
async with HTTPClientPool.get_temp_client() as client:
|
async with HTTPClientPool.get_temp_client() as client:
|
||||||
response = await client.get('https://example.com')
|
response = await client.get('https://example.com')
|
||||||
"""
|
"""
|
||||||
config = {
|
default_config = {
|
||||||
"http2": False,
|
"http2": False,
|
||||||
"verify": True,
|
"verify": True,
|
||||||
"timeout": httpx.Timeout(10.0),
|
"timeout": httpx.Timeout(
|
||||||
|
connect=config.http_connect_timeout,
|
||||||
|
read=config.http_read_timeout,
|
||||||
|
write=config.http_write_timeout,
|
||||||
|
pool=config.http_pool_timeout,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
config.update(kwargs)
|
default_config.update(kwargs)
|
||||||
|
|
||||||
client = httpx.AsyncClient(**config)
|
client = httpx.AsyncClient(**default_config)
|
||||||
try:
|
try:
|
||||||
yield client
|
yield client
|
||||||
finally:
|
finally:
|
||||||
@@ -182,25 +193,30 @@ class HTTPClientPool:
|
|||||||
Returns:
|
Returns:
|
||||||
配置好的 httpx.AsyncClient 实例
|
配置好的 httpx.AsyncClient 实例
|
||||||
"""
|
"""
|
||||||
config: Dict[str, Any] = {
|
client_config: Dict[str, Any] = {
|
||||||
"http2": False,
|
"http2": False,
|
||||||
"verify": True,
|
"verify": True,
|
||||||
"follow_redirects": True,
|
"follow_redirects": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
if timeout:
|
if timeout:
|
||||||
config["timeout"] = timeout
|
client_config["timeout"] = timeout
|
||||||
else:
|
else:
|
||||||
config["timeout"] = httpx.Timeout(10.0, read=300.0)
|
client_config["timeout"] = httpx.Timeout(
|
||||||
|
connect=config.http_connect_timeout,
|
||||||
|
read=config.http_read_timeout,
|
||||||
|
write=config.http_write_timeout,
|
||||||
|
pool=config.http_pool_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
# 添加代理配置
|
# 添加代理配置
|
||||||
proxy_url = build_proxy_url(proxy_config) if proxy_config else None
|
proxy_url = build_proxy_url(proxy_config) if proxy_config else None
|
||||||
if proxy_url:
|
if proxy_url:
|
||||||
config["proxy"] = proxy_url
|
client_config["proxy"] = proxy_url
|
||||||
logger.debug(f"创建带代理的HTTP客户端: {proxy_config.get('url', 'unknown')}")
|
logger.debug(f"创建带代理的HTTP客户端: {proxy_config.get('url', 'unknown')}")
|
||||||
|
|
||||||
config.update(kwargs)
|
client_config.update(kwargs)
|
||||||
return httpx.AsyncClient(**config)
|
return httpx.AsyncClient(**client_config)
|
||||||
|
|
||||||
|
|
||||||
# 便捷访问函数
|
# 便捷访问函数
|
||||||
|
|||||||
@@ -148,6 +148,7 @@ class Config:
|
|||||||
|
|
||||||
# HTTP 请求超时配置(秒)
|
# HTTP 请求超时配置(秒)
|
||||||
self.http_connect_timeout = float(os.getenv("HTTP_CONNECT_TIMEOUT", "10.0"))
|
self.http_connect_timeout = float(os.getenv("HTTP_CONNECT_TIMEOUT", "10.0"))
|
||||||
|
self.http_read_timeout = float(os.getenv("HTTP_READ_TIMEOUT", "300.0"))
|
||||||
self.http_write_timeout = float(os.getenv("HTTP_WRITE_TIMEOUT", "60.0"))
|
self.http_write_timeout = float(os.getenv("HTTP_WRITE_TIMEOUT", "60.0"))
|
||||||
self.http_pool_timeout = float(os.getenv("HTTP_POOL_TIMEOUT", "10.0"))
|
self.http_pool_timeout = float(os.getenv("HTTP_POOL_TIMEOUT", "10.0"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user