Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Reduce amount of simultaneous open files

Jonny Burgerf0008aabf123
diff --git a/packages/lambda/src/api/upload-dir.ts b/packages/lambda/src/api/upload-dir.ts
index c0918071ed..542fd53b86 100644
--- a/packages/lambda/src/api/upload-dir.ts
+++ b/packages/lambda/src/api/upload-dir.ts
@@ -6,6 +6,7 @@ import path from 'node:path';
import type {Privacy} from '../defaults';
import type {AwsRegion} from '../pricing/aws-regions';
import {getS3Client} from '../shared/aws-clients';
+import {chunk} from '../shared/chunk';
import {makeS3Key} from '../shared/make-s3-key';
type FileInfo = {
@@ -95,34 +96,41 @@ export const uploadDir = async ({
const client = getS3Client(region, null);
- const uploads = files.map((filePath) => {
- const Key = makeS3Key(keyPrefix, localDir, filePath.name);
- const Body = createReadStream(filePath.name);
- const ContentType = mimeTypes.lookup(Key) || 'application/octet-stream';
- const ACL =
- privacy === 'no-acl'
- ? undefined
- : privacy === 'private'
- ? 'private'
- : 'public-read';
- const paralellUploads3 = new Upload({
- client,
- queueSize: 4,
- partSize: 5 * 1024 * 1024,
- params: {
- Key,
- Bucket: bucket,
- Body,
- ACL,
- ContentType,
- },
- });
- paralellUploads3.on('httpUploadProgress', (progress) => {
- progresses[filePath.name] = progress.loaded ?? 0;
- });
- return paralellUploads3.done();
- });
- const promise = Promise.all(uploads);
+ const chunkedFiles = chunk(files, 200);
+
+ const uploadAll = (async () => {
+ for (const filesChunk of chunkedFiles) {
+ const uploads = filesChunk.map((filePath) => {
+ const Key = makeS3Key(keyPrefix, localDir, filePath.name);
+ const Body = createReadStream(filePath.name);
+ const ContentType = mimeTypes.lookup(Key) || 'application/octet-stream';
+ const ACL =
+ privacy === 'no-acl'
+ ? undefined
+ : privacy === 'private'
+ ? 'private'
+ : 'public-read';
+
+ const paralellUploads3 = new Upload({
+ client,
+ queueSize: 4,
+ partSize: 5 * 1024 * 1024,
+ params: {
+ Key,
+ Bucket: bucket,
+ Body,
+ ACL,
+ ContentType,
+ },
+ });
+ paralellUploads3.on('httpUploadProgress', (progress) => {
+ progresses[filePath.name] = progress.loaded ?? 0;
+ });
+ return paralellUploads3.done();
+ });
+ await Promise.all(uploads);
+ }
+ })();
const interval = setInterval(() => {
onProgress({
@@ -132,6 +140,6 @@ export const uploadDir = async ({
filesUploaded: files.filter((f) => progresses[f.name] === f.size).length,
});
}, 1000);
- await promise;
+ await uploadAll;
clearInterval(interval);
};
diff --git a/packages/lambda/src/shared/chunk.ts b/packages/lambda/src/shared/chunk.ts
new file mode 100644
index 0000000000..6c2ae809c2
--- /dev/null
+++ b/packages/lambda/src/shared/chunk.ts
@@ -0,0 +1,7 @@
+export const chunk = <T>(input: T[], size: number) => {
+ return input.reduce<T[][]>((arr, item, idx) => {
+ return idx % size === 0
+ ? [...arr, [item]]
+ : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
+ }, []);
+};