2021-08-31 14:44:08 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-07 12:26:50 -08:00
|
|
|
import { ManualPromise } from './manualPromise';
|
2022-04-07 12:55:44 -08:00
|
|
|
import { monotonicTime } from './';
|
2021-08-31 14:44:08 -07:00
|
|
|
|
2022-01-25 11:22:28 -08:00
|
|
|
export class TimeoutRunnerError extends Error {}
|
|
|
|
|
|
|
|
type TimeoutRunnerData = {
|
2022-03-08 19:05:23 -08:00
|
|
|
lastElapsedSync: number,
|
2022-01-25 11:22:28 -08:00
|
|
|
timer: NodeJS.Timer | undefined,
|
|
|
|
timeoutPromise: ManualPromise<any>,
|
|
|
|
};
|
2022-04-07 12:26:50 -08:00
|
|
|
|
2022-01-25 11:22:28 -08:00
|
|
|
export class TimeoutRunner {
|
|
|
|
private _running: TimeoutRunnerData | undefined;
|
|
|
|
private _timeout: number;
|
|
|
|
private _elapsed: number;
|
|
|
|
|
|
|
|
constructor(timeout: number) {
|
|
|
|
this._timeout = timeout;
|
|
|
|
this._elapsed = 0;
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:22:28 -08:00
|
|
|
async run<T>(cb: () => Promise<T>): Promise<T> {
|
|
|
|
const running = this._running = {
|
2022-03-08 19:05:23 -08:00
|
|
|
lastElapsedSync: monotonicTime(),
|
2022-01-25 11:22:28 -08:00
|
|
|
timer: undefined,
|
|
|
|
timeoutPromise: new ManualPromise(),
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const resultPromise = Promise.race([
|
|
|
|
cb(),
|
|
|
|
running.timeoutPromise
|
|
|
|
]);
|
|
|
|
this._updateTimeout(running, this._timeout);
|
|
|
|
return await resultPromise;
|
|
|
|
} finally {
|
|
|
|
this._updateTimeout(running, 0);
|
|
|
|
if (this._running === running)
|
|
|
|
this._running = undefined;
|
|
|
|
}
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interrupt() {
|
2022-01-25 11:22:28 -08:00
|
|
|
if (this._running)
|
|
|
|
this._updateTimeout(this._running, -1);
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:05:23 -08:00
|
|
|
elapsed() {
|
|
|
|
this._syncElapsedAndStart();
|
|
|
|
return this._elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTimeout(timeout: number, elapsed?: number) {
|
2022-01-25 11:22:28 -08:00
|
|
|
this._timeout = timeout;
|
2022-03-08 19:05:23 -08:00
|
|
|
if (elapsed !== undefined) {
|
|
|
|
this._syncElapsedAndStart();
|
|
|
|
this._elapsed = elapsed;
|
|
|
|
}
|
2022-01-25 11:22:28 -08:00
|
|
|
if (this._running)
|
|
|
|
this._updateTimeout(this._running, timeout);
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:05:23 -08:00
|
|
|
private _syncElapsedAndStart() {
|
|
|
|
if (this._running) {
|
|
|
|
const now = monotonicTime();
|
|
|
|
this._elapsed += now - this._running.lastElapsedSync;
|
|
|
|
this._running.lastElapsedSync = now;
|
|
|
|
}
|
2022-01-25 11:22:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private _updateTimeout(running: TimeoutRunnerData, timeout: number) {
|
|
|
|
if (running.timer) {
|
|
|
|
clearTimeout(running.timer);
|
|
|
|
running.timer = undefined;
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
2022-03-08 19:05:23 -08:00
|
|
|
this._syncElapsedAndStart();
|
2022-01-25 11:22:28 -08:00
|
|
|
if (timeout === 0)
|
2021-08-31 14:44:08 -07:00
|
|
|
return;
|
2022-03-08 19:05:23 -08:00
|
|
|
timeout = timeout - this._elapsed;
|
2021-08-31 14:44:08 -07:00
|
|
|
if (timeout <= 0)
|
2022-01-25 11:22:28 -08:00
|
|
|
running.timeoutPromise.reject(new TimeoutRunnerError());
|
2021-08-31 14:44:08 -07:00
|
|
|
else
|
2022-01-25 11:22:28 -08:00
|
|
|
running.timer = setTimeout(() => running.timeoutPromise.reject(new TimeoutRunnerError()), timeout);
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:22:28 -08:00
|
|
|
export async function raceAgainstTimeout<T>(cb: () => Promise<T>, timeout: number): Promise<{ result: T, timedOut: false } | { timedOut: true }> {
|
|
|
|
const runner = new TimeoutRunner(timeout);
|
|
|
|
try {
|
|
|
|
return { result: await runner.run(cb), timedOut: false };
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TimeoutRunnerError)
|
|
|
|
return { timedOut: true };
|
|
|
|
throw e;
|
|
|
|
}
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
2023-01-05 11:14:37 -08:00
|
|
|
|
|
|
|
export async function pollAgainstTimeout<T>(callback: () => Promise<{ continuePolling: boolean, result: T }>, timeout: number, pollIntervals: number[] = [100, 250, 500, 1000]): Promise<{ result?: T, timedOut: boolean }> {
|
|
|
|
const startTime = monotonicTime();
|
|
|
|
const lastPollInterval = pollIntervals.pop() ?? 1000;
|
|
|
|
let lastResult: T|undefined;
|
|
|
|
const wrappedCallback = () => Promise.resolve().then(callback);
|
|
|
|
while (true) {
|
|
|
|
const elapsed = monotonicTime() - startTime;
|
|
|
|
if (timeout !== 0 && elapsed > timeout)
|
|
|
|
break;
|
|
|
|
const received = timeout !== 0 ? await raceAgainstTimeout(wrappedCallback, timeout - elapsed)
|
|
|
|
: await wrappedCallback().then(value => ({ result: value, timedOut: false }));
|
|
|
|
if (received.timedOut)
|
|
|
|
break;
|
|
|
|
lastResult = received.result.result;
|
|
|
|
if (!received.result.continuePolling)
|
|
|
|
return { result: received.result.result, timedOut: false };
|
|
|
|
await new Promise(x => setTimeout(x, pollIntervals!.shift() ?? lastPollInterval));
|
|
|
|
}
|
|
|
|
return { timedOut: true, result: lastResult };
|
|
|
|
}
|