From 3100043bb1b22fc0ec988aba7eca5b96c6f17888 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Fri, 9 Aug 2024 13:28:18 +0200 Subject: [PATCH] Add time out to AWSClient (#5326) --- .../providers/artifact-storage-writer.ts | 6 +++++ packages/services/cdn-worker/src/aws.ts | 25 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/services/api/src/modules/schema/providers/artifact-storage-writer.ts b/packages/services/api/src/modules/schema/providers/artifact-storage-writer.ts index de33f5271..c62d17057 100644 --- a/packages/services/api/src/modules/schema/providers/artifact-storage-writer.ts +++ b/packages/services/api/src/modules/schema/providers/artifact-storage-writer.ts @@ -62,6 +62,9 @@ export class ArtifactStorageWriter { // This boolean makes Google Cloud Storage & AWS happy. signQuery: true, }, + // Add a timeout to prevent hanging the request. + // Should be more than enough to write an artifact. + timeout: 10_000, }); if (result.status !== 200) { @@ -95,6 +98,9 @@ export class ArtifactStorageWriter { // This boolean makes Google Cloud Storage & AWS happy. signQuery: true, }, + // Add a timeout to prevent hanging the request. + // Should be more than enough to delete an artifact. + timeout: 5_000, }); if (result.status !== 204) { diff --git a/packages/services/cdn-worker/src/aws.ts b/packages/services/cdn-worker/src/aws.ts index 833861e68..53a11b75d 100644 --- a/packages/services/cdn-worker/src/aws.ts +++ b/packages/services/cdn-worker/src/aws.ts @@ -45,6 +45,10 @@ type AwsRequestInit = RequestInit & { allHeaders?: boolean; singleEncode?: boolean; }; + /** + * Timeout in milliseconds for each fetch call. + */ + timeout?: number; }; export class AwsClient { @@ -91,7 +95,15 @@ export class AwsClient { async sign(input: RequestInfo, init?: AwsRequestInit) { if (input instanceof Request) { const { method, url, headers, body } = input; - init = Object.assign({ method, url, headers }, init); + init = Object.assign( + { + method, + url, + headers, + signal: init?.timeout ? AbortSignal.timeout(init.timeout) : undefined, + }, + init, + ); if (init.body == null && headers.has('Content-Type')) { init.body = body != null && headers.has('X-Amz-Content-Sha256') @@ -120,9 +132,14 @@ export class AwsClient { if (i === this.retries) { return fetched; // No need to await if we're returning anyway } - const res = await fetched; - if (res.status < 500 && res.status !== 429 && res.status !== 499) { - return res; + try { + const res = await fetched; + if (res.status < 500 && res.status !== 429 && res.status !== 499) { + return res; + } + } catch (error) { + // Retry also when there's an exception + console.error(error); } await new Promise(resolve => setTimeout(resolve, Math.random() * this.initRetryMs * Math.pow(2, i)),