Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/interceptors/docker/docker-interception-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ import {
stopDockerTunnel,
} from './docker-tunnel-proxy';
import { ensureDockerInjectionVolumeExists } from './docker-data-injection';
import { withTimeout } from '../../util/promise';

let dockerAvailableCache: Promise<boolean> | undefined;

const DOCKER_AVAILABILITY_TIMEOUT_MS = 3_000;

export const isDockerAvailable = (options: { logError?: boolean } = {}) => {
if (dockerAvailableCache) return dockerAvailableCache;
else {
dockerAvailableCache = (async () => { // Catch sync & async setup errors
return new Docker().info();
return withTimeout(DOCKER_AVAILABILITY_TIMEOUT_MS, new Docker().info());
})()
.then((info: { OSType?: 'windows' | 'linux' }) => {
if (info.OSType === 'windows') {
Expand Down Expand Up @@ -203,4 +206,4 @@ export async function deleteAllInterceptedDockerData(proxyPort: number | 'all'):
delete pendingDeactivations[proxyPort];
})()
]) as Promise<unknown> as Promise<void>;
}
}
9 changes: 6 additions & 3 deletions src/util/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export async function waitUntil<T extends unknown>(
}

export class TimeoutError extends CustomError {
constructor() {
super('Timeout', { code: 'timeout' });
constructor(timeoutMs?: number) {
super(
timeoutMs === undefined ? 'Timeout' : `Timeout after ${timeoutMs}ms`,
{ code: 'timeout' }
);
}
}

Expand All @@ -35,6 +38,6 @@ export async function withTimeout<T>(
return Promise.race([
promise,
delay(timeoutMs, { unref: true })
.then(() => { throw new TimeoutError(); })
.then(() => { throw new TimeoutError(timeoutMs); })
]);
}