Skip to content

Commit 02f37d5

Browse files
committed
Avoid duplicate parallel 'is tunnel running' checks
1 parent 36a1aae commit 02f37d5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/interceptors/docker/docker-tunnel-proxy.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ export async function prepareDockerTunnel() {
2626
});
2727
}
2828

29+
// We use this to avoid duplicate ensureRunning calls - collapsing them instead into a single promise.
30+
const ongoingEnsureTunnelRunningChecks: { [port: number]: Promise<void> | undefined } = {};
31+
2932
// Fully check that the container is created, up & running, recreating it if not.
3033
// This does *not* connect any networks, so most usage will need to connect up the
3134
// networks with updateTunnelledNetworks afterwards.
32-
export async function ensureDockerTunnelRunning(proxyPort: number) {
33-
await containerMutex.runExclusive(async () => {
35+
export function ensureDockerTunnelRunning(proxyPort: number) {
36+
if (ongoingEnsureTunnelRunningChecks[proxyPort]) {
37+
return ongoingEnsureTunnelRunningChecks[proxyPort]!;
38+
}
39+
40+
ongoingEnsureTunnelRunningChecks[proxyPort] = containerMutex.runExclusive(async () => {
3441
const docker = new Docker();
3542

3643
// Make sure we have the image available (should've been pre-pulled, but just in case)
@@ -100,7 +107,12 @@ export async function ensureDockerTunnelRunning(proxyPort: number) {
100107

101108
// Asynchronously, update the Docker port that's in use for this container.
102109
portCache[proxyPort] = refreshDockerTunnelPortCache(proxyPort);
110+
}).finally(() => {
111+
// Clean up the promise, so that future calls to ensureRunning re-run this check.
112+
ongoingEnsureTunnelRunningChecks[proxyPort] = undefined;
103113
});
114+
115+
return ongoingEnsureTunnelRunningChecks[proxyPort]!;
104116
}
105117

106118
// Update the containers network connections. If the container isn't running, this

0 commit comments

Comments
 (0)