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

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 7
// 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 = 7
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) {
KenLSM marked this conversation as resolved.
Show resolved Hide resolved
const now = new Date().getTime()
const elaspedSinceXDownloads =
KenLSM marked this conversation as resolved.
Show resolved Hide resolved
now - timeSinceLastXAttachmentDownload

const waitTime = Math.max(
0,
ATTACHMENT_DOWNLOAD_CONVOY_MINIMUM_SEPARATION_TIME -
elaspedSinceXDownloads,
)
if (waitTime >= 0) {
await waitForMs(waitTime)
}
KenLSM marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
Loading