优化: 登录有效期7天/数据库连接池增强
This commit is contained in:
parent
84cd53143b
commit
1fe912ac76
|
|
@ -13,7 +13,7 @@ from app.models.models import User
|
||||||
# 配置
|
# 配置
|
||||||
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key-change-in-production")
|
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key-change-in-production")
|
||||||
ALGORITHM = "HS256"
|
ALGORITHM = "HS256"
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60"))
|
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "10080")) # 7天
|
||||||
|
|
||||||
# HTTP Bearer 认证
|
# HTTP Bearer 认证
|
||||||
security = HTTPBearer(auto_error=False)
|
security = HTTPBearer(auto_error=False)
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,69 @@
|
||||||
import os
|
import os
|
||||||
from sqlalchemy import create_engine
|
import time
|
||||||
|
from sqlalchemy import create_engine, event
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from sqlalchemy.pool import QueuePool
|
||||||
|
from typing import Generator
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
DATABASE_URL = os.getenv(
|
DATABASE_URL = os.getenv(
|
||||||
"DATABASE_URL",
|
"DATABASE_URL",
|
||||||
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 增强版数据库引擎配置
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
DATABASE_URL,
|
DATABASE_URL,
|
||||||
pool_pre_ping=True,
|
# 连接池配置
|
||||||
pool_size=20,
|
poolclass=QueuePool,
|
||||||
max_overflow=40,
|
pool_size=20, # 常规连接数
|
||||||
pool_recycle=3600,
|
max_overflow=40, # 允许超出的连接数(高并发时)
|
||||||
pool_timeout=30,
|
pool_timeout=30, # 获取连接超时时间(秒)
|
||||||
echo=False
|
pool_recycle=1800, # 连接回收时间(30分钟),避免连接过期
|
||||||
|
pool_pre_ping=True, # 每次获取连接前检查连接是否有效
|
||||||
|
echo=False,
|
||||||
|
# 连接参数优化
|
||||||
|
connect_args={
|
||||||
|
"connect_timeout": 10,
|
||||||
|
"application_name": "zodiac-api",
|
||||||
|
"options": "-c statement_timeout=30000" # 查询超时30秒
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 添加连接事件监听器
|
||||||
|
@event.listens_for(engine, "connect")
|
||||||
|
def set_connect_timeout(dbapi_conn, connection_record):
|
||||||
|
"""设置连接参数"""
|
||||||
|
cursor = dbapi_conn.cursor()
|
||||||
|
cursor.execute("SET statement_timeout = 30000")
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
@event.listens_for(engine, "checkout")
|
||||||
|
def check_connection(dbapi_conn, connection_record, connection_proxy):
|
||||||
|
"""检出连接时检查"""
|
||||||
|
try:
|
||||||
|
cursor = dbapi_conn.cursor()
|
||||||
|
cursor.execute("SELECT 1")
|
||||||
|
cursor.close()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"连接检查失败: {e}")
|
||||||
|
raise Exception("数据库连接无效")
|
||||||
|
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
|
def get_db() -> Generator:
|
||||||
def get_db():
|
"""获取数据库会话,带错误处理"""
|
||||||
db = SessionLocal(expire_on_commit=False)
|
db = SessionLocal(expire_on_commit=False)
|
||||||
try:
|
try:
|
||||||
yield db
|
yield db
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"数据库会话错误: {e}")
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,15 @@ def get_users(
|
||||||
"role": u.role,
|
"role": u.role,
|
||||||
"user_code": u.user_code,
|
"user_code": u.user_code,
|
||||||
"created_at": u.f99_92_created_at.isoformat() if u.f99_92_created_at else None,
|
"created_at": u.f99_92_created_at.isoformat() if u.f99_92_created_at else None,
|
||||||
"collection_count": count
|
"collectionCount": count,
|
||||||
|
"level": u.f99_94_level,
|
||||||
|
"aiCount": u.f99_95_ai_count,
|
||||||
|
"searchCount": u.f99_96_search_count,
|
||||||
|
"loginCount": u.f99_98_login_count,
|
||||||
|
"points": u.f99_100_points,
|
||||||
|
"balance": float(u.f01_11_balance) if u.f01_11_balance else 0,
|
||||||
|
"totalAmount": float(u.f01_12_total_amount) if u.f01_12_total_amount else 0,
|
||||||
|
"phoneVerified": u.f01_06_phone_verified,
|
||||||
})
|
})
|
||||||
|
|
||||||
return user_list
|
return user_list
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue