Skip to content

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

chore(gateway-pools): tighten the load report limit and clarify comments

bernie-g4f20e5ad8dd1
diff --git a/backend/src/ee/routes/v2/gateway-router.ts b/backend/src/ee/routes/v2/gateway-router.ts
index bf2a582d7b..08874c04df 100644
--- a/backend/src/ee/routes/v2/gateway-router.ts
+++ b/backend/src/ee/routes/v2/gateway-router.ts
@@ -62,10 +62,10 @@ export const registerGatewayV2Router = async (server: FastifyZodProvider) => {
}
});
- // Separate from /heartbeat because of what its handler costs, not how it is called: a heartbeat
- // makes the platform dial back through the relay to probe liveness and then writes to Postgres,
- // which is far too expensive at the cadence pool selection needs. This one only writes a Redis key,
- // and deliberately does not touch heartbeat, so a gateway cannot look alive by reporting load.
+ // Heartbeat is expensive: the platform dials back through the relay to check the gateway is
+ // really there, then writes to Postgres. Every gateway reports load every 10 seconds, which is
+ // far too often for that. This one just writes a Redis key. It does not touch heartbeat, so a
+ // gateway cannot look alive just by reporting load.
server.route({
method: "POST",
url: "/load",
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.ts
index 49a752052c..15d6a55e49 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,10 +11,16 @@ export const pickRandomGateway = <T extends TSelectableGateway>(
};
/**
- * False when some members report their own load and others are too old to, because the two numbers
- * are on different scales: a non-reporting member is counted only on channels this platform opened,
- * which is strictly lower than a reporting peer's true occupancy. Comparing them would pull traffic
- * onto un-upgraded gateways for a whole rollout, so such a pool gets no load awareness at all.
+ * Load numbers are only worth comparing when every member is measured the same way.
+ *
+ * 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.
*/
export const isPoolComparable = <T extends TSelectableGateway>(
candidates: T[],
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index 0b9f193b02..f85ec1e344 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1008,8 +1008,9 @@ export const gatewayV2ServiceFactory = ({
throw new NotFoundError({ message: `Gateway with ID '${orgPermission.id}' not found` });
}
- // Deliberately no heartbeat side effect: liveness stays with /heartbeat so a gateway cannot look
- // healthy purely by reporting load, and so this stays a single Redis write.
+ // Do not mark the gateway alive here. A load report only proves the gateway can reach us;
+ // heartbeat proves we can reach it back through the relay. Updating liveness from this would
+ // keep a gateway whose relay path is broken looking healthy.
await getGatewayLoadTracker()?.recordReportedLoad(gateway.id, activeChannels);
return { gatewayId: gateway.id, activeChannels };
diff --git a/backend/src/server/config/rateLimiter.ts b/backend/src/server/config/rateLimiter.ts
index e4023659de..e028cd6346 100644
--- a/backend/src/server/config/rateLimiter.ts
+++ b/backend/src/server/config/rateLimiter.ts
@@ -38,13 +38,15 @@ export const writeLimit: RateLimitOptions = {
keyGenerator: (req) => req.realIp
};
-// Gateways report load every 10s (6/min each). Keyed by the reporting gateway rather than by IP,
-// because the write limiter's IP key is shared: ~100 gateways behind one NAT address would exhaust
-// a 200/min quota on load reports alone, start getting 429s, and their entries would go stale.
+// Gateways report load every 10s (6/min each), so 10 leaves room for tick drift and a restart
+// landing in the same window without leaving headroom for a flood. Keyed by the reporting gateway
+// rather than by IP, because the write limiter's IP key is shared: ~100 gateways behind one NAT
+// address would exhaust a 200/min quota on load reports alone, start getting 429s, and their
+// entries would go stale.
export const gatewayLoadReportLimit: RateLimitOptions = {
timeWindow: 60 * 1000,
hook: "preValidation",
- max: 30,
+ max: 10,
keyGenerator: (req) => {
const actorId = (req as { permission?: { id?: string } }).permission?.id;
return actorId ? `gateway-load:${actorId}` : req.realIp;