Merge pull request 'fix: 数据库连接池添加超时配置,优化用户列表查询性能' (#6) from fix/db-pool into main
Reviewed-on: http://47.253.189.47:3000/coolbot/jiachenlong/pulls/6
This commit is contained in:
commit
fcb8108734
|
|
@ -5,8 +5,8 @@ from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
# 支持 MySQL, PostgreSQL
|
# 支持 MySQL, PostgreSQL
|
||||||
DATABASE_URL = os.getenv(
|
DATABASE_URL = os.getenv(
|
||||||
"DATABASE_URL",
|
DATABASE_URL,
|
||||||
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
postgresql://postgres:postgres@localhost:5432/zodiac
|
||||||
)
|
)
|
||||||
|
|
||||||
# 数据库引擎配置
|
# 数据库引擎配置
|
||||||
|
|
@ -15,6 +15,8 @@ engine = create_engine(
|
||||||
pool_pre_ping=True,
|
pool_pre_ping=True,
|
||||||
pool_size=10,
|
pool_size=10,
|
||||||
max_overflow=20,
|
max_overflow=20,
|
||||||
|
pool_recycle=3600, # 1小时回收连接
|
||||||
|
pool_timeout=30, # 获取连接超时30秒
|
||||||
echo=False
|
echo=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,34 +5,32 @@ import string
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# 加载环境变量
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
# 阿里云短信配置
|
# 阿里云短信配置
|
||||||
SMS_CONFIG = {
|
SMS_CONFIG = {
|
||||||
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5t6HUnpFBLEK9194kPVG"),
|
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5tQAx5niD7JQVqGE5acE"),
|
||||||
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "LEr4Q8yRxb8D5b24cKfCwlt4MMoke1"),
|
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "QsQFAEKBkaNynIoKyvdIi3BUyWVZu1"),
|
||||||
"sign_name": "阿里云",
|
"sign_name": os.getenv("SMS_SIGN_NAME", "苏州算力"),
|
||||||
"template_code": "100001",
|
"template_code": os.getenv("SMS_TEMPLATE_CODE", "SMS_501590956"),
|
||||||
}
|
}
|
||||||
|
|
||||||
# 验证码缓存(生产环境建议用Redis)
|
# 验证码缓存
|
||||||
# 格式: { phone: { code: "123456", expire: 1234567890 } }
|
|
||||||
VERIFICATION_CODES = {}
|
VERIFICATION_CODES = {}
|
||||||
|
|
||||||
|
|
||||||
def generate_code(length: int = 6) -> str:
|
def generate_code(length: int = 6) -> str:
|
||||||
"""生成6位数字验证码"""
|
|
||||||
return ''.join(random.choices(string.digits, k=length))
|
return ''.join(random.choices(string.digits, k=length))
|
||||||
|
|
||||||
|
|
||||||
def send_verification_code(phone: str) -> dict:
|
def send_verification_code(phone: str) -> dict:
|
||||||
"""发送短信验证码"""
|
|
||||||
from alibabacloud_dysmsapi20170525 import models
|
from alibabacloud_dysmsapi20170525 import models
|
||||||
from alibabacloud_dysmsapi20170525.client import Client
|
from alibabacloud_dysmsapi20170525.client import Client
|
||||||
from alibabacloud_tea_openapi import models as open_models
|
from alibabacloud_tea_openapi import models as open_models
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 生成验证码
|
code = generate_code()
|
||||||
code = generate_code(6)
|
|
||||||
|
|
||||||
# 配置客户端
|
# 配置客户端
|
||||||
config = open_models.Config(
|
config = open_models.Config(
|
||||||
|
|
@ -57,10 +55,9 @@ def send_verification_code(phone: str) -> dict:
|
||||||
|
|
||||||
# 检查结果
|
# 检查结果
|
||||||
if response.body.code == "OK":
|
if response.body.code == "OK":
|
||||||
# 保存验证码
|
|
||||||
VERIFICATION_CODES[phone] = {
|
VERIFICATION_CODES[phone] = {
|
||||||
"code": code,
|
"code": code,
|
||||||
"expire": int(time.time()) + 300 # 5分钟有效
|
"expire": int(time.time()) + 300
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
|
|
@ -72,35 +69,23 @@ def send_verification_code(phone: str) -> dict:
|
||||||
"success": False,
|
"success": False,
|
||||||
"message": f"发送失败: {response.body.message}"
|
"message": f"发送失败: {response.body.message}"
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"success": False,
|
||||||
"message": f"发送失败: {str(e)}"
|
"message": f"发送失败: {str(e)}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def verify_code(phone: str, code: str) -> bool:
|
def verify_code(phone: str, code: str) -> bool:
|
||||||
"""验证验证码"""
|
|
||||||
if phone not in VERIFICATION_CODES:
|
if phone not in VERIFICATION_CODES:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
stored = VERIFICATION_CODES[phone]
|
cached = VERIFICATION_CODES[phone]
|
||||||
|
if int(time.time()) > cached["expire"]:
|
||||||
# 检查是否过期
|
|
||||||
if int(time.time()) > stored["expire"]:
|
|
||||||
del VERIFICATION_CODES[phone]
|
del VERIFICATION_CODES[phone]
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 验证码匹配
|
if cached["code"] == code:
|
||||||
if stored["code"] == code:
|
|
||||||
# 验证成功,删除验证码
|
|
||||||
del VERIFICATION_CODES[phone]
|
del VERIFICATION_CODES[phone]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check_code_exists(phone: str) -> bool:
|
|
||||||
"""检查是否已发送过验证码"""
|
|
||||||
return phone in VERIFICATION_CODES
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue