mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
* perf: reduce memory usage by ~200MB+ at startup
Two key optimizations:
1. Use importlib.util.find_spec() instead of __import__() in dependency
checking. find_spec() only locates modules without executing them,
avoiding loading all 36 dependencies (~222MB) into memory at startup.
2. Introduce shared aiohttp.ClientSession via httpclient module.
Previously, every HTTP request created a new ClientSession, which
creates a new TCPConnector and SSL context, loading system root
certificates each time (~270MB total allocations observed via memray).
Now all HTTP client code reuses shared sessions.
- satori.py and coze_server_api/client.py are left unchanged as they
create one session per adapter lifecycle (not per-request).
Profiling data (memray):
- Peak memory: 403MB
- SSL context creation: 270MB / 6.7M allocations (67% of total)
- Dependency import: 222MB (55% of peak)
- Expected reduction: 150-350MB at startup
* fix: remove unused aiohttp imports (ruff F401)
* style: ruff format
79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
import requests
|
||
from langbot.pkg.utils import httpclient
|
||
|
||
|
||
def post_json(base_url, token, data=None):
|
||
headers = {'Content-Type': 'application/json'}
|
||
|
||
url = base_url + f'?key={token}'
|
||
|
||
try:
|
||
response = requests.post(url, json=data, headers=headers, timeout=60)
|
||
response.raise_for_status()
|
||
result = response.json()
|
||
|
||
if result:
|
||
return result
|
||
else:
|
||
raise RuntimeError(response.text)
|
||
except Exception as e:
|
||
print(f'http请求失败, url={url}, exception={e}')
|
||
raise RuntimeError(str(e))
|
||
|
||
|
||
def get_json(base_url, token):
|
||
headers = {'Content-Type': 'application/json'}
|
||
|
||
url = base_url + f'?key={token}'
|
||
|
||
try:
|
||
response = requests.get(url, headers=headers, timeout=60)
|
||
response.raise_for_status()
|
||
result = response.json()
|
||
|
||
if result:
|
||
return result
|
||
else:
|
||
raise RuntimeError(response.text)
|
||
except Exception as e:
|
||
print(f'http请求失败, url={url}, exception={e}')
|
||
raise RuntimeError(str(e))
|
||
|
||
|
||
async def async_request(
|
||
base_url: str,
|
||
token_key: str,
|
||
method: str = 'POST',
|
||
params: dict = None,
|
||
# headers: dict = None,
|
||
data: dict = None,
|
||
json: dict = None,
|
||
):
|
||
"""
|
||
通用异步请求函数
|
||
|
||
:param base_url: 请求URL
|
||
:param token_key: 请求token
|
||
:param method: HTTP方法 (GET, POST, PUT, DELETE等)
|
||
:param params: URL查询参数
|
||
# :param headers: 请求头
|
||
:param data: 表单数据
|
||
:param json: JSON数据
|
||
:return: 响应文本
|
||
"""
|
||
headers = {'Content-Type': 'application/json'}
|
||
url = f'{base_url}?key={token_key}'
|
||
session = httpclient.get_session()
|
||
async with session.request(
|
||
method=method, url=url, params=params, headers=headers, data=data, json=json
|
||
) as response:
|
||
response.raise_for_status() # 如果状态码不是200,抛出异常
|
||
result = await response.json()
|
||
# print(result)
|
||
return result
|
||
# if result.get('Code') == 200:
|
||
#
|
||
# return await result
|
||
# else:
|
||
# raise RuntimeError("请求失败",response.text)
|