codeant-ai-for-open-source[bot] commented on code in PR #40856:
URL: https://github.com/apache/superset/pull/40856#discussion_r3374951892
##########
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) {
Review Comment:
**🟠Architect Review — HIGH**
Connection limit checks use the raw `sockets` and
`channels[channel].sockets` registries, which are only cleaned asynchronously;
recently closed sockets remain counted until the next GC interval, so new
connections can be rejected as "over limit" even though no active connections
are actually consuming capacity.
**Suggestion:** Before enforcing limits, base the counts on an
actively-pruned view of connections (for example, invoking
`checkSockets`/`cleanChannel` or factoring their logic into a shared helper) so
that total and per-channel limits are computed from live socket state rather
than stale registry entries.
[Fix in
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=aee61ef876a04f5180c52bdee738b575&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
| [Fix in VSCode
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=aee61ef876a04f5180c52bdee738b575&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is an **Architect / Logical Review** comment left during a code review.
These reviews are first-class, important findings — not optional suggestions.
Do NOT dismiss this as a 'big architectural change' just because the title says
architect review; most of these can be resolved with a small, localized fix
once the intent is understood.
**Path:** superset-websocket/src/index.ts
**Line:** 173:185
**Comment:**
*HIGH: Connection limit checks use the raw `sockets` and
`channels[channel].sockets` registries, which are only cleaned asynchronously;
recently closed sockets remain counted until the next GC interval, so new
connections can be rejected as "over limit" even though no active connections
are actually consuming capacity.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
If a suggested approach is provided above, use it as the authoritative
instruction. If no explicit code suggestion is given, you MUST still draft and
apply your own minimal, localized fix — do not punt back with 'no suggestion
provided, review manually'. Keep the change as small as possible: add a guard
clause, gate on a loading state, reorder an await, wrap in a conditional, etc.
Do not refactor surrounding code or expand scope beyond the finding.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
--
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]