imap: add backend mutex

This commit is contained in:
Simon Ser 2020-02-29 12:13:32 +01:00
parent 5cd1c7a921
commit 4346b0163f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 12 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package imap
import ( import (
"errors" "errors"
"sync"
"github.com/emersion/go-imap" "github.com/emersion/go-imap"
imapbackend "github.com/emersion/go-imap/backend" imapbackend "github.com/emersion/go-imap/backend"
@ -16,7 +17,10 @@ type backend struct {
sessions *auth.Manager sessions *auth.Manager
eventsManager *events.Manager eventsManager *events.Manager
updates chan imapbackend.Update 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) { func (be *backend) Login(info *imap.ConnInfo, username, password string) (imapbackend.User, error) {

View File

@ -60,6 +60,11 @@ type user struct {
} }
func getUser(be *backend, username string, c *protonmail.Client, privateKeys openpgp.EntityList) (*user, error) { 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 { if u, ok := be.users[username]; ok {
u.Lock() u.Lock()
u.numClients++ u.numClients++
@ -268,6 +273,8 @@ func (u *user) RenameMailbox(existingName, newName string) error {
} }
func (u *user) Logout() error { func (u *user) Logout() error {
u.backend.Lock()
defer u.backend.Unlock()
u.Lock() u.Lock()
defer u.Unlock() defer u.Unlock()