From 4f112c16dfac61deb128b48e9d516f78ab55fc95 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sun, 28 Feb 2021 13:22:17 +0100 Subject: Start IMAP connections in the background and use them on the go --- internal/imap/client.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'internal/imap/client.go') diff --git a/internal/imap/client.go b/internal/imap/client.go index 783acf6..c916a64 100644 --- a/internal/imap/client.go +++ b/internal/imap/client.go @@ -1,6 +1,8 @@ package imap import ( + "sync/atomic" + uidplus "github.com/emersion/go-imap-uidplus" imapClient "github.com/emersion/go-imap/client" @@ -17,15 +19,17 @@ type connConf struct { type Client struct { connConf - mailboxes *mailboxes - commander *commander - connections [numberConns]*connection - nextFreeIndex int + mailboxes *mailboxes + commander *commander + connections [numberConns]*connection + idxCounter int32 + connChannel chan *connection } func (cl *Client) Disconnect() { if cl != nil { cl.stopCommander() + close(cl.connChannel) connected := false for _, conn := range cl.connections { @@ -39,7 +43,9 @@ func (cl *Client) Disconnect() { } func (cl *Client) createConnection(c *imapClient.Client) *connection { - if cl.nextFreeIndex >= len(cl.connections) { + nextIndex := int(atomic.AddInt32(&cl.idxCounter, 1)) - 1 + + if nextIndex >= len(cl.connections) { panic("Too many connections") } @@ -51,12 +57,14 @@ func (cl *Client) createConnection(c *imapClient.Client) *connection { c: client, } - cl.connections[cl.nextFreeIndex] = conn - cl.nextFreeIndex++ + cl.connections[nextIndex] = conn return conn } func NewClient() *Client { - return &Client{mailboxes: NewMailboxes()} + return &Client{ + mailboxes: NewMailboxes(), + connChannel: make(chan *connection, 0), + } } -- cgit v1.2.3-70-g09d2