From 4346b0163fce59412f72723200a41e2f6cad8bdd Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sat, 29 Feb 2020 12:13:32 +0100 Subject: [PATCH] imap: add backend mutex --- imap/backend.go | 6 +++++- imap/user.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/imap/backend.go b/imap/backend.go index 9b952d4..e44769b 100644 --- a/imap/backend.go +++ b/imap/backend.go @@ -2,6 +2,7 @@ package imap import ( "errors" + "sync" "github.com/emersion/go-imap" imapbackend "github.com/emersion/go-imap/backend" @@ -16,7 +17,10 @@ type backend struct { sessions *auth.Manager eventsManager *events.Manager updates chan imapbackend.Update - users map[string]*user + + sync.Mutex // protects everything below + + users map[string]*user } func (be *backend) Login(info *imap.ConnInfo, username, password string) (imapbackend.User, error) { diff --git a/imap/user.go b/imap/user.go index 6f99742..5f6dd9f 100644 --- a/imap/user.go +++ b/imap/user.go @@ -60,6 +60,11 @@ type user struct { } func getUser(be *backend, username string, c *protonmail.Client, privateKeys openpgp.EntityList) (*user, error) { + // TODO: logging a user in may take some time, find a way not to lock all + // other logins during this time + be.Lock() + defer be.Unlock() + if u, ok := be.users[username]; ok { u.Lock() u.numClients++ @@ -268,6 +273,8 @@ func (u *user) RenameMailbox(existingName, newName string) error { } func (u *user) Logout() error { + u.backend.Lock() + defer u.backend.Unlock() u.Lock() defer u.Unlock()