Skip to content

Add UploadNormalizationLimiter and overlap writes - #192

Merged
kmandryk merged 4 commits into
developfrom
AI-1240
Jun 16, 2026
Merged

Add UploadNormalizationLimiter and overlap writes#192
kmandryk merged 4 commits into
developfrom
AI-1240

Conversation

@kmandryk

@kmandryk kmandryk commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Introduce an in-process UploadNormalizationLimiter to bound concurrent PDF/image normalization (Math.max(2, availableParallelism())). Start the original blob write concurrently with normalization to overlap I/O and CPU, await the original write before writing the normalized blob, and release the decoded upload buffer early to reduce peak memory. Move expensive pdf-lib parsing into normalizeToPdf (validateForUpload now does a cheap magic-byte probe for PDFs), add robust parse/error handling, and generate thumbnails from the normalized PDF bytes. Wire the limiter into the Document module/service, add unit tests for the limiter and updated upload behavior/order, and update documentation to describe the upload memory/concurrency model and API implications.

Summary

address upload-ocr bottleneck
AI-1240

Testing

Re-run payload-sizes large × 5 VU on the load-test instance to validate OOM/failure-rate acceptance criteria

Introduce an in-process UploadNormalizationLimiter to bound concurrent PDF/image normalization (Math.max(2, availableParallelism())). Start the original blob write concurrently with normalization to overlap I/O and CPU, await the original write before writing the normalized blob, and release the decoded upload buffer early to reduce peak memory. Move expensive pdf-lib parsing into normalizeToPdf (validateForUpload now does a cheap magic-byte probe for PDFs), add robust parse/error handling, and generate thumbnails from the normalized PDF bytes. Wire the limiter into the Document module/service, add unit tests for the limiter and updated upload behavior/order, and update documentation to describe the upload memory/concurrency model and API implications.

@dbarkowsky dbarkowsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just requesting docs on the Semaphore file.
The other comment is just a hypothetical unless it's something you want to address.

Comment thread apps/backend-services/src/document/document.service.ts
Comment thread apps/backend-services/src/upload/upload-normalization-limiter.ts
kmandryk added 2 commits June 16, 2026 12:11
Introduce an in-process Semaphore and UploadNormalizationLimiter to cap concurrent PDF/image normalization per process (uses availableParallelism/cpus and floors to at least 2). Semaphore enqueues waiters FIFO and exposes a run() helper. DocumentService now awaits the original write before recording a conversion failure and intentionally retains the original blob on normalization failure so clients can still download the upload. Update docs to explain that normalized_file_path is absent on failure but file_path remains valid.
@kmandryk
kmandryk merged commit 1f3c993 into develop Jun 16, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants