From 47f64af5b96a9a77b6dc42cbea04be86b97a3261 Mon Sep 17 00:00:00 2001 From: Owen Kosman Date: Thu, 28 Mar 2024 20:16:24 +0000 Subject: [PATCH 1/3] imapclient: Enhancement - Add custom interval to IDLE command --- imapclient/idle.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/imapclient/idle.go b/imapclient/idle.go index e3904de9..19b4e560 100644 --- a/imapclient/idle.go +++ b/imapclient/idle.go @@ -32,6 +32,20 @@ func (c *Client) Idle() (*IdleCommand, error) { return cmd, nil } +func (c *Client) IdleWithInterval(restartInterval *time.Duration) (*IdleCommand, error) { + child, err := c.idle() + if err != nil { + return nil, err + } + + cmd := &IdleCommand{ + stop: make(chan struct{}), + done: make(chan struct{}), + } + go cmd.runWithInterval(c, child, restartInterval) + return cmd, nil +} + // IdleCommand is an IDLE command. // // Initially, the IDLE command is running. The server may send unilateral @@ -79,6 +93,38 @@ func (cmd *IdleCommand) run(c *Client, child *idleCommand) { } } +func (cmd *IdleCommand) runWithInterval(c *Client, child *idleCommand, restartInterval *time.Duration) { + defer close(cmd.done) + + timer := time.NewTimer(*restartInterval) + defer timer.Stop() + + defer func() { + if child != nil { + if err := child.Close(); err != nil && cmd.err == nil { + cmd.err = err + } + } + }() + + for { + select { + case <-timer.C: + timer.Reset(idleRestartInterval) + + if cmd.err = child.Close(); cmd.err != nil { + return + } + if child, cmd.err = c.idle(); cmd.err != nil { + return + } + case <-cmd.stop: + cmd.lastChild = child + return + } + } +} + // Close stops the IDLE command. // // This method blocks until the command to stop IDLE is written, but doesn't From 22b885a4b9adb2b93781c695102b2c5dd8e083c3 Mon Sep 17 00:00:00 2001 From: Owen Kosman Date: Fri, 29 Mar 2024 11:17:45 +0000 Subject: [PATCH 2/3] added wait command --- imapclient/idle.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imapclient/idle.go b/imapclient/idle.go index 19b4e560..2dd5b7dd 100644 --- a/imapclient/idle.go +++ b/imapclient/idle.go @@ -115,6 +115,9 @@ func (cmd *IdleCommand) runWithInterval(c *Client, child *idleCommand, restartIn if cmd.err = child.Close(); cmd.err != nil { return } + if cmd.err = child.Wait(); cmd.err != nil { + return + } if child, cmd.err = c.idle(); cmd.err != nil { return } From 3019e377a0dc7b53b2fb4a7d9c310fe5b4b91e52 Mon Sep 17 00:00:00 2001 From: Owen Kosman Date: Fri, 29 Mar 2024 12:24:56 +0000 Subject: [PATCH 3/3] fixed bug with lingering old interval --- imapclient/idle.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imapclient/idle.go b/imapclient/idle.go index 2dd5b7dd..79d38de6 100644 --- a/imapclient/idle.go +++ b/imapclient/idle.go @@ -110,7 +110,7 @@ func (cmd *IdleCommand) runWithInterval(c *Client, child *idleCommand, restartIn for { select { case <-timer.C: - timer.Reset(idleRestartInterval) + timer.Reset(*restartInterval) if cmd.err = child.Close(); cmd.err != nil { return