Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/redisHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ dayjs.extend(UTC);
dayjs.extend(Timezone);
dayjs.extend(AdvancedFormat);

export const logConnectionCount = (language: string, status: ConnectionStatus) => {
export const logConnectionCount = (language: string, status: ConnectionStatus, connectionCounter?: number) => {
const redisKey = `${CONNECTION_STATS}:${language}:${dayjs().tz('Asia/Kolkata').format('YYYY-MM-DDTHH:mm')}`
const totalRedisKey = `${CONNECTION_STATS}:${language}`
if (status === ConnectionStatus.ACTIVE) {
redis.hincrby(redisKey, ACTIVE, 1)
redis.hincrby(redisKey, ACTIVE, connectionCounter)
}
if (status === ConnectionStatus.LANGUAGE_SERVER_IN_USE) {
redis.hincrby(redisKey, LANGUAGE_SERVER_IN_USE, 1)
Expand All @@ -29,7 +29,7 @@ export const logConnectionCount = (language: string, status: ConnectionStatus) =
redis.hincrby(totalRedisKey, INCOMING, 1)
}
if (status === ConnectionStatus.LIVE) {
redis.hincrby(redisKey, LIVE, 1)
redis.hincrby(redisKey, LIVE, connectionCounter)
}
const expiryTimeInSeconds = 60 * 60 * 6;
redis.expire(redisKey, expiryTimeInSeconds, 'NX');
Expand Down
38 changes: 24 additions & 14 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,47 @@ function toSocket(webSocket: ws): rpc.IWebSocket {
}
}
const language: string = argv.language
const processCount: number = argv.count || 2
const langServer = languageServers[language]
let connection = null
let localConnection = rpcServer.createServerProcess(`${langServer[0]}LSP`, langServer[0], langServer.slice(1));
let connectionCounter = 1
let processCollection = []
let processInUse = [];
for (var i = 0; i < processCount; i++) {
processCollection.push(rpcServer.createServerProcess(`${langServer[0]}LSP - ${i}`, langServer[0], langServer.slice(1)))
processInUse.push(false)
}
setInterval(() => {
console.log("VALID CONNECTION:", connection != null)
if (connection != null) {
logConnectionCount(language, ConnectionStatus.ACTIVE)
const activeConnections = connectionCounter - 1;
console.log("VALID CONNECTION:", activeConnections)
if (activeConnections > 0) {
logConnectionCount(language, ConnectionStatus.ACTIVE, activeConnections)
}
logConnectionCount(language, ConnectionStatus.LIVE)
logConnectionCount(language, ConnectionStatus.LIVE, processCount)
}, 60000)
wss.on('connection', (client: ws, request: http.IncomingMessage) => {
if (!langServer || !langServer.length) {
console.error('Invalid language server');
client.close();
return;
}
if (connection != null) {
if (connectionCounter > processCount || processInUse.indexOf(false) === -1) {
logConnectionCount(language, ConnectionStatus.LANGUAGE_SERVER_IN_USE)
client.close();
return;
}
let socket: rpc.IWebSocket = toSocket(client);
connection = rpcServer.createWebSocketConnection(socket);
const connectionId = processInUse.indexOf(false)
const socket: rpc.IWebSocket = toSocket(client);
const localConnection = processCollection[connectionId]
const connection = rpcServer.createWebSocketConnection(socket);
processInUse[connectionId] = true
rpcServer.forward(connection, localConnection);
logConnectionCount(language, ConnectionStatus.INCOMING)
console.log("Forwarding new client");
console.log("Forwarding new client: ", connectionId);
connectionCounter++;
socket.onClose((code) => {
console.log('Client closed', code);
connectionCounter--;
console.log('Client closed: ', code, connectionId);
logConnectionCount(language, ConnectionStatus.CLOSED)
connection = null;
localConnection.dispose();
localConnection = rpcServer.createServerProcess(`${langServer[0]}LSP`, langServer[0], langServer.slice(1));
processInUse[connectionId] = false
});
});