Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(validator): reorganize agent pool management and telemetry attributes

Victor Hugo dos Santos5e9611def652
diff --git a/backend/src/lib/telemetry/metrics.ts b/backend/src/lib/telemetry/metrics.ts
index 8181f1b94f..941c67f210 100644
--- a/backend/src/lib/telemetry/metrics.ts
+++ b/backend/src/lib/telemetry/metrics.ts
@@ -10,7 +10,7 @@ import type { Knex } from "knex";
import { classifyError } from "@app/lib/errors/classify";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
-import { getAgentPoolStats } from "@app/lib/validator/safe-request";
+import { getAgentPoolStats } from "@app/lib/validator/agent-pool";
import { getConfig } from "../config/env";
diff --git a/backend/src/lib/telemetry/telemetry-attributes.ts b/backend/src/lib/telemetry/telemetry-attributes.ts
index c653334f0f..97435eddef 100644
--- a/backend/src/lib/telemetry/telemetry-attributes.ts
+++ b/backend/src/lib/telemetry/telemetry-attributes.ts
@@ -32,6 +32,7 @@ export const INFISICAL_CORE_METER_ATTRIBUTES = [
"email_dispatch.outcome",
"email_dispatch.dimension",
"db.pool.state",
+ "pool.max",
// Closed enums on infisical.legacy_root_key.usage; per-project attribution is in logs, not here.
"legacy_key.operation",
"legacy_key.surface",
diff --git a/backend/src/lib/validator/agent-pool.ts b/backend/src/lib/validator/agent-pool.ts
new file mode 100644
index 0000000000..2cb4b869b2
--- /dev/null
+++ b/backend/src/lib/validator/agent-pool.ts
@@ -0,0 +1,11 @@
+import http from "node:http";
+import https from "node:https";
+
+// Pool of agents keyed by connection signature for connection reuse across
+// repeated calls. The cache key includes the validated IP set so DNS record
+// changes naturally produce a fresh entry. Insertion order doubles as LRU
+// recency, so eviction at the cap drops the least recently used entry.
+export const AGENT_CACHE_MAX = 200;
+export const agentCache = new Map<string, http.Agent | https.Agent>();
+
+export const getAgentPoolStats = () => ({ size: agentCache.size, max: AGENT_CACHE_MAX });
diff --git a/backend/src/lib/validator/safe-request.ts b/backend/src/lib/validator/safe-request.ts
index 304597e3c0..7258467eb7 100644
--- a/backend/src/lib/validator/safe-request.ts
+++ b/backend/src/lib/validator/safe-request.ts
@@ -16,6 +16,7 @@ import { recordSafeRequestAgentEvictionMetric } from "@app/lib/telemetry/metrics
import { BadRequestError } from "../errors";
import { isPrivateIp } from "../ip/ipRange";
+import { AGENT_CACHE_MAX, agentCache } from "./agent-pool";
const formatEntries = (entries: LookupAddress[]) => entries.map((e) => `${e.family}:${e.address}`).join(",");
@@ -168,13 +169,6 @@ const hasAgentCustomization = (opts: TBuildAgentOptions): boolean =>
opts.keepAlive !== undefined ||
opts.checkServerIdentity !== undefined;
-// Pool of agents keyed by connection signature for connection reuse across
-// repeated calls. The cache key includes the validated IP set so DNS record
-// changes naturally produce a fresh entry. Insertion order doubles as LRU
-// recency, so eviction at the cap drops the least recently used entry.
-const AGENT_CACHE_MAX = 200;
-const agentCache = new Map<string, http.Agent | https.Agent>();
-
// A keepAlive agent's idle sockets keep the agent itself reachable through their
// own event listeners, so dropping the map entry frees neither: the agent is
// never collected and each pooled socket holds its fd (and the peer's connection
@@ -216,8 +210,6 @@ const evictLeastRecentlyUsedAgents = () => {
}
};
-export const getAgentPoolStats = () => ({ size: agentCache.size, max: AGENT_CACHE_MAX });
-
const buildAgentCacheKey = (
validated: TValidatedHost | undefined,
protocol: string,