feat(register): 添加资源清理功能并更新依赖

- 在 Register 类中实现 close 方法用于关闭会话、HTTP 客户端和邮箱服务
- 添加 httpx>=0.28.1 到项目依赖列表
- 实现异常处理确保资源释放过程的稳定性
- 为会话管理和 HTTP 客户端添加适当的关闭逻辑
This commit is contained in:
cnlimiter
2026-03-26 00:23:25 +08:00
parent 08327b4425
commit bb3bd2596d
2 changed files with 23 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ dependencies = [
"psycopg[binary]>=3.1.18", "psycopg[binary]>=3.1.18",
"websockets>=16.0", "websockets>=16.0",
"path>=17.1.1", "path>=17.1.1",
"httpx>=0.28.1",
] ]
[project.optional-dependencies] [project.optional-dependencies]

View File

@@ -358,6 +358,28 @@ class RegistrationEngine:
self._log(f"初始化会话失败: {e}", "error") self._log(f"初始化会话失败: {e}", "error")
return False return False
def close(self):
"""关闭注册流程占用的资源"""
if self.session:
try:
self.session.close()
except Exception as e:
self._log(f"关闭注册会话失败: {e}", "warning")
finally:
self.session = None
try:
self.http_client.close()
except Exception as e:
self._log(f"关闭 HTTP 客户端失败: {e}", "warning")
close_email_service = getattr(self.email_service, "close", None)
if callable(close_email_service):
try:
close_email_service()
except Exception as e:
self._log(f"关闭邮箱服务失败: {e}", "warning")
def _get_device_id(self) -> Optional[str]: def _get_device_id(self) -> Optional[str]:
"""获取 Device ID""" """获取 Device ID"""
if not self.oauth_start: if not self.oauth_start: