Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imapclient: Enhancement - Add custom interval to IDLE command #603

Open
wants to merge 6 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions imapclient/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,6 +93,41 @@ 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(*restartInterval)

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
}
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
Expand Down