Debian Codegraph @

worker/types/worker.go

raw
1 package types
2
3 import (
4 "context"
5 "sync"
6 "sync/atomic"
7
8 "git.sr.ht/~rjarry/aerc/lib/log"
9 "git.sr.ht/~rjarry/aerc/models"
10 )
11
12 type WorkerInteractor interface {
13 log.Logger
14 Actions() chan WorkerMessage
15 ProcessAction(WorkerMessage) WorkerMessage
16 PostAction(context.Context, WorkerMessage, func(msg WorkerMessage))
17 PostMessage(WorkerMessage, func(msg WorkerMessage))
18 Unwrap() WorkerInteractor
19 Name() string
20 }
21
22 var lastId int64 = 1 // access via atomic
23
24 type Backend interface {
25 Run()
26 Capabilities() *models.Capabilities
27 PathSeparator() string
28 }
29
30 type Worker struct {
31 Backend Backend
32
33 actions chan WorkerMessage
34 actionCallbacks map[int64]func(msg WorkerMessage)
35 messages chan WorkerMessage
36 messageCallbacks map[int64]func(msg WorkerMessage)
37 name string
38
39 sync.Mutex
40 log.Logger
41 }
42
43 func NewWorker(name string, messages chan WorkerMessage) *Worker {
44 return &Worker{
45 Logger: log.NewLogger(name, 2),
46 actions: make(chan WorkerMessage, 32),
47 actionCallbacks: make(map[int64]func(msg WorkerMessage)),
48 messages: messages,
49 messageCallbacks: make(map[int64]func(msg WorkerMessage)),
50 name: name,
51 }
52 }
53
54 func (worker *Worker) Unwrap() WorkerInteractor {
55 return nil
56 }
57
58 func (worker *Worker) Actions() chan WorkerMessage {
59 return worker.actions
60 }
61
62 func (worker *Worker) Name() string {
63 return worker.name
64 }
65
66 func (worker *Worker) setId(msg WorkerMessage) {
67 id := atomic.AddInt64(&lastId, 1)
68 msg.SetId(id)
69 }
70
71 // PostAction posts an action to the worker. This method should not be called
72 // from the same goroutine that the worker runs in or deadlocks may occur.
73 // If ctx is non-nil, it will be attached to the message for cancellation.
74 func (worker *Worker) PostAction(
75 ctx context.Context, msg WorkerMessage, cb func(msg WorkerMessage),
76 ) {
77 worker.setId(msg)
78 if ctx != nil {
79 msg.SetContext(ctx)
80 }
81 msg.SetAccount(worker.name)
82
83 if cb != nil {
84 worker.Lock()
85 worker.actionCallbacks[msg.GetId()] = cb
86 worker.Unlock()
87 }
88
89 worker.actions <- msg
90 }
91
92 // PostMessage posts an message to the UI. This method should not be called
93 // from the same goroutine that the UI runs in or deadlocks may occur
94 func (worker *Worker) PostMessage(msg WorkerMessage,
95 cb func(msg WorkerMessage),
96 ) {
97 worker.setId(msg)
98 msg.SetAccount(worker.name)
99
100 if cb != nil {
101 worker.Lock()
102 worker.messageCallbacks[msg.GetId()] = cb
103 worker.Unlock()
104 }
105
106 worker.messages <- msg
107 }
108
109 func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
110 if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
111 worker.Lock()
112 f, ok := worker.actionCallbacks[inResponseTo.GetId()]
113 worker.Unlock()
114 if ok {
115 f(msg)
116 switch msg.(type) {
117 case *Cancelled, *Done:
118 worker.Lock()
119 delete(worker.actionCallbacks, inResponseTo.GetId())
120 worker.Unlock()
121 }
122 }
123 }
124 return msg
125 }
126
127 func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage {
128 if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
129 worker.Lock()
130 f, ok := worker.messageCallbacks[inResponseTo.GetId()]
131 worker.Unlock()
132 if ok {
133 f(msg)
134 if _, ok := msg.(*Done); ok {
135 worker.Lock()
136 delete(worker.messageCallbacks, inResponseTo.GetId())
137 worker.Unlock()
138 }
139 }
140 }
141 return msg
142 }
143
144 func (worker *Worker) PathSeparator() string {
145 return worker.Backend.PathSeparator()
146 }