imap: start reconnect when initial connect fails

Start the reconnect cycle when the initial connect fails. Make the
connection observer send a connection error when the imap client is nil.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
This commit is contained in:
Koni Marti 2022-02-20 00:09:30 +01:00 committed by Robin Jarry
parent 8ed9ae7f0a
commit 48e01bd51f
1 changed files with 11 additions and 3 deletions

View File

@ -191,6 +191,7 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
w.autoReconnect = true
c, err := w.connect()
if err != nil {
checkConn(0)
reterr = err
break
}
@ -335,13 +336,20 @@ func (w *IMAPWorker) exponentialBackoff() (time.Duration, string) {
}
func (w *IMAPWorker) startConnectionObserver() {
emitConnErr := func(errMsg string) {
w.worker.PostMessage(&types.ConnError{
Error: fmt.Errorf(errMsg),
}, nil)
}
if w.client == nil {
emitConnErr("imap client not connected")
return
}
go func() {
select {
case <-w.client.LoggedOut():
if w.autoReconnect {
w.worker.PostMessage(&types.ConnError{
Error: fmt.Errorf("imap: logged out"),
}, nil)
emitConnErr("imap: logged out")
}
case <-w.done:
return