将同步与异步非文本任务的准入唤醒改为按任务和全局队首推进,批量续租等待者,避免大量请求同时争抢 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 千任务压力测试通过。
78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
func TestPostgresPoolConfigSetsDiagnosticAndConnectTimeout(t *testing.T) {
|
|
config, err := postgresPoolConfig("postgresql://gateway:password@localhost:5432/gateway?sslmode=disable")
|
|
if err != nil {
|
|
t.Fatalf("parse PostgreSQL pool config: %v", err)
|
|
}
|
|
if config.ConnConfig.ConnectTimeout != 5*time.Second {
|
|
t.Fatalf("connect timeout = %s, want 5s", config.ConnConfig.ConnectTimeout)
|
|
}
|
|
if applicationName := config.ConnConfig.RuntimeParams["application_name"]; applicationName != "easyai-ai-gateway" {
|
|
t.Fatalf("application_name = %q, want easyai-ai-gateway", applicationName)
|
|
}
|
|
}
|
|
|
|
func TestPostgresPoolConfigRejectsMalformedURL(t *testing.T) {
|
|
if _, err := postgresPoolConfig("://malformed"); err == nil {
|
|
t.Fatal("expected malformed PostgreSQL URL to fail")
|
|
}
|
|
}
|
|
|
|
func TestPostgresPoolConfigWarmsBoundedIdleConnections(t *testing.T) {
|
|
config, err := postgresPoolConfig(
|
|
"postgresql://gateway:password@localhost:5432/gateway?sslmode=disable",
|
|
24,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("parse PostgreSQL pool config: %v", err)
|
|
}
|
|
if config.MaxConns != 24 || config.MinIdleConns != 24 {
|
|
t.Fatalf(
|
|
"pool bounds max=%d minIdle=%d, want 24/24",
|
|
config.MaxConns,
|
|
config.MinIdleConns,
|
|
)
|
|
}
|
|
|
|
small, err := postgresPoolConfig(
|
|
"postgresql://gateway:password@localhost:5432/gateway?sslmode=disable",
|
|
4,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("parse small PostgreSQL pool config: %v", err)
|
|
}
|
|
if small.MinIdleConns != 4 {
|
|
t.Fatalf("small pool minIdle=%d, want 4", small.MinIdleConns)
|
|
}
|
|
}
|
|
|
|
func TestIsPostgresUnavailableClassifiesConnectivityFailures(t *testing.T) {
|
|
for _, testCase := range []struct {
|
|
name string
|
|
err error
|
|
}{
|
|
{name: "deadline", err: context.DeadlineExceeded},
|
|
{name: "connection exception", err: &pgconn.PgError{Code: "08006"}},
|
|
{name: "too many connections", err: &pgconn.PgError{Code: "53300"}},
|
|
{name: "cannot connect now", err: &pgconn.PgError{Code: "57P03"}},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
if !IsPostgresUnavailable(testCase.err) {
|
|
t.Fatalf("error %v was not classified as PostgreSQL unavailable", testCase.err)
|
|
}
|
|
})
|
|
}
|
|
if IsPostgresUnavailable(&pgconn.PgError{Code: "42601"}) {
|
|
t.Fatal("SQL syntax error was incorrectly classified as PostgreSQL unavailable")
|
|
}
|
|
}
|