Skip to content

Commit

Permalink
Limit imports in flight for getCollection (withastro#10708)
Browse files Browse the repository at this point in the history
  • Loading branch information
horo-fox authored and PeterDraex committed Apr 23, 2024
1 parent 654f5dc commit dcf13b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-zoos-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Limits parallel imports within `getCollection()` to prevent EMFILE errors when accessing files
6 changes: 4 additions & 2 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { MarkdownHeading } from '@astrojs/markdown-remark';
import pLimit from 'p-limit';
import { ZodIssueCode, string as zodString } from 'zod';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { prependForwardSlash } from '../core/path.js';
Expand Down Expand Up @@ -80,8 +81,9 @@ export function createGetCollection({
// Always return a new instance so consumers can safely mutate it
entries = [...cacheEntriesByCollection.get(collection)!];
} else {
const limit = pLimit(10);
entries = await Promise.all(
lazyImports.map(async (lazyImport) => {
lazyImports.map((lazyImport) => limit(async () => {
const entry = await lazyImport();
return type === 'content'
? {
Expand All @@ -103,7 +105,7 @@ export function createGetCollection({
collection: entry.collection,
data: entry.data,
};
})
}))
);
cacheEntriesByCollection.set(collection, entries);
}
Expand Down

0 comments on commit dcf13b1

Please sign in to comment.