Skip to content

Commit

Permalink
Merge pull request #1060 from internxt/feat/manage-concurrency-on-chrome
Browse files Browse the repository at this point in the history
[_]: feat/dynamic-upload-concurrency-depending-on-ram-chrome
  • Loading branch information
sg-gs authored Mar 19, 2024
2 parents ee77737 + 143e09e commit a025a56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/app/network/UploadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const uploadFileWithManager = (
};

class UploadManager {
private currentGroupBeingUploaded: FileSizeType = FileSizeType.Small;
private filesGroups: Record<
FileSizeType,
{
Expand Down Expand Up @@ -101,6 +102,33 @@ class UploadManager {
(fileData, next: (err: Error | null, res?: DriveFileData) => void) => {
if (this.abortController?.signal.aborted ?? fileData.abortController?.signal.aborted) return;

if (window.performance && (window.performance as any).memory) {
const memory = window.performance.memory;

if (memory && memory.jsHeapSizeLimit !== null && memory.usedJSHeapSize !== null) {
const memoryUsagePercentage = memory.usedJSHeapSize / memory.jsHeapSizeLimit;
const shouldIncreaseConcurrency = memoryUsagePercentage < 0.7 && this.currentGroupBeingUploaded !== FileSizeType.Big;

if (shouldIncreaseConcurrency) {
const newConcurrency = Math.min(
this.uploadQueue.concurrency + 1,
this.filesGroups[FileSizeType.Small].concurrency
);
console.warn(`Memory usage under 70%. Increasing upload concurrency to ${newConcurrency}`);
this.uploadQueue.concurrency = newConcurrency;
}

const shouldReduceConcurrency = memoryUsagePercentage >= 0.8 && this.uploadQueue.concurrency > 1;

if (shouldReduceConcurrency) {
console.warn('Memory usage reached 80%. Reducing upload concurrency.');
this.uploadQueue.concurrency = 1;
}
}
} else {
console.warn('Memory usage control is not available');
}

let uploadAttempts = 0;
const uploadId = randomBytes(10).toString('hex');
const taskId = fileData.taskId;
Expand Down Expand Up @@ -454,8 +482,12 @@ class UploadManager {

if (smallSizedFiles.length > 0) await uploadFiles(smallSizedFiles, this.filesGroups.small.concurrency);

this.currentGroupBeingUploaded = FileSizeType.Medium;

if (mediumSizedFiles.length > 0) await uploadFiles(mediumSizedFiles, this.filesGroups.medium.concurrency);

this.currentGroupBeingUploaded = FileSizeType.Big;

if (bigSizedFiles.length > 0) await uploadFiles(bigSizedFiles, this.filesGroups.big.concurrency);

return uploadedFilesData;
Expand Down
7 changes: 7 additions & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ interface Window {
ready: (cb: () => void) => void;
execute: (siteKey: string, { action: string }) => Promise<string>;
};
performance: {
memory?: {
jsHeapSizeLimit: number;
totalJSHeapSize: number;
usedJSHeapSize: number;
};
}
}

interface Navigator {
Expand Down

0 comments on commit a025a56

Please sign in to comment.