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
+2 -2
View File
@@ -27,11 +27,11 @@ type Store struct {
const (
postgresApplicationName = "easyai-ai-gateway"
postgresConnectTimeout = 5 * time.Second
postgresPoolWarmLimit = 64
)
type PostgresPoolOptions struct {
MaxConns int
MinIdleConns int
IdleInTransactionTimeout time.Duration
LockTimeout time.Duration
}
@@ -130,8 +130,8 @@ func postgresPoolConfigWithOptions(databaseURL string, options PostgresPoolOptio
}
if options.MaxConns > 0 {
config.MaxConns = int32(options.MaxConns)
config.MinIdleConns = int32(min(options.MaxConns, postgresPoolWarmLimit))
}
config.MinIdleConns = int32(min(max(options.MinIdleConns, 0), int(config.MaxConns)))
return config, nil
}