diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2022-10-27 15:35:41 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-06 23:17:51 +0100 |
commit | 155f0c3f28a1d7bdb5c317f83a195b2fe9594b0d (patch) | |
tree | 459b42cf1fe6bbbfc54ee346d3b06c02a6cd39df /worker/maildir | |
parent | 2cbcdf3175211fdbb7c36f37ea5254c5e6ffe79d (diff) | |
download | aerc-155f0c3f28a1d7bdb5c317f83a195b2fe9594b0d.tar.gz |
maildir/sort: get MessageInfos in parallel
Sorting in the maildir worker requires reading each file in the
directory. Use waitgroups and goroutines to read files in parallel.
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/maildir')
-rw-r--r-- | worker/maildir/worker.go | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 5a4316c4..30faddac 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -10,8 +10,10 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "sort" "strings" + "sync" "github.com/emersion/go-maildir" "github.com/fsnotify/fsnotify" @@ -436,14 +438,28 @@ func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, return uids, nil } var msgInfos []*models.MessageInfo + mu := sync.Mutex{} + wg := sync.WaitGroup{} + // Hard limit at 2x CPU cores + max := runtime.NumCPU() * 2 + limit := make(chan struct{}, max) for _, uid := range uids { - info, err := w.msgInfoFromUid(uid) - if err != nil { - logging.Errorf("could not get message info: %v", err) - continue - } - msgInfos = append(msgInfos, info) + limit <- struct{}{} + wg.Add(1) + go func(uid uint32) { + defer wg.Done() + info, err := w.msgInfoFromUid(uid) + if err != nil { + logging.Errorf("could not get message info: %v", err) + return + } + mu.Lock() + msgInfos = append(msgInfos, info) + mu.Unlock() + <-limit + }(uid) } + wg.Wait() sortedUids, err := lib.Sort(msgInfos, criteria) if err != nil { logging.Errorf("could not sort the messages: %v", err) |