Updates
This commit is contained in:
@@ -46,7 +46,7 @@ export default async function grabTtydServerInfo({
|
||||
);
|
||||
}
|
||||
|
||||
const available_port = getNextAvailablePort();
|
||||
const available_port = await getNextAvailablePort();
|
||||
|
||||
let url = `/ttyd/${available_port}`;
|
||||
|
||||
|
||||
@@ -25,19 +25,26 @@ function makeSockaddr(port: number): Buffer {
|
||||
return buf;
|
||||
}
|
||||
|
||||
export default function getNextAvailablePort(
|
||||
export default async function getNextAvailablePort(
|
||||
startPort = AppData["DynamicPortStart"],
|
||||
maxPort = 65535,
|
||||
): number {
|
||||
for (let port = startPort; port <= maxPort; port++) {
|
||||
const fd = libc.symbols.socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) continue;
|
||||
): Promise<number> {
|
||||
const MAX_RETRIES = 5;
|
||||
let retries = 0;
|
||||
|
||||
const addr = makeSockaddr(port);
|
||||
const result = libc.symbols.bind(fd, ptr(addr), addr.byteLength);
|
||||
libc.symbols.close(fd);
|
||||
while (retries < MAX_RETRIES) {
|
||||
for (let port = startPort; port <= maxPort; port++) {
|
||||
const fd = libc.symbols.socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) continue;
|
||||
|
||||
if (result === 0) return port;
|
||||
const addr = makeSockaddr(port);
|
||||
const result = libc.symbols.bind(fd, ptr(addr), addr.byteLength);
|
||||
libc.symbols.close(fd);
|
||||
|
||||
if (result === 0) return port;
|
||||
|
||||
await Bun.sleep(2000);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`No available port found in range ${startPort}-${maxPort}`);
|
||||
|
||||
Reference in New Issue
Block a user