perf(mail): fetch only headers for list/search (no body download)
list and search now fetch BODY.PEEK[HEADER] + BODYSTRUCTURE instead of the whole RFC822 message, so listing a large mailbox no longer downloads every message body and attachment. Header parsing reuses the same go-message path (RFC2047 decoding/formatting preserved); has_attachments is derived from the BODYSTRUCTURE tree. FetchFull keeps fetching the full message for get. Validated end-to-end against a live IMAP account: list/search/get output identical to the prior full-fetch behaviour, has_attachments correct. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+80
-37
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
@@ -66,18 +67,43 @@ func (c *Client) SelectFolder(folder string) (uint32, uint32, error) {
|
||||
return mbox.UidValidity, maxUID, nil
|
||||
}
|
||||
|
||||
func (c *Client) fetchByUIDSet(folder string, set *imap.SeqSet, full bool) ([]Message, error) {
|
||||
// hasAttachment reports whether a BODYSTRUCTURE tree contains a part marked
|
||||
// Content-Disposition: attachment — the same notion of "attachment" that
|
||||
// ParseMessage uses (go-message routes attachment-disposition parts to
|
||||
// AttachmentHeader). This lets list/search report has_attachments without
|
||||
// downloading any body.
|
||||
func hasAttachment(bs *imap.BodyStructure) bool {
|
||||
if bs == nil {
|
||||
return false
|
||||
}
|
||||
if strings.EqualFold(bs.Disposition, "attachment") {
|
||||
return true
|
||||
}
|
||||
for _, p := range bs.Parts {
|
||||
if hasAttachment(p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// fetchHeadersByUIDSet fetches only message headers (BODY.PEEK[HEADER]) plus the
|
||||
// BODYSTRUCTURE, never the body, so listing is cheap on large mailboxes.
|
||||
func (c *Client) fetchHeadersByUIDSet(folder string, set *imap.SeqSet) ([]Header, error) {
|
||||
if _, err := c.c.Select(folder, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
section := &imap.BodySectionName{Peek: true} // entire message, no \Seen side-effect
|
||||
items := []imap.FetchItem{imap.FetchUid, section.FetchItem()}
|
||||
section := &imap.BodySectionName{
|
||||
BodyPartName: imap.BodyPartName{Specifier: imap.HeaderSpecifier},
|
||||
Peek: true, // BODY.PEEK[HEADER] — headers only, no \Seen side-effect
|
||||
}
|
||||
items := []imap.FetchItem{imap.FetchUid, imap.FetchBodyStructure, section.FetchItem()}
|
||||
|
||||
msgCh := make(chan *imap.Message, 16)
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- c.c.UidFetch(set, items, msgCh) }()
|
||||
|
||||
var out []Message
|
||||
var out []Header
|
||||
for m := range msgCh {
|
||||
r := m.GetBody(section)
|
||||
if r == nil {
|
||||
@@ -85,25 +111,64 @@ func (c *Client) fetchByUIDSet(folder string, set *imap.SeqSet, full bool) ([]Me
|
||||
}
|
||||
raw, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("uid %d: read body: %w", m.Uid, err)
|
||||
return nil, fmt.Errorf("uid %d: read header: %w", m.Uid, err)
|
||||
}
|
||||
parsed, err := ParseMessage(m.Uid, raw)
|
||||
h, err := ParseHeaderBytes(m.Uid, raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !full {
|
||||
parsed.Attachments = nil
|
||||
parsed.BodyText = ""
|
||||
}
|
||||
out = append(out, parsed)
|
||||
h.HasAttachments = hasAttachment(m.BodyStructure)
|
||||
out = append(out, h)
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Header.UID > out[j].Header.UID })
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].UID > out[j].UID })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// fetchFullByUID fetches one message in full (body + attachments).
|
||||
func (c *Client) fetchFullByUID(folder string, uid uint32) (Message, error) {
|
||||
if _, err := c.c.Select(folder, true); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
section := &imap.BodySectionName{Peek: true} // entire message, no \Seen side-effect
|
||||
items := []imap.FetchItem{imap.FetchUid, section.FetchItem()}
|
||||
|
||||
set := new(imap.SeqSet)
|
||||
set.AddNum(uid)
|
||||
msgCh := make(chan *imap.Message, 1)
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- c.c.UidFetch(set, items, msgCh) }()
|
||||
|
||||
var (
|
||||
msg Message
|
||||
found bool
|
||||
)
|
||||
for m := range msgCh {
|
||||
r := m.GetBody(section)
|
||||
if r == nil {
|
||||
continue
|
||||
}
|
||||
raw, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("uid %d: read body: %w", m.Uid, err)
|
||||
}
|
||||
parsed, err := ParseMessage(m.Uid, raw)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
msg, found = parsed, true
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if !found {
|
||||
return Message{}, fmt.Errorf("uid %d not found in %s", uid, folder)
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (c *Client) FetchHeaders(folder string, uids []uint32) ([]Header, error) {
|
||||
set := new(imap.SeqSet)
|
||||
if len(uids) == 0 {
|
||||
@@ -113,11 +178,7 @@ func (c *Client) FetchHeaders(folder string, uids []uint32) ([]Header, error) {
|
||||
set.AddNum(u)
|
||||
}
|
||||
}
|
||||
msgs, err := c.fetchByUIDSet(folder, set, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return headersOf(msgs), nil
|
||||
return c.fetchHeadersByUIDSet(folder, set)
|
||||
}
|
||||
|
||||
func (c *Client) FetchHeadersRange(folder string, sinceUID, beforeUID uint32, limit int) ([]Header, error) {
|
||||
@@ -131,11 +192,10 @@ func (c *Client) FetchHeadersRange(folder string, sinceUID, beforeUID uint32, li
|
||||
hi = beforeUID - 1
|
||||
}
|
||||
set.AddRange(lo, hi)
|
||||
msgs, err := c.fetchByUIDSet(folder, set, false)
|
||||
h, err := c.fetchHeadersByUIDSet(folder, set)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h := headersOf(msgs)
|
||||
if limit > 0 && len(h) > limit {
|
||||
h = h[:limit]
|
||||
}
|
||||
@@ -143,16 +203,7 @@ func (c *Client) FetchHeadersRange(folder string, sinceUID, beforeUID uint32, li
|
||||
}
|
||||
|
||||
func (c *Client) FetchFull(folder string, uid uint32) (Message, error) {
|
||||
set := new(imap.SeqSet)
|
||||
set.AddNum(uid)
|
||||
msgs, err := c.fetchByUIDSet(folder, set, true)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if len(msgs) == 0 {
|
||||
return Message{}, fmt.Errorf("uid %d not found in %s", uid, folder)
|
||||
}
|
||||
return msgs[0], nil
|
||||
return c.fetchFullByUID(folder, uid)
|
||||
}
|
||||
|
||||
type SearchCriteria struct {
|
||||
@@ -195,11 +246,3 @@ func (c *Client) Search(folder string, sc SearchCriteria, limit int) ([]Header,
|
||||
}
|
||||
return c.FetchHeaders(folder, uids)
|
||||
}
|
||||
|
||||
func headersOf(msgs []Message) []Header {
|
||||
out := make([]Header, 0, len(msgs))
|
||||
for _, m := range msgs {
|
||||
out = append(out, m.Header)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user