fix: Incorrect error shown to user when connection limit is reached (#5695)
This commit is contained in:
4
server/collaboration/CloseEvents.ts
Normal file
4
server/collaboration/CloseEvents.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const TooManyConnections = {
|
||||
code: 4503,
|
||||
reason: "Too Many Connections",
|
||||
};
|
||||
@@ -6,28 +6,36 @@ import {
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { trace } from "@server/logging/tracing";
|
||||
import { TooManyConnections } from "./CloseEvents";
|
||||
|
||||
@trace()
|
||||
export class ConnectionLimitExtension implements Extension {
|
||||
/**
|
||||
* Map of documentId -> connection count
|
||||
*/
|
||||
connectionsByDocument: Map<string, number> = new Map();
|
||||
connectionsByDocument: Map<string, Set<string>> = new Map();
|
||||
|
||||
/**
|
||||
* onDisconnect hook
|
||||
* @param data The disconnect payload
|
||||
*/
|
||||
onDisconnect(data: onDisconnectPayload) {
|
||||
const { documentName } = data;
|
||||
const { documentName, socketId } = data;
|
||||
|
||||
const currConnections = this.connectionsByDocument.get(documentName) || 0;
|
||||
const newConnections = currConnections - 1;
|
||||
this.connectionsByDocument.set(documentName, newConnections);
|
||||
const connections = this.connectionsByDocument.get(documentName);
|
||||
if (connections) {
|
||||
connections.delete(socketId);
|
||||
|
||||
if (connections.size === 0) {
|
||||
this.connectionsByDocument.delete(documentName);
|
||||
} else {
|
||||
this.connectionsByDocument.set(documentName, connections);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.debug(
|
||||
"multiplayer",
|
||||
`${newConnections} connections to "${documentName}"`
|
||||
`${connections?.size} connections to "${documentName}"`
|
||||
);
|
||||
|
||||
return Promise.resolve();
|
||||
@@ -40,23 +48,24 @@ export class ConnectionLimitExtension implements Extension {
|
||||
onConnect(data: onConnectPayload) {
|
||||
const { documentName } = data;
|
||||
|
||||
const currConnections = this.connectionsByDocument.get(documentName) || 0;
|
||||
if (currConnections >= env.COLLABORATION_MAX_CLIENTS_PER_DOCUMENT) {
|
||||
const connections =
|
||||
this.connectionsByDocument.get(documentName) || new Set();
|
||||
if (connections?.size >= env.COLLABORATION_MAX_CLIENTS_PER_DOCUMENT) {
|
||||
Logger.info(
|
||||
"multiplayer",
|
||||
`Rejected connection to "${documentName}" because it has reached the maximum number of connections`
|
||||
);
|
||||
|
||||
// Rejecting the promise will cause the connection to be dropped.
|
||||
return Promise.reject();
|
||||
return Promise.reject(TooManyConnections);
|
||||
}
|
||||
|
||||
const newConnections = currConnections + 1;
|
||||
this.connectionsByDocument.set(documentName, newConnections);
|
||||
connections.add(data.socketId);
|
||||
this.connectionsByDocument.set(documentName, connections);
|
||||
|
||||
Logger.debug(
|
||||
"multiplayer",
|
||||
`${newConnections} connections to "${documentName}"`
|
||||
`${connections.size} connections to "${documentName}"`
|
||||
);
|
||||
|
||||
return Promise.resolve();
|
||||
|
||||
Reference in New Issue
Block a user