FastAPI数据库连接池配置与监控

avatar
cmdragon 大乘
image image

扫描二维码关注或者微信搜一搜:编程智域 前端至全栈交流与成长

探索数千个预构建的 AI 应用,开启你的下一个伟大创意https://tools.cmdragon.cn/

FastAPI数据库连接池配置与监控实战

1. 数据库连接池基础原理

数据库连接池如同出租车调度站,预先创建多个可用连接供应用程序随时调用。当客户端请求到达时,连接池会分配空闲连接;请求结束后,连接会返回池中等待下次使用。这种机制相比传统即用即建的方式,能有效降低连接创建开销。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 配置Tortoise-ORM连接池示例
TORTOISE_ORM = {
"connections": {
"default": {
"engine": "tortoise.backends.asyncpg",
"credentials": {
"host": "localhost",
"port": "5432",
"user": "postgres",
"password": "secret",
"database": "mydb",
"minsize": 3, # 最小保持连接数
"maxsize": 20, # 最大连接数
"max_inactive_connection_lifetime": 300 # 空闲连接存活时间(秒)
}
}
},
"apps": {
"models": {
"models": ["models"],
"default_connection": "default"
}
}
}

2. 连接池参数详解

  • minsize:相当于出租车公司的最低保障车队,即使深夜时段也保持3辆待命
  • maxsize:节假日最大调度能力,最多可派出20辆出租车
  • max_inactive_connection_lifetime:车辆闲置5分钟后自动回收,节省停车费用

实时监控示例代码:

1
2
3
4
5
6
7
8
9
10
11
from tortoise import Tortoise


@app.get("/pool-status")
async def get_pool_status():
pool = Tortoise.get_connection("default")._pool
return {
"current_size": pool._size,
"idle": len(pool._holders),
"in_use": pool._size - len(pool._holders)
}

3. 生产环境监控方案

采用Prometheus + Grafana构建可视化监控平台:

  1. 安装监控组件:
1
pip install prometheus-client prometheus-fastapi-instrumentator
  1. 集成指标收集:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from prometheus_client import make_asgi_app
from prometheus_fastapi_instrumentator import Instrumentator

# 添加Prometheus中间件
metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)


# 自定义连接池指标
class DatabaseMetrics:
def __init__(self):
self.connections_in_use = Gauge(
'db_connections_in_use',
'Current active connections'
)

async def update_metrics(self):
pool = Tortoise.get_connection("default")._pool
self.connections_in_use.set(pool._size - len(pool._holders))


# 启动定时任务
@app.on_event("startup")
async def start_metrics_task():
metrics = DatabaseMetrics()

async def _task():
while True:
await metrics.update_metrics()
await asyncio.sleep(5)

asyncio.create_task(_task())

4. 连接池性能优化实战

用户注册场景下的连接管理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from fastapi import APIRouter
from models import User_Pydantic, UserIn_Pydantic, Users

router = APIRouter()


@router.post("/users", response_model=User_Pydantic)
async def create_user(user: UserIn_Pydantic):
try:
# 自动获取连接执行操作
user_obj = await Users.create(**user.dict())
return await User_Pydantic.from_tortoise_orm(user_obj)
except Exception as e:
# 记录异常但不干扰连接池
logger.error(f"Create user failed: {str(e)}")
raise HTTPException(status_code=400, detail="User creation failed")

课后Quiz

问题1:当数据库响应变慢时,如何快速判断是否连接池不足?
A) 检查CPU使用率
B) 监控连接等待队列
C) 查看磁盘空间
D) 重启数据库服务

答案:B) 监控连接等待队列。当所有连接都被占用时,新请求会进入等待队列,此时需要适当增大maxsize或优化查询性能。

问题2:以下哪种情况可能导致连接泄漏?
A) 未关闭游标对象
B) 忘记提交事务
C) 未设置max_inactive_connection_lifetime
D) 所有选项都可能

答案:D) 所有选项都可能。未释放的资源都会导致连接无法回到池中,最终耗尽连接池。

常见报错处理

错误现象
TimeoutError: Connection pool exhausted

解决方案

  1. 检查当前连接使用情况:
1
2
3
4
5
6
7
8
9
# 临时获取连接池状态
from tortoise import Tortoise


async def check_pool():
conn = Tortoise.get_connection("default")
print(f"Max size: {conn._pool._maxsize}")
print(f"Current size: {conn._pool._size}")
print(f"Available: {len(conn._pool._holders)}")
  1. 优化建议:
  • 适当增加maxsize参数
  • 检查是否存在长时间未提交的事务
  • 添加连接等待超时配置:
1
2
3
4
credentials = {
...
"timeout": 30 # 等待连接超时时间(秒)
}

预防措施

  • 使用async with管理事务:
1
2
3
async with in transaction():
# 数据库操作
await User.create(...)
  • 定期执行SELECT 1保持空闲连接
  • 设置合理的max_inactive_connection_lifetime(建议300-600秒)

余下文章内容请点击跳转至 个人博客页面 或者 扫码关注或者微信搜一搜:编程智域 前端至全栈交流与成长,阅读完整的文章:

往期文章归档: