From 6a0823f70045c42735135838dd630f73ae99eb0c Mon Sep 17 00:00:00 2001 From: Ken Lee Shu Ming Date: Thu, 27 Jun 2024 21:43:58 +0800 Subject: [PATCH 1/2] fix(attachment): convoy separation (#7449) * feat: add min separation time between each convoy * fix: do not wait when waitTime is 0 * chore: fix typo * fix: reduce convoy size to 5 --- .../storage/useDecryptionWorkers.ts | 26 +++++++++++++++++++ frontend/src/utils/waitForMs.ts | 5 ++++ 2 files changed, 31 insertions(+) create mode 100644 frontend/src/utils/waitForMs.ts diff --git a/frontend/src/features/admin-form/responses/ResponsesPage/storage/useDecryptionWorkers.ts b/frontend/src/features/admin-form/responses/ResponsesPage/storage/useDecryptionWorkers.ts index 8335d7edf3..13b745a086 100644 --- a/frontend/src/features/admin-form/responses/ResponsesPage/storage/useDecryptionWorkers.ts +++ b/frontend/src/features/admin-form/responses/ResponsesPage/storage/useDecryptionWorkers.ts @@ -2,6 +2,8 @@ import { useCallback, useEffect, useRef, useState } from 'react' import { useMutation, UseMutationOptions } from 'react-query' import { datadogLogs } from '@datadog/browser-logs' +import { waitForMs } from '~utils/waitForMs' + import { useAdminForm } from '~features/admin-form/common/queries' import { trackDownloadNetworkFailure, @@ -27,6 +29,12 @@ import { const NUM_OF_METADATA_ROWS = 5 +// We will download attachments in convoys of 5 +// This is to prevent the script from downloading too many attachments at once +// which could cause it to block downloads. +const ATTACHMENT_DOWNLOAD_CONVOY_SIZE = 5 +const ATTACHMENT_DOWNLOAD_CONVOY_MINIMUM_SEPARATION_TIME = 1000 + const killWorkers = (workers: CleanableDecryptionWorkerApi[]): void => { return workers.forEach((worker) => worker.cleanup()) } @@ -141,6 +149,7 @@ const useDecryptionWorkers = ({ const downloadStartTime = performance.now() let progress = 0 + let timeSinceLastXAttachmentDownload = 0 return new Promise((resolve, reject) => { reader @@ -181,7 +190,24 @@ const useDecryptionWorkers = ({ errorCount++ console.error('Error in getResponseInstance', e) } + if (downloadAttachments && decryptResult.downloadBlob) { + // Ensure attachments downloads are spaced out to avoid browser blocking downloads + if (progress % ATTACHMENT_DOWNLOAD_CONVOY_SIZE === 0) { + const now = new Date().getTime() + const elapsedSinceXDownloads = + now - timeSinceLastXAttachmentDownload + + const waitTime = Math.max( + 0, + ATTACHMENT_DOWNLOAD_CONVOY_MINIMUM_SEPARATION_TIME - + elapsedSinceXDownloads, + ) + if (waitTime > 0) { + await waitForMs(waitTime) + } + timeSinceLastXAttachmentDownload = now + } await downloadResponseAttachment( decryptResult.downloadBlob, decryptResult.id, diff --git a/frontend/src/utils/waitForMs.ts b/frontend/src/utils/waitForMs.ts new file mode 100644 index 0000000000..f04e7a6df5 --- /dev/null +++ b/frontend/src/utils/waitForMs.ts @@ -0,0 +1,5 @@ +export const waitForMs = (ms: number): Promise => { + return new Promise((resolve) => { + setTimeout(resolve, ms) + }) +} From f1acaf068c3bf9a579c76021674c9c59212958bb Mon Sep 17 00:00:00 2001 From: Ken Date: Thu, 27 Jun 2024 21:44:54 +0800 Subject: [PATCH 2/2] chore: bump version to v6.130.1 --- CHANGELOG.md | 8 ++++++++ frontend/package-lock.json | 4 ++-- frontend/package.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37581061e8..97ebbaedb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,21 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v6.130.1](https://github.com/opengovsg/FormSG/compare/v6.130.0...v6.130.1) + +- fix(attachment): convoy separation [`#7449`](https://github.com/opengovsg/FormSG/pull/7449) +- build: release v6.130.0 [`#7441`](https://github.com/opengovsg/FormSG/pull/7441) + #### [v6.130.0](https://github.com/opengovsg/FormSG/compare/v6.129.0...v6.130.0) +> 26 June 2024 + - feat: update to platform flag [`#7437`](https://github.com/opengovsg/FormSG/pull/7437) - feat(mrf): multiple static emails [`#7433`](https://github.com/opengovsg/FormSG/pull/7433) - build: merge release v6.129.0 to develop [`#7434`](https://github.com/opengovsg/FormSG/pull/7434) - chore(deps-dev): bump axios-mock-adapter from 1.21.4 to 1.22.0 [`#7435`](https://github.com/opengovsg/FormSG/pull/7435) - build: release v6.129.0 [`#7432`](https://github.com/opengovsg/FormSG/pull/7432) +- chore: bump version to v6.130.0 [`295ed77`](https://github.com/opengovsg/FormSG/commit/295ed7717a904ef19b32531e0f2e7dfcc919fa87) #### [v6.129.0](https://github.com/opengovsg/FormSG/compare/v6.128.0...v6.129.0) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index ae8a7b53ae..4f4a83d46f 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -1,12 +1,12 @@ { "name": "form-frontend", - "version": "6.130.0", + "version": "6.130.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "form-frontend", - "version": "6.130.0", + "version": "6.130.1", "hasInstallScript": true, "dependencies": { "@chakra-ui/react": "^1.8.6", diff --git a/frontend/package.json b/frontend/package.json index 50fe84ac49..a9fa1333f6 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "form-frontend", - "version": "6.130.0", + "version": "6.130.1", "homepage": ".", "private": true, "dependencies": { diff --git a/package-lock.json b/package-lock.json index eafb3a3582..d9648ddc51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "FormSG", - "version": "6.130.0", + "version": "6.130.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "FormSG", - "version": "6.130.0", + "version": "6.130.1", "hasInstallScript": true, "dependencies": { "@aws-sdk/client-cloudwatch-logs": "^3.536.0", diff --git a/package.json b/package.json index 4146e6c552..02134d46b8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "FormSG", "description": "Form Manager for Government", - "version": "6.130.0", + "version": "6.130.1", "homepage": "https://form.gov.sg", "authors": [ "FormSG "