aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/imap/connection.go7
-rw-r--r--internal/imap/mailboxes.go41
2 files changed, 43 insertions, 5 deletions
diff --git a/internal/imap/connection.go b/internal/imap/connection.go
index 88b1496..358445b 100644
--- a/internal/imap/connection.go
+++ b/internal/imap/connection.go
@@ -99,6 +99,13 @@ func (conn *connection) ensureFolder(folder Folder) error {
return nil
}
+ if conn.mailboxes.locking(folder) {
+ // someone else tried to create the MB -- try again, now that he's done
+ return conn.ensureFolder(folder)
+ } else {
+ defer conn.mailboxes.unlocking(folder)
+ }
+
log.Printf("Checking for folder '%s'", folder)
mbox, found, err := conn.list(folder.str)
diff --git a/internal/imap/mailboxes.go b/internal/imap/mailboxes.go
index d0fdede..f1dc6c6 100644
--- a/internal/imap/mailboxes.go
+++ b/internal/imap/mailboxes.go
@@ -7,8 +7,38 @@ import (
)
type mailboxes struct {
- mb map[string]*imap.MailboxInfo
- mu sync.RWMutex
+ mb map[string]*imap.MailboxInfo
+ mu sync.RWMutex
+ changeLocks map[string]chan struct{}
+}
+
+func (mbs *mailboxes) unlocking(elem Folder) {
+ mbs.mu.Lock()
+ defer mbs.mu.Unlock()
+
+ ch, ok := mbs.changeLocks[elem.str]
+ if !ok {
+ panic("Unlocking where nothing is locked")
+ }
+ close(ch)
+ delete(mbs.changeLocks, elem.str)
+}
+
+func (mbs *mailboxes) locking(elem Folder) bool {
+ mbs.mu.Lock()
+ ch, ok := mbs.changeLocks[elem.str]
+ if !ok {
+ ch = make(chan struct{})
+ mbs.changeLocks[elem.str] = ch
+ mbs.mu.Unlock()
+ // we created the lock, we are in charge and done here
+ return false
+ } else {
+ // someone else is working, we wait till he's done
+ mbs.mu.Unlock() // we are not doing anything...
+ <-ch
+ return true
+ }
}
func (mbs *mailboxes) contains(elem Folder) bool {
@@ -28,7 +58,8 @@ func (mbs *mailboxes) add(elem *imap.MailboxInfo) {
func NewMailboxes() *mailboxes {
return &mailboxes{
- mb: map[string]*imap.MailboxInfo{},
- mu: sync.RWMutex{},
+ mb: map[string]*imap.MailboxInfo{},
+ changeLocks: map[string]chan struct{}{},
+ mu: sync.RWMutex{},
}
-} \ No newline at end of file
+}
/td>-3/+3 2012-09-07No need for a shebang in bash completion fileChristophe Fergeau1-1/+1 2012-09-06Update COPYING to reflect man page and project page.1.1.2Jason A. Donenfeld1-10/+11 2012-09-06Use BINDIR, MANDIR and SYSCONFDIR vars in MakefileChristophe Fergeau1-5/+8 2012-09-06Add dependencies to README.Jason A. Donenfeld1-0/+8 2012-09-06Change debian name.Jason A. Donenfeld5-5/+4 2012-09-06Use more normal sleep variant.1.1.1Jason A. Donenfeld1-1/+1 2012-09-06Use --noreport instead of head -n -2 for tree so that it works on mac.Jason A. Donenfeld1-1/+1 2012-09-05Be sure to explicitly state that install is a phony target.Jason A. Donenfeld1-0/+2 2012-09-05Bump debian version horribly.1.1Jason A. Donenfeld2-2/+2 2012-09-04No echo mode.Jason A. Donenfeld2-16/+42 2012-09-04Properly quote the path too.Jason A. Donenfeld1-1/+1 2012-09-04Allow passwords having spaces to go unbroken to the clipboard.Bernardo Freitas Paulo da Costa1-1/+1 2012-09-04Separate out the massive git example.Jason A. Donenfeld1-10/+14 2012-09-04Prepare for debianification.1.0Jason A. Donenfeld9-4/+60 2012-09-03Fix readme typo.Jason A. Donenfeld1-1/+1 2012-09-03Show program name properly in error message.Jason A. Donenfeld1-1/+1 2012-09-03Move examples into manpage.Jason A. Donenfeld4-93/+224 2012-09-03Make into a real project.Jason A. Donenfeld8-5/+173 2012-09-03Support pass gitJason A. Donenfeld2-1/+15 2012-08-31Add remove synonyms.Jason A. Donenfeld1-2/+2 2012-08-31Use basename in usage.Jason A. Donenfeld1-2/+1 2012-08-19now using gpg_id as a varMatthew Ramirez1-2/+2 2012-08-07Forty five seconds.Jason A. Donenfeld1-1/+1 2012-08-06Deal with klipper and new lines.Jason A. Donenfeld1-3/+19 2012-08-06Update examples.Jason A. Donenfeld1-7/+7 2012-08-06Update readme.Jason A. Donenfeld1-11/+13 2012-08-06Be slicker and more like git.Jason A. Donenfeld1-114/+173