Skip to content

Commit

Permalink
Attempt to reuse the same port for the server
Browse files Browse the repository at this point in the history
  • Loading branch information
nisargjhaveri committed Jan 6, 2025
1 parent 9bcf629 commit 98023ea
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,48 @@ import { logger } from './logger';
let server: Server|undefined = undefined;
let serverUri: vscode.Uri|undefined = undefined;

async function startServerAtPort(context: vscode.ExtensionContext, port: number): Promise<vscode.Uri> {
server = new Server(
{
port: port,
host: "127.0.0.1"
},
/* httpsOptions */ undefined,
{
staticClientPath: vscode.Uri.joinPath(context.extensionUri, "dist/web").fsPath,
}
);

serverUri = vscode.Uri.parse(await server.start());
return serverUri;
}

async function ensureServerListening(context: vscode.ExtensionContext) {
if (!server) {
server = new Server(
{
port: 0,
host: "127.0.0.1"
},
/* httpsOptions */ undefined,
{
staticClientPath: vscode.Uri.joinPath(context.extensionUri, "dist/web").fsPath,
}
);
}
if (!(server && server.isListening())) {
let port: number = 44615;
let remainingAttempts = 5;

while (remainingAttempts > 0) {
remainingAttempts--;

if (!server.isListening()) {
serverUri = vscode.Uri.parse(await server.start());
try {
await startServerAtPort(context, port);
break;
} catch (e: any) {
if (e.code === "EADDRINUSE" && remainingAttempts > 0) {
logger.info(`Port ${port} is already in use, trying another port...`);
if (remainingAttempts === 1) {
// Try random port on the last attempt
port = 0;
} else {
port++;
}
} else {
logger.error(`Failed to start server: ${e.message}`);
throw e;
}
}
}
}
}

Expand Down

0 comments on commit 98023ea

Please sign in to comment.