Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[front] Handle backpressure manually to avoid getting stuck #9575

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion front/lib/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,32 @@ const extractTextFromFileAndUpload: ProcessingFunction = async (
config.getTextExtractionUrl()
).fromStream(readStream, file.contentType);

await pipeline(processedStream, writeStream);
processedStream.on("data", (chunk) => {
// Handle backpressure
const canWrite = writeStream.write(chunk);
if (!canWrite) {
readStream.pause();
writeStream.once("drain", () => readStream.resume());
}
});

processedStream.on("end", () => {
writeStream.end();
});

processedStream.on("error", (err) => {
throw err;
});

writeStream.on("error", (err) => {
throw err;
});

// Await the completion of the writeStream
await new Promise<void>((resolve, reject) => {
writeStream.on("finish", resolve);
writeStream.on("error", reject);
});

return new Ok(undefined);
} catch (err) {
Expand Down
Loading