Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(attachment): convoy separation (#7449) #7451

Merged
merged 2 commits into from
Jun 28, 2024
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "form-frontend",
"version": "6.130.0",
"version": "6.130.1",
"homepage": ".",
"private": true,
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())
}
Expand Down Expand Up @@ -141,6 +149,7 @@ const useDecryptionWorkers = ({
const downloadStartTime = performance.now()

let progress = 0
let timeSinceLastXAttachmentDownload = 0

return new Promise<DownloadResult>((resolve, reject) => {
reader
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/utils/waitForMs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const waitForMs = (ms: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
Expand Down
Loading