-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cache.go
309 lines (253 loc) · 7.75 KB
/
cache.go
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package proton_api_bridge
import (
"context"
"log"
"sync"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/henrybear327/go-proton-api"
)
type cacheEntry struct {
link *proton.Link
kr *crypto.KeyRing
}
type cache struct {
data map[string]*cacheEntry
children map[string]map[string]interface{}
enableCaching bool
sync.RWMutex
}
func newCache(enableCaching bool) *cache {
return &cache{
data: make(map[string]*cacheEntry),
children: make(map[string]map[string]interface{}),
enableCaching: enableCaching,
}
}
// func (cache *cache) _debug() {
// if !cache.enableCaching {
// return
// }
// cache.RLock()
// defer cache.RUnlock()
// for k, v := range cache.data {
// log.Printf("data %#v %p", k, v.link)
// }
// for k, v := range cache.children {
// log.Printf("children %#v %#v", k, v)
// }
// }
func (cache *cache) _get(linkID string) *cacheEntry {
if !cache.enableCaching {
return nil
}
cache.RLock()
defer cache.RUnlock()
if data, ok := cache.data[linkID]; ok {
return data
}
return nil
}
func (cache *cache) _insert(linkID string, link *proton.Link, kr *crypto.KeyRing) {
if !cache.enableCaching {
return
}
cache.Lock()
defer cache.Unlock()
cache.data[linkID] = &cacheEntry{
link: link,
kr: kr,
}
if link != nil {
if data, ok := cache.children[link.ParentLinkID]; ok {
data[link.LinkID] = nil
cache.children[link.ParentLinkID] = data
} else {
tmp := make(map[string]interface{})
tmp[link.LinkID] = nil
cache.children[link.ParentLinkID] = tmp
}
} else {
// TODO: we should never have missing link though
log.Fatalln("we should never have missing link though")
}
}
// due to recursion, we can't perform locking here
// this function should only be called from _remove
func (cache *cache) _remove_nolock(linkID string, includingChildren bool) {
var link *proton.Link
if data, ok := cache.data[linkID]; ok {
link = data.link
delete(cache.data, linkID)
} else {
return
}
// remove linkID from parent's map
if data, ok := cache.children[link.ParentLinkID]; ok {
if _, ok := data[link.LinkID]; ok {
delete(data, link.LinkID)
cache.children[link.ParentLinkID] = data
} else {
log.Fatalln("we have an issue for cache inconsistency where link is not found in the parent's map")
}
} else {
log.Fatalln("we have an issue for cache inconsistency where link's parent map is missing")
}
// we don't recursively go upward to clean up the parent's map
// instead, we rely on periodic cache flushing
if includingChildren {
if data, ok := cache.children[link.LinkID]; ok {
for k := range data {
cache._remove_nolock(k, true)
}
} // else {
// might have nothing is the link doesn't have any children
// }
delete(cache.children, link.LinkID)
}
}
func (cache *cache) _remove(linkID string, includingChildren bool) {
if !cache.enableCaching {
return
}
cache.Lock()
defer cache.Unlock()
cache._remove_nolock(linkID, includingChildren)
}
/* The original non-caching version, which resolves the keyring recursively */
func (protonDrive *ProtonDrive) _getLinkKRByID(ctx context.Context, linkID string) (*crypto.KeyRing, error) {
if linkID == "" {
// most likely someone requested root link's parent link, which happen to be ""
// return protonDrive.MainShareKR.Copy() // we need to return a deep copy since the keyring will be freed by the caller when it finishes using the keyring -> now we go through caching, so we won't clear kr
return protonDrive.MainShareKR, nil
}
link, err := protonDrive.getLink(ctx, linkID)
if err != nil {
return nil, err
}
return protonDrive._getLinkKR(ctx, link)
}
/* The original non-caching version, which resolves the keyring recursively */
func (protonDrive *ProtonDrive) _getLinkKR(ctx context.Context, link *proton.Link) (*crypto.KeyRing, error) {
if link.ParentLinkID == "" { // link is rootLink
signatureVerificationKR, err := protonDrive.getSignatureVerificationKeyring([]string{link.SignatureEmail})
if err != nil {
return nil, err
}
nodeKR, err := link.GetKeyRing(protonDrive.MainShareKR, signatureVerificationKR)
if err != nil {
return nil, err
}
return nodeKR, nil
}
parentLink, err := protonDrive.getLink(ctx, link.ParentLinkID)
if err != nil {
return nil, err
}
// parentNodeKR is used to decrypt the current node's KR, as each node has its keyring, which can be decrypted by its parent
parentNodeKR, err := protonDrive._getLinkKR(ctx, parentLink)
if err != nil {
return nil, err
}
signatureVerificationKR, err := protonDrive.getSignatureVerificationKeyring([]string{link.SignatureEmail})
if err != nil {
return nil, err
}
nodeKR, err := link.GetKeyRing(parentNodeKR, signatureVerificationKR)
if err != nil {
return nil, err
}
return nodeKR, nil
}
func (protonDrive *ProtonDrive) getLink(ctx context.Context, linkID string) (*proton.Link, error) {
if linkID == "" {
// this is only possible when doing rootLink's parent, which should be handled beforehand
return nil, ErrWrongUsageOfGetLink
}
// attempt to get from cache first
if data := protonDrive.cache._get(linkID); data != nil && data.link != nil {
return data.link, nil
}
// no cached data, fetch
link, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID)
if err != nil {
return nil, err
}
// populate cache
protonDrive.cache._insert(linkID, &link, nil)
return &link, nil
}
func (protonDrive *ProtonDrive) getLinkKR(ctx context.Context, link *proton.Link) (*crypto.KeyRing, error) {
if !protonDrive.cache.enableCaching {
return protonDrive._getLinkKR(ctx, link)
}
if link == nil {
return nil, ErrWrongUsageOfGetLinkKR
}
// attempt to get from cache first
if data := protonDrive.cache._get(link.LinkID); data != nil && data.link != nil {
if data.kr != nil {
return data.kr, nil
}
// decrypt keyring and cache it
parentNodeKR, err := protonDrive.getLinkKRByID(ctx, data.link.ParentLinkID)
if err != nil {
return nil, err
}
signatureVerificationKR, err := protonDrive.getSignatureVerificationKeyring([]string{data.link.SignatureEmail})
if err != nil {
return nil, err
}
kr, err := data.link.GetKeyRing(parentNodeKR, signatureVerificationKR)
if err != nil {
return nil, err
}
data.kr = kr
return data.kr, nil
}
// no cached data, fetch
protonDrive.cache._insert(link.LinkID, link, nil)
return protonDrive.getLinkKR(ctx, link)
}
func (protonDrive *ProtonDrive) getLinkKRByID(ctx context.Context, linkID string) (*crypto.KeyRing, error) {
if !protonDrive.cache.enableCaching {
return protonDrive._getLinkKRByID(ctx, linkID)
}
if linkID == "" {
return protonDrive.MainShareKR, nil
}
// attempt to get from cache first
if data := protonDrive.cache._get(linkID); data != nil && data.link != nil {
return protonDrive.getLinkKR(ctx, data.link)
}
// log.Println("Not from cache")
// no cached data, fetch
link, err := protonDrive.getLink(ctx, linkID)
if err != nil {
return nil, err
}
return protonDrive.getLinkKR(ctx, link)
}
func (protonDrive *ProtonDrive) removeLinkIDFromCache(linkID string, includingChildren bool) {
if !protonDrive.cache.enableCaching {
return
}
// log.Println("===================================")
// log.Println(linkID, includingChildren)
// protonDrive.cache._debug()
protonDrive.cache._remove(linkID, includingChildren)
// protonDrive.cache._debug()
// log.Println("===================================")
}
func (protonDrive *ProtonDrive) ClearCache() {
if !protonDrive.cache.enableCaching {
return
}
protonDrive.cache.Lock()
defer protonDrive.cache.Unlock()
// TODO: we come back to fix this later
// for _, entry := range protonDrive.cache.data {
// entry.kr.ClearPrivateParams()
// }
protonDrive.cache.data = make(map[string]*cacheEntry)
protonDrive.cache.children = make(map[string]map[string]interface{})
}