aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/idler.go
blob: 369ebd2e463f00a6f09465960c019a21aebf8e11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package imap

import (
	"fmt"
	"time"

	"git.sr.ht/~rjarry/aerc/log"
	"git.sr.ht/~rjarry/aerc/worker/types"
	"github.com/emersion/go-imap"
)

var errIdleTimeout = fmt.Errorf("idle timeout")

// idler manages the idle mode of the imap server. Enter idle mode if there's
// no other task and leave idle mode when a new task arrives. Idle mode is only
// used when the client is ready and connected. After a connection loss, make
// sure that idling returns gracefully and the worker remains responsive.
type idler struct {
	client    *imapClient
	debouncer *time.Timer
	debounce  time.Duration
	timeout   time.Duration
	worker    types.WorkerInteractor
	stop      chan struct{}
	start     chan struct{}
	done      chan error
}

func newIdler(cfg imapConfig, w types.WorkerInteractor, startIdler chan struct{}) *idler {
	return &idler{
		debouncer: nil,
		debounce:  cfg.idle_debounce,
		timeout:   cfg.idle_timeout,
		worker:    w,
		stop:      make(chan struct{}),
		start:     startIdler,
		done:      make(chan error),
	}
}

func (i *idler) SetClient(c *imapClient) {
	i.client = c
}

func (i *idler) ready() bool {
	return (i.client != nil && i.client.State() == imap.SelectedState)
}

func (i *idler) Start() {
	if !i.ready() {
		return
	}

	select {
	case <-i.stop:
		// stop channel is nil (probably after a debounce), we don't
		// want to close it
	default:
		close(i.stop)
	}

	// create new stop channel
	i.stop = make(chan struct{})

	// clear done channel
	clearing := true
	for clearing {
		select {
		case <-i.done:
			continue
		default:
			clearing = false
		}
	}

	i.worker.Tracef("idler (start): start idle after debounce")
	i.debouncer = time.AfterFunc(i.debounce, func() {
		i.start <- struct{}{}
		i.worker.Tracef("idler (start): started")
	})
}

func (i *idler) Execute() {
	if !i.ready() {
		return
	}

	// we need to call client.Idle in a goroutine since it is blocking call
	// and we still want to receive messages
	go func() {
		defer log.PanicHandler()

		start := time.Now()
		err := i.client.Idle(i.stop, nil)
		if err != nil {
			i.worker.Errorf("idle returned error: %v", err)
		}
		i.worker.Tracef("idler (execute): idleing for %s", time.Since(start))

		i.done <- err
	}()
}

func (i *idler) Stop() error {
	if !i.ready() {
		return nil
	}

	select {
	case <-i.stop:
		i.worker.Debugf("idler (stop): idler already stopped?")
		return nil
	default:
		close(i.stop)
	}

	if i.debouncer != nil {
		if i.debouncer.Stop() {
			i.worker.Tracef("idler (stop): debounced")
			return nil
		}
	}

	select {
	case err := <-i.done:
		i.worker.Tracef("idler (stop): idle stopped: %v", err)
		return err
	case <-time.After(i.timeout):
		i.worker.Errorf("idler (stop): cannot stop idle (timeout)")
		return errIdleTimeout
	}
}