aboutsummaryrefslogtreecommitdiff
path: root/internal/imap
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-22 23:40:53 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-22 23:40:53 +0200
commit9280ecb7e0b0039d6c1f4800373eb76452145078 (patch)
treee3dd11e9953f949709505731a16e8246afe536aa /internal/imap
parent15b6c155a8476cf86e8bd745e239e55e77317909 (diff)
downloadfeed2imap-go-9280ecb7e0b0039d6c1f4800373eb76452145078.tar.gz
feed2imap-go-9280ecb7e0b0039d6c1f4800373eb76452145078.tar.bz2
feed2imap-go-9280ecb7e0b0039d6c1f4800373eb76452145078.zip
Concurrent feed processing; central imap handler
Diffstat (limited to 'internal/imap')
-rw-r--r--internal/imap/client.go29
-rw-r--r--internal/imap/commando.go92
-rw-r--r--internal/imap/imap.go4
3 files changed, 121 insertions, 4 deletions
diff --git a/internal/imap/client.go b/internal/imap/client.go
index 3f004e2..da06f54 100644
--- a/internal/imap/client.go
+++ b/internal/imap/client.go
@@ -17,6 +17,7 @@ type Client struct {
mailboxes mailboxes
delimiter string
toplevel Folder
+ commander *commander
}
type Folder struct {
@@ -50,6 +51,8 @@ func (mbs mailboxes) add(elem *imap.MailboxInfo) {
func (client *Client) Disconnect() {
if client != nil {
+ client.stopCommander()
+
connected := (client.c.State() & imap.ConnectedState) != 0
_ = client.c.Logout()
@@ -119,7 +122,7 @@ func (client *Client) fetchDelimiter() error {
return nil
}
-func (client *Client) EnsureFolder(folder Folder) error {
+func (client *Client) ensureFolder(folder Folder) error {
if client.mailboxes.contains(folder) {
return nil
}
@@ -146,6 +149,26 @@ func (client *Client) EnsureFolder(folder Folder) error {
}
}
-func (client *Client) PutMessage(folder Folder, message string, date time.Time) error {
- return client.c.Append(folder.String(), nil, date, strings.NewReader(message))
+func (client *Client) EnsureFolder(folder Folder, errorHandler ErrorHandler) {
+ client.commander.execute(ensureCommando{folder}, errorHandler)
+}
+
+func (client *Client) putMessages(folder Folder, messages []string) error {
+ if len(messages) == 0 {
+ return nil
+ }
+
+ now := time.Now()
+ for _, msg := range messages {
+ reader := strings.NewReader(msg)
+ if err := client.c.Append(folder.str, nil, now, reader); err != nil {
+ return fmt.Errorf("uploading message to %s: %w", folder, err)
+ }
+ }
+
+ return nil
+}
+
+func (client *Client) PutMessages(folder Folder, messages []string, errorHandler ErrorHandler) {
+ client.commander.execute(addCommando{folder, messages}, errorHandler)
}
diff --git a/internal/imap/commando.go b/internal/imap/commando.go
new file mode 100644
index 0000000..fbfeaec
--- /dev/null
+++ b/internal/imap/commando.go
@@ -0,0 +1,92 @@
+package imap
+
+import "github.com/Necoro/feed2imap-go/internal/log"
+
+const maxPipeDepth = 10
+
+type commander struct {
+ client *Client
+ pipe chan<- execution
+ done chan<- struct{}
+}
+
+type command interface {
+ execute(client *Client) error
+}
+
+type ErrorHandler func(error) string
+
+type execution struct {
+ cmd command
+ done chan<- struct{}
+ errorHandler ErrorHandler
+}
+
+type addCommando struct {
+ folder Folder
+ messages []string
+}
+
+func (cmd addCommando) execute(client *Client) error {
+ return client.putMessages(cmd.folder, cmd.messages)
+}
+
+type ensureCommando struct {
+ folder Folder
+}
+
+func (cmd ensureCommando) execute(client *Client) error {
+ return client.ensureFolder(cmd.folder)
+}
+
+func (commander *commander) execute(command command, handler ErrorHandler) {
+ done := make(chan struct{})
+ commander.pipe <- execution{command, done, handler}
+ <-done
+}
+
+func executioner(client *Client, pipe <-chan execution, done <-chan struct{}) {
+ for {
+ select {
+ case <-done:
+ return
+ case execution := <-pipe:
+ select { // break as soon as done is there
+ case <-done:
+ return
+ default:
+ }
+ if err := execution.cmd.execute(client); err != nil {
+ if execution.errorHandler == nil {
+ log.Error(err)
+ } else {
+ log.Error(execution.errorHandler(err))
+ }
+ }
+ close(execution.done)
+ }
+ }
+}
+
+func (client *Client) startCommander() {
+ if client.commander != nil {
+ return
+ }
+
+ pipe := make(chan execution, maxPipeDepth)
+ done := make(chan struct{})
+
+ client.commander = &commander{client, pipe, done}
+
+ go executioner(client, pipe, done)
+}
+
+func (client *Client) stopCommander() {
+ if client.commander == nil {
+ return
+ }
+
+ close(client.commander.done)
+
+ client.commander = nil
+}
diff --git a/internal/imap/imap.go b/internal/imap/imap.go
index f284f81..4965ca5 100644
--- a/internal/imap/imap.go
+++ b/internal/imap/imap.go
@@ -121,9 +121,11 @@ func Connect(url *url.URL) (*Client, error) {
log.Printf("Determined '%s' as toplevel, with '%s' as delimiter", client.toplevel, client.delimiter)
- if err = client.EnsureFolder(client.toplevel); err != nil {
+ if err = client.ensureFolder(client.toplevel); err != nil {
return nil, err
}
+ client.startCommander()
+
return &client, nil
}