Debian Codegraph @

worker/imap/idler.go

raw
1 package imap
2
3 import (
4 "fmt"
5 "time"
6
7 "git.sr.ht/~rjarry/aerc/lib/log"
8 "git.sr.ht/~rjarry/aerc/worker/types"
9 "github.com/emersion/go-imap"
10 )
11
12 var errIdleTimeout = fmt.Errorf("idle timeout")
13
14 // idler manages the idle mode of the imap server. Enter idle mode if there's
15 // no other task and leave idle mode when a new task arrives. Idle mode is only
16 // used when the client is ready and connected. After a connection loss, make
17 // sure that idling returns gracefully and the worker remains responsive.
18 type idler struct {
19 client *imapClient
20 debouncer *time.Timer
21 debounce time.Duration
22 timeout time.Duration
23 worker types.WorkerInteractor
24 stop chan struct{}
25 start chan struct{}
26 done chan error
27 }
28
29 func newIdler(w types.WorkerInteractor, startIdler chan struct{}) *idler {
30 return &idler{
31 debouncer: nil,
32 debounce: 10 * time.Millisecond,
33 timeout: 10 * time.Second,
34 worker: w,
35 stop: make(chan struct{}),
36 start: startIdler,
37 done: make(chan error),
38 }
39 }
40
41 func (i *idler) SetClient(c *imapClient) {
42 i.client = c
43 }
44
45 func (i *idler) ready() bool {
46 return (i.client != nil && i.client.State() == imap.SelectedState)
47 }
48
49 func (i *idler) Start() {
50 if !i.ready() {
51 return
52 }
53
54 select {
55 case <-i.stop:
56 // stop channel is nil (probably after a debounce), we don't
57 // want to close it
58 default:
59 close(i.stop)
60 }
61
62 // create new stop channel
63 i.stop = make(chan struct{})
64
65 // clear done channel
66 clearing := true
67 for clearing {
68 select {
69 case <-i.done:
70 continue
71 default:
72 clearing = false
73 }
74 }
75
76 i.worker.Tracef("idler (start): start idle after debounce")
77 i.debouncer = time.AfterFunc(i.debounce, func() {
78 i.start <- struct{}{}
79 i.worker.Tracef("idler (start): started")
80 })
81 }
82
83 func (i *idler) Execute() {
84 if !i.ready() {
85 return
86 }
87
88 // we need to call client.Idle in a goroutine since it is blocking call
89 // and we still want to receive messages
90 go func() {
91 defer log.PanicHandler()
92
93 start := time.Now()
94 err := i.client.Idle(i.stop, nil)
95 if err != nil {
96 i.worker.Errorf("idle returned error: %v", err)
97 }
98 i.worker.Tracef("idler (execute): idleing for %s", time.Since(start))
99
100 i.done <- err
101 }()
102 }
103
104 func (i *idler) Stop() error {
105 if !i.ready() {
106 return nil
107 }
108
109 select {
110 case <-i.stop:
111 i.worker.Debugf("idler (stop): idler already stopped?")
112 return nil
113 default:
114 close(i.stop)
115 }
116
117 if i.debouncer != nil {
118 if i.debouncer.Stop() {
119 i.worker.Tracef("idler (stop): debounced")
120 return nil
121 }
122 }
123
124 select {
125 case err := <-i.done:
126 i.worker.Tracef("idler (stop): idle stopped: %v", err)
127 return err
128 case <-time.After(i.timeout):
129 i.worker.Errorf("idler (stop): cannot stop idle (timeout)")
130 return errIdleTimeout
131 }
132 }