Skip to content

Commit

Permalink
Batch file uploads to avoid EMFILE (#5632)
Browse files Browse the repository at this point in the history
* Potentially fix #1586

* Rewrite comment

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>

* Run prettier (oops)

---------

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
  • Loading branch information
horo-fox and petebacondarwin authored Jun 6, 2024
1 parent 314f7c4 commit 8741b7f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/wrangler/src/pages/upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,23 @@ export const upload = async (
const doUpload = async (): Promise<void> => {
// Populate the payload only when actually uploading (this is limited to 3 concurrent uploads at 50 MiB per bucket meaning we'd only load in a max of ~150 MiB)
// This is so we don't run out of memory trying to upload the files.
const payload: UploadPayloadFile[] = await Promise.all(
bucket.files.map(async (file) => ({
key: file.hash,
value: (await readFile(file.path)).toString("base64"),
metadata: {
contentType: file.contentType,
},
base64: true,
}))
);
const payload: UploadPayloadFile[] = [];

for (let i = 0; i < bucket.files.length; i += 1000) {
// only read up to 1000 files, from disk, at a time to avoid `EMFILE` error (on Windows)
payload.push(
...(await Promise.all(
bucket.files.slice(i * 1000, (i + 1) * 1000).map(async (file) => ({
key: file.hash,
value: (await readFile(file.path)).toString("base64"),
metadata: {
contentType: file.contentType,
},
base64: true,
}))
))
);
}

try {
logger.debug("POST /pages/assets/upload");
Expand Down

0 comments on commit 8741b7f

Please sign in to comment.