mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
perf: 优化 Provider 预加载和 bcrypt 密码验证性能
- 使用 selectinload 预加载 endpoints 避免 N+1 查询 - 将 bcrypt 密码验证移至线程池执行,避免阻塞事件循环
This commit is contained in:
@@ -34,7 +34,7 @@ from src.plugins.manager import get_plugin_manager
|
||||
|
||||
async def initialize_providers():
|
||||
"""从数据库初始化提供商(仅用于日志记录)"""
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
||||
from src.database.database import create_session
|
||||
from src.models.database import Provider
|
||||
@@ -44,9 +44,10 @@ async def initialize_providers():
|
||||
db: Session = create_session()
|
||||
|
||||
try:
|
||||
# 从数据库加载所有活跃的提供商
|
||||
# 从数据库加载所有活跃的提供商(使用 selectinload 预加载 endpoints 避免 N+1 查询)
|
||||
providers = (
|
||||
db.query(Provider)
|
||||
.options(selectinload(Provider.endpoints))
|
||||
.filter(Provider.is_active.is_(True))
|
||||
.order_by(Provider.provider_priority.asc())
|
||||
.all()
|
||||
|
||||
@@ -266,7 +266,8 @@ class AuthService:
|
||||
logger.warning(f"登录失败 - 该用户使用 LDAP 认证: {email}")
|
||||
return None
|
||||
|
||||
if not user.verify_password(password):
|
||||
# 在线程池中执行 bcrypt 密码验证,避免阻塞事件循环
|
||||
if not await run_in_threadpool(user.verify_password, password):
|
||||
logger.warning(f"登录失败 - 密码错误: {email}")
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user