1
0
Fork 0

Added custom promise with timeout

pull/1140/head
Sankalp Kotewar 2022-08-03 13:25:47 +00:00
parent 970264135a
commit 8be69a26ed
2 changed files with 30 additions and 16 deletions

View File

@ -8,6 +8,7 @@ import {
const useAzureSdk = true
const downloadConcurrency = 8
const timeoutInMs = 30000
const abortTimeInMs = 2700000
const uploadConcurrency = 4
const uploadChunkSize = 32 * 1024 * 1024
@ -17,7 +18,8 @@ test('getDownloadOptions sets defaults', async () => {
expect(actualOptions).toEqual({
useAzureSdk,
downloadConcurrency,
timeoutInMs
timeoutInMs,
abortTimeInMs
})
})
@ -25,7 +27,8 @@ test('getDownloadOptions overrides all settings', async () => {
const expectedOptions: DownloadOptions = {
useAzureSdk: false,
downloadConcurrency: 14,
timeoutInMs: 20000
timeoutInMs: 20000,
abortTimeInMs: 2700000
}
const actualOptions = getDownloadOptions(expectedOptions)

View File

@ -6,7 +6,6 @@ import * as buffer from 'buffer'
import * as fs from 'fs'
import * as stream from 'stream'
import * as util from 'util'
import * as timer from 'timers/promises'
import * as utils from './cacheUtils'
import {SocketTimeout} from './constants'
@ -251,7 +250,6 @@ export async function downloadCacheStorageSDK(
try {
downloadProgress.startDisplayTimer()
const controller = new AbortController();
const timerController = new AbortController();
const abortSignal = controller.signal;
while (!downloadProgress.isDone()) {
const segmentStart =
@ -263,7 +261,8 @@ export async function downloadCacheStorageSDK(
)
downloadProgress.nextSegment(segmentSize)
const result = await Promise.race([client.downloadToBuffer(
const abortTimeInMs = options.abortTimeInMs == undefined ? 2700000 : options.abortTimeInMs;
const result = await promiseWithTimeout(abortTimeInMs, client.downloadToBuffer(
segmentStart,
segmentSize,
{
@ -271,14 +270,11 @@ export async function downloadCacheStorageSDK(
concurrency: options.downloadConcurrency,
onProgress: downloadProgress.onProgress()
}
),
timer.setTimeout(options.abortTimeInMs, 'timeout',{ signal: timerController.signal })]);
));
if(result === 'timeout') {
controller.abort();
throw new Error("Download aborted, segment download timed out.");
} else {
timerController.abort();
core.debug("Download completely successfully, cancelling timer.")
}
@ -290,3 +286,18 @@ export async function downloadCacheStorageSDK(
}
}
}
const promiseWithTimeout = (timeoutMs: number, promise: Promise<any>) => {
let timeoutHandle: NodeJS.Timeout;
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs);
});
return Promise.race([
promise,
timeoutPromise,
]).then((result) => {
clearTimeout(timeoutHandle);
return result;
});
}