限制 AgentRun manager Postgres 连接池 (#223)
This commit is contained in:
@@ -12,6 +12,7 @@ import { normalizeRunEventPayload, requireEventType } from "../common/events.js"
|
|||||||
|
|
||||||
interface PostgresStoreOptions {
|
interface PostgresStoreOptions {
|
||||||
connectionString: string;
|
connectionString: string;
|
||||||
|
poolMax?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MigrationDefinition {
|
interface MigrationDefinition {
|
||||||
@@ -408,7 +409,7 @@ export class PostgresAgentRunStore implements AgentRunStore {
|
|||||||
private appliedMigrationId: string | null = null;
|
private appliedMigrationId: string | null = null;
|
||||||
|
|
||||||
constructor(options: PostgresStoreOptions) {
|
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> {
|
async migrate(): Promise<void> {
|
||||||
|
|||||||
+11
-1
@@ -111,13 +111,23 @@ export async function openAgentRunStoreFromEnv(env: NodeJS.ProcessEnv = process.
|
|||||||
const databaseUrl = env.DATABASE_URL?.trim();
|
const databaseUrl = env.DATABASE_URL?.trim();
|
||||||
if (databaseUrl) {
|
if (databaseUrl) {
|
||||||
const { createPostgresAgentRunStore } = await import("./postgres-store.js");
|
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;
|
const storeMode = env.AGENTRUN_STORE ?? env.AGENTRUN_MGR_STORE;
|
||||||
if (storeMode === "memory") return new MemoryAgentRunStore();
|
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" } });
|
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 {
|
export class MemoryAgentRunStore implements AgentRunStore {
|
||||||
private readonly runs = new Map<string, RunRecord>();
|
private readonly runs = new Map<string, RunRecord>();
|
||||||
private readonly commands = new Map<string, CommandRecord>();
|
private readonly commands = new Map<string, CommandRecord>();
|
||||||
|
|||||||
Reference in New Issue
Block a user