限制 AgentRun manager Postgres 连接池 (#223)

This commit is contained in:
Lyon
2026-06-22 03:44:54 +08:00
committed by GitHub
parent 205df57eb7
commit 30bce7eb69
2 changed files with 13 additions and 2 deletions
+2 -1
View File
@@ -12,6 +12,7 @@ import { normalizeRunEventPayload, requireEventType } from "../common/events.js"
interface PostgresStoreOptions {
connectionString: string;
poolMax?: number;
}
interface MigrationDefinition {
@@ -408,7 +409,7 @@ export class PostgresAgentRunStore implements AgentRunStore {
private appliedMigrationId: string | null = null;
constructor(options: PostgresStoreOptions) {
this.pool = new Pool({ connectionString: options.connectionString, application_name: "agentrun-mgr-v01" });
this.pool = new Pool({ connectionString: options.connectionString, application_name: "agentrun-mgr-v01", ...(options.poolMax === undefined ? {} : { max: options.poolMax }) });
}
async migrate(): Promise<void> {
+11 -1
View File
@@ -111,13 +111,23 @@ export async function openAgentRunStoreFromEnv(env: NodeJS.ProcessEnv = process.
const databaseUrl = env.DATABASE_URL?.trim();
if (databaseUrl) {
const { createPostgresAgentRunStore } = await import("./postgres-store.js");
return createPostgresAgentRunStore({ connectionString: databaseUrl });
return createPostgresAgentRunStore({ connectionString: databaseUrl, poolMax: optionalPositiveIntegerEnv(env, "AGENTRUN_POSTGRES_POOL_MAX") });
}
const storeMode = env.AGENTRUN_STORE ?? env.AGENTRUN_MGR_STORE;
if (storeMode === "memory") return new MemoryAgentRunStore();
throw new AgentRunError("infra-failed", "DATABASE_URL is required for agentrun-mgr live runtime; set AGENTRUN_STORE=memory only for explicit self-test/dev mode", { httpStatus: 503, details: { adapter: "postgres", databaseUrl: "missing", memoryFallback: "disabled" } });
}
function optionalPositiveIntegerEnv(env: NodeJS.ProcessEnv, name: string): number | undefined {
const raw = env[name]?.trim();
if (!raw) return undefined;
const value = Number(raw);
if (!Number.isSafeInteger(value) || value < 1) {
throw new AgentRunError("infra-failed", `${name} must be a positive integer`, { httpStatus: 503, details: { env: name, valuesPrinted: false } });
}
return value;
}
export class MemoryAgentRunStore implements AgentRunStore {
private readonly runs = new Map<string, RunRecord>();
private readonly commands = new Map<string, CommandRecord>();