From 1980744f7bf9e147abf649d37a2fa7dddd4e7254 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Sat, 20 Jan 2024 11:31:03 +0100 Subject: idler: improve the imap idler Rewrite the imap idler to make it more fault tolerant and prevent hangs (and possibly short writes). Fixes: https://todo.sr.ht/~rjarry/aerc/208 Signed-off-by: Koni Marti Tested-by: Karel Balej Acked-by: Robin Jarry --- worker/middleware/gmailworker.go | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'worker/middleware/gmailworker.go') diff --git a/worker/middleware/gmailworker.go b/worker/middleware/gmailworker.go index 807f7bff..f9924732 100644 --- a/worker/middleware/gmailworker.go +++ b/worker/middleware/gmailworker.go @@ -8,20 +8,14 @@ import ( "github.com/emersion/go-imap/client" ) -type idler interface { - Start() - Stop() error -} - type gmailWorker struct { types.WorkerInteractor mu sync.Mutex client *client.Client - idler idler } // NewGmailWorker returns an IMAP middleware for the X-GM-EXT-1 extension -func NewGmailWorker(base types.WorkerInteractor, c *client.Client, i idler, +func NewGmailWorker(base types.WorkerInteractor, c *client.Client, ) types.WorkerInteractor { base.Infof("loading worker middleware: X-GM-EXT-1") @@ -29,37 +23,30 @@ func NewGmailWorker(base types.WorkerInteractor, c *client.Client, i idler, for iter := base; iter != nil; iter = iter.Unwrap() { if g, ok := iter.(*gmailWorker); ok { base.Infof("already loaded; resetting") - err := g.reset(c, i) + err := g.reset(c) if err != nil { base.Errorf("reset failed: %v", err) } return base } } - return &gmailWorker{WorkerInteractor: base, client: c, idler: i} + return &gmailWorker{WorkerInteractor: base, client: c} } func (g *gmailWorker) Unwrap() types.WorkerInteractor { return g.WorkerInteractor } -func (g *gmailWorker) reset(c *client.Client, i idler) error { +func (g *gmailWorker) reset(c *client.Client) error { g.mu.Lock() defer g.mu.Unlock() g.client = c - g.idler = i return nil } func (g *gmailWorker) ProcessAction(msg types.WorkerMessage) types.WorkerMessage { - switch msg := msg.(type) { - case *types.FetchMessageHeaders: + if msg, ok := msg.(*types.FetchMessageHeaders); ok && len(msg.Uids) > 0 { g.mu.Lock() - err := g.idler.Stop() - if err != nil { - g.Errorf("idler reported an error: %v", err) - break - } handler := xgmext.NewHandler(g.client) uids, err := handler.FetchEntireThreads(msg.Uids) @@ -71,7 +58,6 @@ func (g *gmailWorker) ProcessAction(msg types.WorkerMessage) types.WorkerMessage msg.Uids = uids } - g.idler.Start() g.mu.Unlock() } return g.WorkerInteractor.ProcessAction(msg) -- cgit