in sdk/server-proxies/src/tunnels/TunnelConnection.ts [320:363]
public async startConnectionAsync(target: ConnectionTarget, shouldRetry?: (e: unknown, retryCount: number) => boolean, abortSignal?: AbortSignalLike): Promise<string> {
if (this._stopped) {
throw new Error(`Lifetime has stopped, hub: ${this.hub}`);
}
const url = this.getUrl(target, this.hub);
const client = new WebPubSubTunnelClient(url, this.credential, this.id, target.target);
logger.verbose(`Starting connection: ${client.id}, hub: ${this.hub}`);
client.on("stop", () => {
logger.warning(`Client ${client.getPrintableIdentifier()} stopped, hub: ${this.hub}`);
this.tryEndLife(client.id);
});
client.on("message", () => {
while (client.messageQueue.length > 0) {
const message = client.messageQueue.shift();
if (message) {
this.processMessage(client, message, abortSignal);
}
}
});
this.clients.set(client.id, client);
let retryAttempt = 0;
let retry = false;
do {
if (abortSignal?.aborted || this._stopped) {
throw new Error(`Stop starting new client for aborted or stopped`);
}
try {
await client.startAsync(abortSignal);
logger.info(`Connected connections: (${this.clients.size})`);
logger.verbose(Array.from(this.printClientLines()).join("\n"));
return client.id;
} catch (err) {
retryAttempt++;
retry = shouldRetry !== undefined && shouldRetry(err, retryAttempt);
if (retry){
logger.info(`Error starting client ${client.getPrintableIdentifier()}: ${err}, retry ${retryAttempt} in 2 seconds, hub: ${this.hub}`);
await delay(2000);
} else {
throw err;
}
}
} while(retry);
throw "Unexpected";
}