summary refs log tree commit diff stats
path: root/lib/msgstore.go
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-04-28 13:26:38 +0000
committerDrew DeVault <sir@cmpwn.com>2019-04-29 09:49:48 -0400
commita275f65848f2c1fdd4302f56121defc408e7d8b6 (patch)
tree13dfcf8e26f904f2de3d2c494b90c4f6c2fd8be0 /lib/msgstore.go
parentf1698a337eb2b511835cd6cc38bf76d8e776ffa1 (diff)
downloadaerc-a275f65848f2c1fdd4302f56121defc408e7d8b6.tar.gz
lib/msgstore: protect with a mutex
MessageStore has a lot of exported fields that can be read from the outside.
Each read must be protected, because a call from Update could happen at any
time.
Diffstat (limited to 'lib/msgstore.go')
-rw-r--r--lib/msgstore.go25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/msgstore.go b/lib/msgstore.go
index 9d537fc..b39d0bb 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -2,6 +2,7 @@ package lib
 
 import (
 	"io"
+	"sync"
 	"time"
 
 	"github.com/emersion/go-imap"
@@ -9,7 +10,10 @@ import (
 	"git.sr.ht/~sircmpwn/aerc2/worker/types"
 )
 
+// Accesses to fields must be guarded by MessageStore.Lock/Unlock
 type MessageStore struct {
+	sync.Mutex
+
 	Deleted  map[uint32]interface{}
 	DirInfo  types.DirectoryInfo
 	Messages map[uint32]*types.MessageInfo
@@ -45,6 +49,9 @@ func NewMessageStore(worker *types.Worker,
 func (store *MessageStore) FetchHeaders(uids []uint32,
 	cb func(*types.MessageInfo)) {
 
+	store.Lock()
+	defer store.Unlock()
+
 	// TODO: this could be optimized by pre-allocating toFetch and trimming it
 	// at the end. In practice we expect to get most messages back in one frame.
 	var toFetch imap.SeqSet
@@ -67,6 +74,9 @@ func (store *MessageStore) FetchHeaders(uids []uint32,
 }
 
 func (store *MessageStore) FetchFull(uids []uint32, cb func(io.Reader)) {
+	store.Lock()
+	defer store.Unlock()
+
 	// TODO: this could be optimized by pre-allocating toFetch and trimming it
 	// at the end. In practice we expect to get most messages back in one frame.
 	var toFetch imap.SeqSet
@@ -103,8 +113,7 @@ func (store *MessageStore) FetchBodyPart(
 	})
 }
 
-func (store *MessageStore) merge(
-	to *types.MessageInfo, from *types.MessageInfo) {
+func merge(to *types.MessageInfo, from *types.MessageInfo) {
 
 	if from.BodyStructure != nil {
 		to.BodyStructure = from.BodyStructure
@@ -125,6 +134,8 @@ func (store *MessageStore) merge(
 }
 
 func (store *MessageStore) Update(msg types.WorkerMessage) {
+	store.Lock()
+
 	update := false
 	switch msg := msg.(type) {
 	case *types.DirectoryInfo:
@@ -144,7 +155,7 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
 		update = true
 	case *types.MessageInfo:
 		if existing, ok := store.Messages[msg.Uid]; ok && existing != nil {
-			store.merge(existing, msg)
+			merge(existing, msg)
 		} else {
 			store.Messages[msg.Uid] = msg
 		}
@@ -186,6 +197,9 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
 		store.Uids = uids
 		update = true
 	}
+
+	store.Unlock()
+
 	if update {
 		store.update()
 	}
@@ -202,11 +216,16 @@ func (store *MessageStore) update() {
 }
 
 func (store *MessageStore) Delete(uids []uint32) {
+	store.Lock()
+
 	var set imap.SeqSet
 	for _, uid := range uids {
 		set.AddNum(uid)
 		store.Deleted[uid] = nil
 	}
+
+	store.Unlock()
+
 	store.worker.PostAction(&types.DeleteMessages{Uids: set}, nil)
 	store.update()
 }
updated pydoc pages' href='/akspecs/ranger/commit/doc/test.tc_directory.html?h=v1.6.1&id=f07bb12fc5c59430e995a64956b36331ce3629b9'>f07bb12f ^
34a60763 ^
f07bb12f ^
34a60763 ^
f07bb12f ^





34a60763 ^
f07bb12f ^





34a60763 ^
f07bb12f ^

34a60763 ^
f07bb12f ^

34a60763 ^
f07bb12f ^



34a60763 ^
f07bb12f ^
34a60763 ^
f07bb12f ^
34a60763 ^
f07bb12f ^






f07bb12f ^

34a60763 ^
f07bb12f ^
34a60763 ^





f07bb12f ^
34a60763 ^

f07bb12f ^
34a60763 ^
f07bb12f ^
34a60763 ^





f07bb12f ^
34a60763 ^

f07bb12f ^
34a60763 ^





f07bb12f ^






34a60763 ^

f07bb12f ^
34a60763 ^

f07bb12f ^












34a60763 ^
f07bb12f ^







34a60763 ^


f07bb12f ^
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205