imap: add backend mutex
This commit is contained in:
parent
5cd1c7a921
commit
4346b0163f
|
@ -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) {
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue