Copilot commented on code in PR #40856:
URL: https://github.com/apache/superset/pull/40856#discussion_r3375238936


##########
superset-websocket/src/index.ts:
##########
@@ -159,6 +159,37 @@ export const setLastFirehoseId = (id: string): void => {
   lastFirehoseId = id;
 };
 
+// WebSocket close code used when a connection is refused because a configured
+// connection limit has been reached (1013 = "Try Again Later").
+const CONNECTION_LIMIT_CLOSE_CODE = 1013;
+
+/**
+ * Determines whether accepting a new connection on the given channel would
+ * exceed a configured connection limit. Returns a human-readable reason when a
+ * limit is reached, or `null` when the connection is within limits.
+ *
+ * Both limits are opt-in: a value of `0` (the default) disables the check.
+ */
+export const connectionLimitReason = (channel: string): string | null => {
+  const { maxTotalConnections, maxConnectionsPerChannel } = opts;
+
+  if (
+    maxTotalConnections > 0 &&
+    Object.keys(sockets).length >= maxTotalConnections
+  ) {
+    return `total connection limit (${maxTotalConnections}) reached`;
+  }
+
+  if (maxConnectionsPerChannel > 0) {
+    const channelSize = channels[channel]?.sockets.length ?? 0;
+    if (channelSize >= maxConnectionsPerChannel) {
+      return `per-channel connection limit (${maxConnectionsPerChannel}) 
reached`;
+    }
+  }

Review Comment:
   `connectionLimitReason` uses `channels[channel]?.sockets.length` to enforce 
the per-channel limit, but `channels[channel].sockets` can contain stale 
socketIds after `checkSockets()` deletes entries from `sockets` (channel 
cleanup happens later via `cleanChannel`/GC). This can incorrectly reject new 
connections even when there are fewer active sockets than the limit. Count only 
active sockets (present in `sockets` and in an active readyState) when 
enforcing the per-channel limit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to