perf(postgres): 分离 API 池并降低连接预热

生产同构千并发请求在干净队列上复现 API Pool 31/32、idle=1 且 canceled acquire 持续增长,Worker Pool 仅使用少量连接,确认瓶颈位于 API 数据库连接预算而非 Worker 执行槽。

新增可配置的 AI_GATEWAY_DATABASE_MIN_IDLE_CONNS,生产仅预热 4 条连接;API Pool 独立固定为 48,Worker Pool 继续跟随 P24/P28/P32 的 32/36/40 档位。发布工具、Kubernetes 清单、验收门禁和文档同步更新。

验证:Go 全量测试、gofmt、bash -n、ShellCheck、kubectl kustomize 和 git diff --check 通过。
This commit is contained in:
2026-07-31 03:28:43 +08:00
parent 2606cc8223
commit 2529679b67
11 changed files with 78 additions and 23 deletions
+8 -3
View File
@@ -667,6 +667,8 @@ mark_run_failed() {
apply_capacity_profile() {
local profile=$1
local slots pool media global
local api_pool=48
local min_idle=4
case $profile in
P24) slots=24; pool=32; media=24; global=48 ;;
P28) slots=28; pool=36; media=28; global=56 ;;
@@ -679,13 +681,15 @@ apply_capacity_profile() {
"AI_GATEWAY_ASYNC_WORKER_INSTANCE_HARD_LIMIT=$slots" \
"AI_GATEWAY_ASYNC_WORKER_HARD_LIMIT=$global" \
"AI_GATEWAY_DATABASE_MAX_CONNS=$pool" \
"AI_GATEWAY_DATABASE_MIN_IDLE_CONNS=$min_idle" \
"AI_GATEWAY_MEDIA_MATERIALIZATION_CONCURRENCY=$media" \
"AI_GATEWAY_MEDIA_REQUEST_CONCURRENCY=$media" >/dev/null
remote_kubectl rollout status "deployment/easyai-worker-$site" \
-n "$namespace" --timeout=300s
remote_kubectl set env "deployment/easyai-api-$site" -n "$namespace" \
"AI_GATEWAY_ASYNC_WORKER_HARD_LIMIT=$global" \
"AI_GATEWAY_DATABASE_MAX_CONNS=$pool" >/dev/null
"AI_GATEWAY_DATABASE_MAX_CONNS=$api_pool" \
"AI_GATEWAY_DATABASE_MIN_IDLE_CONNS=$min_idle" >/dev/null
remote_kubectl rollout status "deployment/easyai-api-$site" \
-n "$namespace" --timeout=300s
done
@@ -895,6 +899,7 @@ wait_for_memory_recovery() {
verify_runtime_gates() {
local expected_slots expected_global expected_pool
local expected_api_pool=48
case $active_profile in
P24) expected_slots=24; expected_pool=32; expected_global=48 ;;
P28) expected_slots=28; expected_pool=36; expected_global=56 ;;
@@ -1055,8 +1060,8 @@ WHERE status='active'
pool_max=$(awk '$1=="easyai_gateway_postgres_pool_max_connections" {print $2}' <<<"$metrics")
acquired=$(awk '$1=="easyai_gateway_postgres_pool_acquired_connections" {print $2}' <<<"$metrics")
idle=$(awk '$1=="easyai_gateway_postgres_pool_idle_connections" {print $2}' <<<"$metrics")
[[ ${pool_max%.*} == "$expected_pool" ]]
if [[ ${acquired%.*} == "$expected_pool" && ${idle%.*} == 0 ]]; then
[[ ${pool_max%.*} == "$expected_api_pool" ]]
if [[ ${acquired%.*} == "$expected_api_pool" && ${idle%.*} == 0 ]]; then
echo "API connection pool saturated for site $site" >&2
return 1
fi