Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat(gateway-pools): load balance only when every member reports
bernie-g77cf3e37e3c3
diff --git a/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.test.ts b/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.test.tsindex 475deffca3..e63457cfcf 100644--- a/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.test.ts+++ b/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.test.ts@@ -1,6 +1,6 @@import { TGatewayScore } from "@app/lib/gateway-v2/gateway-load-tracker";-import { isPoolComparable, pickRandomGateway, shuffleForTieBreak } from "./gateway-pool-selection-fns";+import { everyMemberReportsLoad, pickRandomGateway, shuffleForTieBreak } from "./gateway-pool-selection-fns";// Every member reports unless a test says otherwise; a mixed pool is covered separately.const reported = (entries: Record<string, number>): Map<string, TGatewayScore> =>@@ -8,27 +8,31 @@ const reported = (entries: Record<string, number>): Map<string, TGatewayScore> =const gateways = [{ id: "a" }, { id: "b" }, { id: "c" }];-describe("isPoolComparable", () => {- test("comparable when every member reports", () => {- expect(isPoolComparable(gateways, reported({ a: 1, b: 2, c: 3 }))).toBe(true);+describe("everyMemberReportsLoad", () => {+ test("true when every member reports", () => {+ expect(everyMemberReportsLoad(gateways, reported({ a: 1, b: 2, c: 3 }))).toBe(true);});- test("comparable when no member reports, since the scale is consistent", () => {+ test("false when no member reports, because a platform-side count is not the same measurement", () => {const legacy = new Map(["a", "b", "c"].map((id) => [id, { score: 1, base: 1, reported: false }]));- expect(isPoolComparable(gateways, legacy)).toBe(true);+ expect(everyMemberReportsLoad(gateways, legacy)).toBe(false);});- test("not comparable when the pool is mid-rollout", () => {+ test("false when the pool is mid-rollout", () => {const mixed = new Map([["a", { score: 40, base: 40, reported: true }],["b", { score: 0, base: 0, reported: false }],["c", { score: 41, base: 41, reported: true }]]);- expect(isPoolComparable(gateways, mixed)).toBe(false);+ expect(everyMemberReportsLoad(gateways, mixed)).toBe(false);});- test("not comparable when a member has no data at all", () => {- expect(isPoolComparable(gateways, reported({ a: 5, b: 5 }))).toBe(false);+ test("false when a member has no data at all", () => {+ expect(everyMemberReportsLoad(gateways, reported({ a: 5, b: 5 }))).toBe(false);+ });++ test("false for an empty pool, so the caller never claims against nothing", () => {+ expect(everyMemberReportsLoad([], reported({}))).toBe(false);});});diff --git a/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.ts b/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.tsindex 15d6a55e49..706dbef7b4 100644--- a/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.ts+++ b/backend/src/ee/services/gateway-pool/gateway-pool-selection-fns.ts@@ -11,24 +11,18 @@ export const pickRandomGateway = <T extends TSelectableGateway>(};/**- * Load numbers are only worth comparing when every member is measured the same way.+ * Load-aware selection only runs when every member reports its own connection count.*- * A gateway new enough to report sends its real connection count, including PAM sessions the- * platform never sees. A gateway too old to report is counted only on the connections the platform- * itself opened, which is always the lower number.- *- * So a pool mid-upgrade is the bad case: the old member always looks emptier and would soak up- * traffic for the whole rollout. That pool gets no load awareness at all and falls back to random.- * A pool where nobody reports is fine, because every member is measured the same lower way, so- * comparing them is still fair.+ * A gateway new enough to report sends its real count, including PAM sessions the platform never+ * sees. A gateway too old to report can only be scored on the connections the platform itself+ * opened, which is always lower and misses whatever it is really carrying. There is no way to line+ * those two numbers up, and no reason to trust the second one on its own, so any pool that is not+ * fully upgraded picks at random instead.*/-export const isPoolComparable = <T extends TSelectableGateway>(+export const everyMemberReportsLoad = <T extends TSelectableGateway>(candidates: T[],scores: Map<string, TGatewayScore>-): boolean => {- const reportedCount = candidates.filter((c) => scores.get(c.id)?.reported).length;- return reportedCount === 0 || reportedCount === candidates.length;-};+): boolean => candidates.length > 0 && candidates.every((candidate) => scores.get(candidate.id)?.reported);/** Ties are resolved by position, so shuffling first is what randomises between equally loaded members. */export const shuffleForTieBreak = <T>(candidates: T[], random: () => number): T[] => {diff --git a/backend/src/ee/services/gateway-pool/gateway-pool-service.ts b/backend/src/ee/services/gateway-pool/gateway-pool-service.tsindex 56e0bf210e..5e34e90a16 100644--- a/backend/src/ee/services/gateway-pool/gateway-pool-service.ts+++ b/backend/src/ee/services/gateway-pool/gateway-pool-service.ts@@ -21,7 +21,7 @@ import { TPermissionServiceFactory } from "../permission/permission-service-typeimport { TPkiDiscoveryConfigDALFactory } from "../pki-discovery/pki-discovery-config-dal";import { TGatewayPoolDALFactory } from "./gateway-pool-dal";import { TGatewayPoolMembershipDALFactory } from "./gateway-pool-membership-dal";-import { isPoolComparable, pickRandomGateway, shuffleForTieBreak } from "./gateway-pool-selection-fns";+import { everyMemberReportsLoad, pickRandomGateway, shuffleForTieBreak } from "./gateway-pool-selection-fns";import {DEFAULT_POOL_FAILOVER_ATTEMPTS,TAddGatewayToPoolDTO,@@ -300,7 +300,7 @@ export const gatewayPoolServiceFactory = ({const scores = await loadTracker.getScores(pool.map((gateway) => gateway.id));- if (isPoolComparable(pool, scores)) {+ if (everyMemberReportsLoad(pool, scores)) {// Choose and claim atomically. Reading the minimum and then writing the reservation as two// calls lets every concurrent selection observe the same minimum and pile onto it.const claimed = await loadTracker.claimLeastLoaded(@@ -312,7 +312,8 @@ export const gatewayPoolServiceFactory = ({selected = pool.find((gateway) => gateway.id === claimed);if (selected) claimedAtomically = true;} else {- // Mixed-version pool: the scales are not comparable, so there is nothing to claim against.+ // At least one member is too old to report its load, so there is no sound comparison to+ // make and nothing to claim against.selected = pickRandomGateway(pool);}} catch (err) {diff --git a/docs/documentation/platform/gateways/gateway-pools.mdx b/docs/documentation/platform/gateways/gateway-pools.mdxindex a06cf8545c..05e8d03ca2 100644--- a/docs/documentation/platform/gateways/gateway-pools.mdx+++ b/docs/documentation/platform/gateways/gateway-pools.mdx@@ -67,9 +67,8 @@ Because the count comes from the gateway itself, it includes work the platform d<Warning>Load balancing requires gateways running a CLI version that reports connection counts. A gateway- on an older version does not report, and a pool that mixes reporting and non-reporting gateways- falls back to choosing at random rather than comparing counts that are not measured the same way.- Upgrade every gateway in a pool to get load balancing for that pool.+ on an older version does not report, and a pool containing any non-reporting member falls back to+ choosing at random. Upgrade every gateway in a pool to get load balancing for that pool.</Warning>## Failover