Akshar2325 commited on
Commit
3a4ec11
·
1 Parent(s): f8451c5

⚡️ perf(upload): optimize minio file upload performance

Browse files

- switch to file streaming for single file uploads to reduce memory usage
- implement adaptive concurrency for parallel uploads based on cpu cores
- improve resource utilization and upload speed for multi-file operations

src/shared/modules/upload/new-minio-storage/new-minio-storage.service.ts CHANGED
@@ -10,6 +10,7 @@ import { FileType } from './enums/file-type.enum';
10
  import * as fs from 'fs/promises';
11
  import { createReadStream } from 'fs';
12
  import * as path from 'path';
 
13
  import pLimit from 'p-limit';
14
  import { HlsConversionService } from '../../hls-conversion/hls-conversion.service';
15
  import { UploadCoreService } from 'src/core/upload-core/upload-core.service';
@@ -203,13 +204,13 @@ export class MinioStorageService {
203
  try {
204
  const fileName = path.basename(filePath);
205
  const key = `${folderPath}/${fileName}`;
206
- const fileBuffer = await fs.readFile(filePath);
207
 
208
  await this.s3Client.send(
209
  new PutObjectCommand({
210
  Bucket: this.bucket,
211
  Key: key,
212
- Body: fileBuffer,
213
  ContentType: contentType,
214
  }),
215
  );
@@ -304,8 +305,16 @@ export class MinioStorageService {
304
  }
305
  }
306
 
307
- // Upload in parallel with concurrency limit
308
- const limit = pLimit(8);
 
 
 
 
 
 
 
 
309
  const uploadResults = await Promise.all(
310
  allFiles.map((file) =>
311
  limit(async () => {
 
10
  import * as fs from 'fs/promises';
11
  import { createReadStream } from 'fs';
12
  import * as path from 'path';
13
+ import * as os from 'os';
14
  import pLimit from 'p-limit';
15
  import { HlsConversionService } from '../../hls-conversion/hls-conversion.service';
16
  import { UploadCoreService } from 'src/core/upload-core/upload-core.service';
 
204
  try {
205
  const fileName = path.basename(filePath);
206
  const key = `${folderPath}/${fileName}`;
207
+ const stream = createReadStream(filePath);
208
 
209
  await this.s3Client.send(
210
  new PutObjectCommand({
211
  Bucket: this.bucket,
212
  Key: key,
213
+ Body: stream,
214
  ContentType: contentType,
215
  }),
216
  );
 
305
  }
306
  }
307
 
308
+ // Upload in parallel with adaptive concurrency limit based on CPU count
309
+ const cpuCount = os.cpus().length;
310
+ const CONCURRENCY = Math.min(
311
+ Math.max(cpuCount * 2, 4), // minimum 4
312
+ 12, // hard cap at 12
313
+ );
314
+ const limit = pLimit(CONCURRENCY);
315
+ this.logger.log(
316
+ `🚀 Uploading with concurrency: ${CONCURRENCY} (CPU cores: ${cpuCount})`,
317
+ );
318
  const uploadResults = await Promise.all(
319
  allFiles.map((file) =>
320
  limit(async () => {