mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
145 lines
3.3 KiB
Go
145 lines
3.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"runtime/debug"
|
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
"github.com/hanzoai/mail/message"
|
|
"github.com/hanzoai/mail/metrics"
|
|
"github.com/hanzoai/mail/mlog"
|
|
)
|
|
|
|
// We process messages in database transactions in batches. Otherwise, for accounts
|
|
// with many messages, we would get slowdown with many unwritten blocks in memory.
|
|
var reparseMessageBatchSize = 1000
|
|
|
|
// ReparseMessages reparses all messages, updating the MIME structure in
|
|
// Message.ParsedBuf.
|
|
//
|
|
// Typically called during automatic account upgrade, or manually.
|
|
//
|
|
// Returns total number of messages, all of which were reparsed.
|
|
func (a *Account) ReparseMessages(ctx context.Context, log mlog.Log) (int, error) {
|
|
type Result struct {
|
|
Message *Message
|
|
Buf []byte
|
|
Err error
|
|
}
|
|
|
|
// We'll have multiple goroutines that pick up messages to parse. The assumption is
|
|
// that reads of messages from disk are the bottleneck.
|
|
nprog := 10
|
|
work := make(chan *Message, nprog)
|
|
results := make(chan Result, nprog)
|
|
|
|
processMessage := func(m *Message) {
|
|
r := Result{Message: m}
|
|
|
|
defer func() {
|
|
x := recover()
|
|
if x != nil {
|
|
r.Err = fmt.Errorf("unhandled panic parsing message: %v", x)
|
|
log.Error("processMessage panic", slog.Any("err", x))
|
|
debug.PrintStack()
|
|
metrics.PanicInc(metrics.Store)
|
|
}
|
|
|
|
results <- r
|
|
}()
|
|
|
|
mr := a.MessageReader(*m)
|
|
p, err := message.EnsurePart(log.Logger, false, mr, m.Size)
|
|
if err != nil {
|
|
// note: p is still set to a usable part
|
|
log.Debugx("reparsing message", err, slog.Int64("msgid", m.ID))
|
|
}
|
|
r.Buf, r.Err = json.Marshal(p)
|
|
}
|
|
|
|
// Start goroutines that parse messages.
|
|
for range nprog {
|
|
go func() {
|
|
for {
|
|
m, ok := <-work
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
processMessage(m)
|
|
}
|
|
}()
|
|
}
|
|
defer close(work) // Stop goroutines when done.
|
|
|
|
total := 0
|
|
var lastID int64 // Each db transaction starts after lastID.
|
|
for {
|
|
var n int
|
|
err := a.DB.Write(ctx, func(tx *bstore.Tx) error {
|
|
var busy int
|
|
|
|
q := bstore.QueryTx[Message](tx)
|
|
q.FilterEqual("Expunged", false)
|
|
q.FilterGreater("ID", lastID)
|
|
q.Limit(reparseMessageBatchSize)
|
|
q.SortAsc("ID")
|
|
err := q.ForEach(func(m Message) error {
|
|
lastID = m.ID
|
|
n++
|
|
|
|
for {
|
|
select {
|
|
case work <- &m:
|
|
busy++
|
|
return nil
|
|
|
|
case r := <-results:
|
|
busy--
|
|
if r.Err != nil {
|
|
log.Errorx("marshal parsed form of message", r.Err, slog.Int64("msgid", r.Message.ID))
|
|
} else {
|
|
r.Message.ParsedBuf = r.Buf
|
|
if err := tx.Update(r.Message); err != nil {
|
|
return fmt.Errorf("update message: %w", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("reparsing messages: %w", err)
|
|
}
|
|
|
|
// Drain remaining reparses.
|
|
for ; busy > 0; busy-- {
|
|
r := <-results
|
|
if r.Err != nil {
|
|
log.Errorx("marshal parsed form of message", r.Err, slog.Int64("msgid", r.Message.ID))
|
|
} else {
|
|
r.Message.ParsedBuf = r.Buf
|
|
if err := tx.Update(r.Message); err != nil {
|
|
return fmt.Errorf("update message with id %d: %w", r.Message.ID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
total += n
|
|
if err != nil {
|
|
return total, fmt.Errorf("update messages with parsed mime structure: %w", err)
|
|
}
|
|
log.Debug("reparse message progress", slog.Int("total", total))
|
|
if n < reparseMessageBatchSize {
|
|
break
|
|
}
|
|
}
|
|
|
|
return total, nil
|
|
}
|