aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/idler.go
blob: aa61776d8ab42940be1e6da39e694a62b48e6490 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package imap

import (
	"fmt"
	"sync"
	"time"

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

var (
	errIdleTimeout   = fmt.Errorf("idle timeout")
	errIdleModeHangs = fmt.Errorf("idle mode hangs; waiting to reconnect")
)

// 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 {
	sync.Mutex
	config  imapConfig
	client  *imapClient
	worker  types.WorkerInteractor
	stop    chan struct{}
	done    chan error
	waiting bool
	idleing bool
}

func newIdler(cfg imapConfig, w types.WorkerInteractor) *idler {
	return &idler{config: cfg, worker: w, done: make(chan error)}
}

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

func (i *idler) setWaiting(wait bool) {
	i.Lock()
	i.waiting = wait
	i.Unlock()
}

func (i *idler) isWaiting() bool {
	i.Lock()
	defer i.Unlock()
	return i.waiting
}

func (i *idler) isReady() bool {
	i.Lock()
	defer i.Unlock()
	return (!i.waiting && i.client != nil &&
		i.client.State() == imap.SelectedState)
}

func (i *idler) setIdleing(v bool) {
	i.Lock()
	defer i.Unlock()
	i.idleing = v
}

func (i *idler) isIdleing() bool {
	i.Lock()
	defer i.Unlock()
	return i.idleing
}

func (i *idler) Start() {
	switch {
	case i.isReady():
		i.stop = make(chan struct{})

		go func() {
			defer log.PanicHandler()
			select {
			case <-i.stop:
				// debounce idle
				i.done <- nil
			case <-time.After(i.config.idle_debounce):
				// enter idle mode
				i.setIdleing(true)
				now := time.Now()
				err := i.client.Idle(i.stop,
					&client.IdleOptions{
						LogoutTimeout: 0,
						PollInterval:  0,
					})
				i.setIdleing(false)
				i.done <- err
				i.log("elapsed idle time: %v", time.Since(now))
			}
		}()

	case i.isWaiting():
		i.log("not started: wait for idle to exit")
	default:
		i.log("not started: client not ready")
	}
}

func (i *idler) Stop() error {
	var reterr error
	switch {
	case i.isReady():
		close(i.stop)
		select {
		case err := <-i.done:
			if err != nil {
				i.log("<=(idle) with err: %v", err)
			}
			reterr = nil
		case <-time.After(i.config.idle_timeout):
			i.worker.PostMessage(&types.Done{
				Message: types.RespondTo(&types.Disconnect{}),
			}, nil)

			i.waitOnIdle()

			reterr = errIdleTimeout
		}
	case i.isWaiting():
		reterr = errIdleModeHangs
	default:
		reterr = nil
	}
	return reterr
}

func (i *idler) waitOnIdle() {
	i.setWaiting(true)
	go func() {
		defer log.PanicHandler()
		err := <-i.done
		if err == nil {
			i.worker.PostMessage(&types.Done{
				Message: types.RespondTo(&types.Connect{}),
			}, nil)
		} else {
			i.log("<=(idle) waited; with err: %v", err)
		}
		i.setWaiting(false)
		i.stop = make(chan struct{})
		i.Start()
	}()
}

func (i *idler) log(format string, v ...interface{}) {
	msg := fmt.Sprintf(format, v...)
	i.worker.Tracef("idler (%p) [idle:%t,wait:%t] %s", i, i.isIdleing(), i.isWaiting(), msg)
}