@@ -26,11 +26,18 @@ export async function prepareDockerTunnel() {
26
26
} ) ;
27
27
}
28
28
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
+
29
32
// Fully check that the container is created, up & running, recreating it if not.
30
33
// This does *not* connect any networks, so most usage will need to connect up the
31
34
// 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 ( ) => {
34
41
const docker = new Docker ( ) ;
35
42
36
43
// 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) {
100
107
101
108
// Asynchronously, update the Docker port that's in use for this container.
102
109
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 ;
103
113
} ) ;
114
+
115
+ return ongoingEnsureTunnelRunningChecks [ proxyPort ] ! ;
104
116
}
105
117
106
118
// Update the containers network connections. If the container isn't running, this
0 commit comments