diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2023-06-04 21:12:05 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2023-06-04 22:06:19 +0200 |
commit | 565b1a1eef18372aa3e75efa47b34aec47a53571 (patch) | |
tree | 0366be695ee1ccd21ccb9e16c71c1c1d5f34b70e /internal/imap/client.go | |
parent | 997f8076a9945a72b1d0fa77a74baba6ba58d486 (diff) | |
download | feed2imap-go-565b1a1eef18372aa3e75efa47b34aec47a53571.tar.gz feed2imap-go-565b1a1eef18372aa3e75efa47b34aec47a53571.tar.bz2 feed2imap-go-565b1a1eef18372aa3e75efa47b34aec47a53571.zip |
Improve locking around IMAP connect/disconnect.
This ensures that no connect happens _after_ a disconnect has been
issued.
Closes issue #97.
Diffstat (limited to 'internal/imap/client.go')
-rw-r--r-- | internal/imap/client.go | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/internal/imap/client.go b/internal/imap/client.go index ebcd7b3..83903fa 100644 --- a/internal/imap/client.go +++ b/internal/imap/client.go @@ -3,7 +3,7 @@ package imap import ( "fmt" "net" - "sync/atomic" + "sync" "time" uidplus "github.com/emersion/go-imap-uidplus" @@ -23,11 +23,13 @@ type connConf struct { type Client struct { connConf - mailboxes *mailboxes - commander *commander - connections [numberConns]*connection - idxCounter int32 - connChannel chan *connection + mailboxes *mailboxes + commander *commander + connections [numberConns]*connection + idxCounter int + connChannel chan *connection + connLock sync.Mutex + disconnected bool } var dialer imapClient.Dialer @@ -57,6 +59,13 @@ func (cl *Client) connect(url config.Url) (*connection, error) { return nil, err } + cl.connLock.Lock() + defer cl.connLock.Unlock() + + if cl.disconnected { + return nil, nil + } + conn := cl.createConnection(c) if !url.ForceTLS() { @@ -70,11 +79,14 @@ func (cl *Client) connect(url config.Url) (*connection, error) { } cl.connChannel <- conn + return conn, nil } func (cl *Client) Disconnect() { if cl != nil { + cl.connLock.Lock() + cl.stopCommander() close(cl.connChannel) @@ -86,13 +98,18 @@ func (cl *Client) Disconnect() { if connected { log.Print("Disconnected from ", cl.host) } + + cl.disconnected = true + + cl.connLock.Unlock() } } func (cl *Client) createConnection(c *imapClient.Client) *connection { - nextIndex := int(atomic.AddInt32(&cl.idxCounter, 1)) - 1 - if nextIndex >= len(cl.connections) { + cl.idxCounter++ + + if cl.idxCounter >= len(cl.connections) { panic("Too many connections") } @@ -104,8 +121,7 @@ func (cl *Client) createConnection(c *imapClient.Client) *connection { c: client, } - cl.connections[nextIndex] = conn - + cl.connections[cl.idxCounter] = conn return conn } |