Parallelize message processing and batch IMAP fetches #3

Closed
opened 2026-05-11 00:31:33 +02:00 by heiko · 0 comments
Owner

Problem

Messages are processed sequentially: fetch → parse MIME → scan attachments → store flag. For large mailboxes this is slow because each FetchMessage and AddFlag is a synchronous, blocking IMAP command.

Proposal

  • Batch initial fetch: replace N individual UID FETCH <uid> BODY[] calls with a single UID FETCH <set> BODY[] stream — the library returns messages as they arrive
  • Parallelize detection: XRechnung detection (XML/PDF parsing) is CPU-bound and can run concurrently across fetched messages
  • Batch flag store: accumulate matched UIDs during a scan, then issue a single UID STORE <set> +FLAGS ($xrechnung) at the end

Notes

  • The IMAP connection uses a mutex (sync.Mutex) so concurrent IMAP commands need careful sequencing or a command pipeline
  • IDLE loop handles one message at a time — parallelism only helps the initial full-mailbox scan
  • Benchmark before optimising; for typical invoice volumes the current approach may be fast enough
## Problem Messages are processed sequentially: fetch → parse MIME → scan attachments → store flag. For large mailboxes this is slow because each `FetchMessage` and `AddFlag` is a synchronous, blocking IMAP command. ## Proposal - **Batch initial fetch**: replace N individual `UID FETCH <uid> BODY[]` calls with a single `UID FETCH <set> BODY[]` stream — the library returns messages as they arrive - **Parallelize detection**: XRechnung detection (XML/PDF parsing) is CPU-bound and can run concurrently across fetched messages - **Batch flag store**: accumulate matched UIDs during a scan, then issue a single `UID STORE <set> +FLAGS ($xrechnung)` at the end ## Notes - The IMAP connection uses a mutex (`sync.Mutex`) so concurrent IMAP commands need careful sequencing or a command pipeline - IDLE loop handles one message at a time — parallelism only helps the initial full-mailbox scan - Benchmark before optimising; for typical invoice volumes the current approach may be fast enough
heiko added this to the v0.5.0 milestone 2026-05-11 00:31:33 +02:00
heiko closed this issue 2026-05-11 18:45:19 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
IUS/xr-invoiced#3
No description provided.