Loading...
Loading...
Compare original and translation side by side
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/db",
# Pool sizing
pool_size=20, # Steady-state connections
max_overflow=10, # Burst capacity (total max = 30)
# Connection health
pool_pre_ping=True, # Validate before use (adds ~1ms latency)
pool_recycle=3600, # Recreate connections after 1 hour
# Timeouts
pool_timeout=30, # Wait for connection from pool
connect_args={
"command_timeout": 60, # Query timeout
"server_settings": {
"statement_timeout": "60000", # 60s query timeout
},
},
)from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/db",
# 连接池大小设置
pool_size=20, # 稳态连接数
max_overflow=10, # 突发扩容容量(总最大连接数=30)
# 连接健康检查
pool_pre_ping=True, # 使用前验证连接(增加约1ms延迟)
pool_recycle=3600, # 1小时后重建连接
# 超时设置
pool_timeout=30, # 等待获取连接池连接的超时时间
connect_args={
"command_timeout": 60, # 查询超时
"server_settings": {
"statement_timeout": "60000", # 60秒查询超时
},
},
)import asyncpg
pool = await asyncpg.create_pool(
"postgresql://user:pass@localhost/db",
# Pool sizing
min_size=10, # Minimum connections kept open
max_size=20, # Maximum connections
# Connection lifecycle
max_inactive_connection_lifetime=300, # Close idle after 5 min
# Timeouts
command_timeout=60, # Query timeout
timeout=30, # Connection timeout
# Setup for each connection
setup=setup_connection,
)
async def setup_connection(conn):
"""Run on each new connection."""
await conn.execute("SET timezone TO 'UTC'")
await conn.execute("SET statement_timeout TO '60s'")import asyncpg
pool = await asyncpg.create_pool(
"postgresql://user:pass@localhost/db",
# 连接池大小设置
min_size=10, # 保持打开的最小连接数
max_size=20, # 最大连接数
# 连接生命周期
max_inactive_connection_lifetime=300, # 闲置5分钟后关闭连接
# 超时设置
command_timeout=60, # 查询超时
timeout=30, # 连接超时
# 每个连接的初始化操作
setup=setup_connection,
)
async def setup_connection(conn):
"""为每个新连接执行初始化操作。"""
await conn.execute("SET timezone TO 'UTC'")
await conn.execute("SET statement_timeout TO '60s'")import aiohttp
from aiohttp import TCPConnector
connector = TCPConnector(
# Connection limits
limit=100, # Total connections
limit_per_host=20, # Per-host limit
# Timeouts
keepalive_timeout=30, # Keep-alive duration
# SSL
ssl=False, # Or ssl.SSLContext for HTTPS
# DNS
ttl_dns_cache=300, # DNS cache TTL
)
session = aiohttp.ClientSession(
connector=connector,
timeout=aiohttp.ClientTimeout(
total=30, # Total request timeout
connect=10, # Connection timeout
sock_read=20, # Read timeout
),
)import aiohttp
from aiohttp import TCPConnector
connector = TCPConnector(
# 连接限制
limit=100, # 总连接数
limit_per_host=20, # 单主机连接数限制
# 超时设置
keepalive_timeout=30, # 连接保持时长
# SSL配置
ssl=False, # 或使用ssl.SSLContext配置HTTPS
# DNS缓存
ttl_dns_cache=300, # DNS缓存有效期
)
session = aiohttp.ClientSession(
connector=connector,
timeout=aiohttp.ClientTimeout(
total=30, # 请求总超时
connect=10, # 连接超时
sock_read=20, # 读取超时
),
)undefinedundefinedfrom contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: create pools
app.state.db_pool = await asyncpg.create_pool(DATABASE_URL)
app.state.http_session = aiohttp.ClientSession(
connector=TCPConnector(limit=100)
)
yield
# Shutdown: close pools
await app.state.db_pool.close()
await app.state.http_session.close()
app = FastAPI(lifespan=lifespan)from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动阶段:创建连接池
app.state.db_pool = await asyncpg.create_pool(DATABASE_URL)
app.state.http_session = aiohttp.ClientSession(
connector=TCPConnector(limit=100)
)
yield
# 关闭阶段:销毁连接池
await app.state.db_pool.close()
await app.state.http_session.close()
app = FastAPI(lifespan=lifespan)from prometheus_client import Gaugefrom prometheus_client import Gaugeundefinedundefined| Parameter | Small Service | Medium Service | High Load |
|---|---|---|---|
| pool_size | 5-10 | 20-50 | 50-100 |
| max_overflow | 5 | 10-20 | 20-50 |
| pool_pre_ping | True | True | Consider False* |
| pool_recycle | 3600 | 1800 | 900 |
| pool_timeout | 30 | 15 | 5 |
| 参数 | 小型服务 | 中型服务 | 高负载服务 |
|---|---|---|---|
| pool_size | 5-10 | 20-50 | 50-100 |
| max_overflow | 5 | 10-20 | 20-50 |
| pool_pre_ping | 开启 | 开启 | 考虑关闭* |
| pool_recycle | 3600 | 1800 | 900 |
| pool_timeout | 30 | 15 | 5 |
pool_size = (concurrent_requests / avg_queries_per_request) * 1.5
Example:
- 100 concurrent requests
- 3 queries per request average
- pool_size = (100 / 3) * 1.5 = 50pool_size = (并发请求数 / 平均每个请求的查询数) * 1.5
示例:
- 100个并发请求
- 平均每个请求包含3次查询
- pool_size = (100 / 3) * 1.5 = 50undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedsqlalchemy-2-asyncasyncio-advancedobservability-monitoringcaching-strategiessqlalchemy-2-asyncasyncio-advancedobservability-monitoringcaching-strategies