worker/imap: use the IMAP connection from a single goroutine
Unfortunately, the IMAP protocol hasn't been designed to be used from multiple goroutines at the same time. For instance, if you fetch twice the same message from two different goroutines, it's not possible to tell whether the response is for one receiver or the other. For this reason, go-imap clients aren't safe to use from multiple goroutines. This commit changes the IMAP workers to be synchronous again (a command is executed only after the previous one has completed). To use IMAP from different threads, popular clients (e.g. Thunderbird) typically open multiple connections.
This commit is contained in:
parent
072bc26872
commit
089740758c
|
@ -46,12 +46,8 @@ func (imapw *IMAPWorker) handleFetchMessages(
|
|||
msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem,
|
||||
section *imap.BodySectionName) {
|
||||
|
||||
go func() {
|
||||
messages := make(chan *imap.Message)
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- imapw.client.UidFetch(uids, items, messages)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for _msg := range messages {
|
||||
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
|
||||
|
@ -81,7 +77,9 @@ func (imapw *IMAPWorker) handleFetchMessages(
|
|||
}, nil)
|
||||
}
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
}()
|
||||
|
||||
if err := imapw.client.UidFetch(uids, items, messages); err != nil {
|
||||
imapw.worker.PostMessage(&types.Error{
|
||||
Message: types.RespondTo(msg),
|
||||
Error: err,
|
||||
|
@ -90,6 +88,4 @@ func (imapw *IMAPWorker) handleFetchMessages(
|
|||
imapw.worker.PostMessage(
|
||||
&types.Done{types.RespondTo(msg)}, nil)
|
||||
}
|
||||
}()
|
||||
}()
|
||||
}
|
||||
|
|
|
@ -8,11 +8,8 @@ import (
|
|||
|
||||
func (imapw *IMAPWorker) handleListDirectories(msg *types.ListDirectories) {
|
||||
mailboxes := make(chan *imap.MailboxInfo)
|
||||
done := make(chan error, 1)
|
||||
imapw.worker.Logger.Println("Listing mailboxes")
|
||||
go func() {
|
||||
done <- imapw.client.List("", "*", mailboxes)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for mbox := range mailboxes {
|
||||
imapw.worker.PostMessage(&types.Directory{
|
||||
|
@ -21,7 +18,9 @@ func (imapw *IMAPWorker) handleListDirectories(msg *types.ListDirectories) {
|
|||
Attributes: mbox.Attributes,
|
||||
}, nil)
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
}()
|
||||
|
||||
if err := imapw.client.List("", "*", mailboxes); err != nil {
|
||||
imapw.worker.PostMessage(&types.Error{
|
||||
Message: types.RespondTo(msg),
|
||||
Error: err,
|
||||
|
@ -30,5 +29,4 @@ func (imapw *IMAPWorker) handleListDirectories(msg *types.ListDirectories) {
|
|||
imapw.worker.PostMessage(
|
||||
&types.Done{types.RespondTo(msg)}, nil)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) {
|
||||
imapw.worker.Logger.Printf("Opening %s", msg.Directory)
|
||||
go func() {
|
||||
|
||||
_, err := imapw.client.Select(msg.Directory, false)
|
||||
if err != nil {
|
||||
imapw.worker.PostMessage(&types.Error{
|
||||
|
@ -18,7 +18,6 @@ func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) {
|
|||
} else {
|
||||
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (imapw *IMAPWorker) handleFetchDirectoryContents(
|
||||
|
@ -26,7 +25,6 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents(
|
|||
|
||||
imapw.worker.Logger.Printf("Fetching UID list")
|
||||
|
||||
go func() {
|
||||
seqSet := &imap.SeqSet{}
|
||||
seqSet.AddRange(1, imapw.selected.Messages)
|
||||
uids, err := imapw.client.UidSearch(&imap.SearchCriteria{
|
||||
|
@ -46,5 +44,4 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents(
|
|||
}, nil)
|
||||
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue