Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHARD-1058: Add per-IP connection limit to WebSocket server #97

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

S0naliThakur
Copy link
Member

Linear: https://linear.app/shm/issue/SHARD-1058

Summary:

  1. Introduced a new configuration option maxConnectionsPerIP to limit the number of concurrent WebSocket connections from a single IP address.
  2. Updated the onConnection function to check the number of active connections per IP and close the socket if the limit is reached.
  3. Modified unit tests to accommodate the new IP connection limit functionality.

@@ -122,6 +123,7 @@ export const CONFIG: Config = {
maxConnections: Number(process.env.WS_MAX_CONNECTIONS) || 1000,
maxSubscriptionsPerSocket: Number(process.env.WS_MAX_SUBSCRIPTIONS_PER_SOCKET) || 50,
connectionTimeoutMs: Number(process.env.WS_CONNECTION_TIMEOUT_MS) || 24 * 60 * 60 * 1000, // 1 day in ms
maxConnectionsPerIP: Number(process.env.WS_MAX_CONNECTIONS_PER_IP) || 50,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed, let's change it to 2 or 3

@@ -23,15 +23,30 @@ interface Request {

// Add connection counter
let activeConnections = 0
const connectionsByIP = new Map<string, number>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintain sockets itself instead of count. This will help us identify disconnected sockets which can be cleaned up upon request if need be to allow requested connection

// Check max connections limit
if (activeConnections >= CONFIG.websocket.maxConnections) {
socket.close(1003, 'Server busy. Please try again later.')
export const onConnection = async (socket: WebSocket.WebSocket, req: any): Promise<void> => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use specific type for req instead of any?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants