perf(queue): 限制大媒体任务内存并消除准入惊群

将同步与异步非文本任务的准入唤醒改为按任务和全局队首推进,批量续租等待者,避免大量请求同时争抢 advisory lock 和数据库连接。

对 Base64 请求解析、素材解码、上游媒体执行和结果物化增加分层并发限制,复用请求素材并释放重复 Gemini wire 数据;生产 API 默认入口解析并发 16,媒体物化并发 8。

新增 1000 个同步 Gemini 图像编辑请求的模拟上游压力验收,10 MiB 输入和输出下全部成功,Heap 峰值增长约 2.58 GiB,并验证共享上传哈希、单次 attempt 与本地零落盘。

验证:go test ./... -count=1;go vet ./...;gofmt -l 无输出;kubectl kustomize deploy/kubernetes/production;10 MiB Gemini Base64 千任务压力测试通过。
This commit is contained in:
2026-07-30 13:13:32 +08:00
parent fd3b6bf042
commit ea58d21d03
26 changed files with 1980 additions and 74 deletions
+23 -1
View File
@@ -26,6 +26,7 @@ type Store struct {
const (
postgresApplicationName = "easyai-ai-gateway"
postgresConnectTimeout = 5 * time.Second
postgresPoolWarmLimit = 64
)
func defaultAPIKeyScopes() []string {
@@ -88,7 +89,7 @@ func ConnectWithMaxConns(ctx context.Context, databaseURL string, maxConns int)
if err != nil {
return nil, err
}
if err := pool.Ping(ctx); err != nil {
if err := warmPostgresPool(ctx, pool, int(config.MinIdleConns)); err != nil {
pool.Close()
return nil, err
}
@@ -104,10 +105,31 @@ func postgresPoolConfig(databaseURL string, maxConns ...int) (*pgxpool.Config, e
config.ConnConfig.RuntimeParams["application_name"] = postgresApplicationName
if len(maxConns) > 0 && maxConns[0] > 0 {
config.MaxConns = int32(maxConns[0])
config.MinIdleConns = int32(min(maxConns[0], postgresPoolWarmLimit))
}
return config, nil
}
func warmPostgresPool(ctx context.Context, pool *pgxpool.Pool, count int) error {
if count <= 0 {
return pool.Ping(ctx)
}
connections := make([]*pgxpool.Conn, 0, count)
defer func() {
for _, connection := range connections {
connection.Release()
}
}()
for len(connections) < count {
connection, err := pool.Acquire(ctx)
if err != nil {
return err
}
connections = append(connections, connection)
}
return nil
}
func IsPostgresUnavailable(err error) bool {
if err == nil {
return false