forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_manager.go
445 lines (401 loc) · 13.2 KB
/
subscription_manager.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2019 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"path"
"strings"
"sync"
"time"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/tlfhandle"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
"golang.org/x/net/context"
"golang.org/x/time/rate"
)
// userPath is always the full path including the /keybase prefix, but may
// not be canonical or cleaned. The goal is to track whatever the user of this
// type is dealing with without needing them to know if a path is canonicalized
// at any time.
// Examples:
// "/keybase/public/karlthefog@twitter/dir
// "/keybase/team/keybase/dir/../file"
type userPath string
// cleanInTlfPath is clean path rooted at a TLF, and it's what we get
// from Node.GetPathPlaintextSansTlf().
// Examples, considering TLF /keybase/private/user1,user2:
// "/foo/bar" (representing /keybase/private/user1,user2/foo/bar)
// "/" (representing /keybase/private/user1,user2)
type cleanInTlfPath string
func getCleanInTlfPath(p *parsedPath) cleanInTlfPath {
return cleanInTlfPath(path.Clean(p.rawInTlfPath))
}
func getParentPath(p cleanInTlfPath) (parent cleanInTlfPath, ok bool) {
lastSlashIndex := strings.LastIndex(string(p), "/")
if lastSlashIndex <= 0 {
return "", false
}
return p[:lastSlashIndex], true
}
type debouncedNotify struct {
notify func()
shutdown func()
}
func getChSender(ch chan<- struct{}, blocking bool) func() {
if blocking {
return func() {
ch <- struct{}{}
}
}
return func() {
select {
case ch <- struct{}{}:
default:
}
}
}
func debounce(do func(), limit rate.Limit) debouncedNotify {
ctx, shutdown := context.WithCancel(context.Background())
ch := make(chan struct{}, 1)
limiter := rate.NewLimiter(limit, 1)
go func() {
for {
err := limiter.Wait(ctx)
if err != nil {
return
}
select {
case <-ch:
go do()
case <-ctx.Done():
return
}
}
}()
return debouncedNotify{
notify: getChSender(ch, limit == rate.Inf),
shutdown: shutdown,
}
}
type pathSubscriptionRef struct {
folderBranch data.FolderBranch
path cleanInTlfPath
}
// subscriptionManager manages subscriptions. There are two types of
// subscriptions: path and non-path. Path subscriptions are for changes related
// to a specific path, such as file content change, dir children change, and
// timestamp change. Non-path subscriptions are for general changes that are
// not specific to a path, such as journal flushing, online status change, etc.
// We store a debouncedNotify struct for each subscription, which includes a
// notify function that might be debounced if caller asked so.
type subscriptionManager struct {
config Config
shutdownOnlineStatusWatcher func()
lock sync.RWMutex
// TODO HOTPOT-416: add another layer here to reference by topics, and
// actually check topics in LocalChange and BatchChanges.
pathSubscriptions map[pathSubscriptionRef]map[SubscriptionID]debouncedNotify
pathSubscriptionIDToRef map[SubscriptionID]pathSubscriptionRef
nonPathSubscriptions map[keybase1.SubscriptionTopic]map[SubscriptionID]debouncedNotify
nonPathSubscriptionIDToTopic map[SubscriptionID]keybase1.SubscriptionTopic
subscriptionIDs map[SubscriptionID]bool
subscriptionCountByFolderBranch map[data.FolderBranch]int
}
type subscriber struct {
sm *subscriptionManager
notifier SubscriptionNotifier
}
func (sm *subscriptionManager) notifyOnlineStatus() {
sm.lock.RLock()
defer sm.lock.RUnlock()
if sm.nonPathSubscriptions[keybase1.SubscriptionTopic_ONLINE_STATUS] == nil {
return
}
for _, notifier := range sm.nonPathSubscriptions[keybase1.SubscriptionTopic_ONLINE_STATUS] {
notifier.notify()
}
}
func (sm *subscriptionManager) watchOnlineStatus() func() {
ctx, shutdown := context.WithCancel(context.Background())
go func() {
for sm.config.KBFSOps() == nil {
time.Sleep(100 * time.Millisecond)
}
for {
_, invalidateChan := sm.config.KBFSOps().StatusOfServices()
select {
case <-invalidateChan:
sm.notifyOnlineStatus()
case <-ctx.Done():
return
}
}
}()
return shutdown
}
func newSubscriptionManager(config Config) (SubscriptionManager, SubscriptionManagerPublisher) {
sm := &subscriptionManager{
pathSubscriptions: make(map[pathSubscriptionRef]map[SubscriptionID]debouncedNotify),
pathSubscriptionIDToRef: make(map[SubscriptionID]pathSubscriptionRef),
nonPathSubscriptions: make(map[keybase1.SubscriptionTopic]map[SubscriptionID]debouncedNotify),
nonPathSubscriptionIDToTopic: make(map[SubscriptionID]keybase1.SubscriptionTopic),
config: config,
subscriptionIDs: make(map[SubscriptionID]bool),
subscriptionCountByFolderBranch: make(map[data.FolderBranch]int),
}
sm.shutdownOnlineStatusWatcher = sm.watchOnlineStatus()
return sm, sm
}
func (sm *subscriptionManager) Shutdown(ctx context.Context) {
sm.shutdownOnlineStatusWatcher()
pathSids := make([]SubscriptionID, 0, len(sm.pathSubscriptionIDToRef))
nonPathSids := make([]SubscriptionID, 0, len(sm.nonPathSubscriptionIDToTopic))
for sid := range sm.pathSubscriptionIDToRef {
pathSids = append(pathSids, sid)
}
for sid := range sm.nonPathSubscriptionIDToTopic {
nonPathSids = append(nonPathSids, sid)
}
for _, sid := range pathSids {
sm.unsubscribePath(ctx, sid)
}
for _, sid := range nonPathSids {
sm.unsubscribeNonPath(ctx, sid)
}
}
func (sm *subscriptionManager) Subscriber(notifier SubscriptionNotifier) Subscriber {
return subscriber{sm: sm, notifier: notifier}
}
func (sm *subscriptionManager) checkSubscriptionIDLocked(sid SubscriptionID) (setter func(), err error) {
if sm.subscriptionIDs[sid] {
return nil, errors.Errorf("duplicate subscription ID %q", sid)
}
return func() {
sm.subscriptionIDs[sid] = true
}, nil
}
func (sm *subscriptionManager) registerForChangesLocked(fb data.FolderBranch) {
if sm.subscriptionCountByFolderBranch[fb] == 0 {
_ = sm.config.Notifier().RegisterForChanges(
[]data.FolderBranch{fb}, sm)
}
sm.subscriptionCountByFolderBranch[fb]++
}
func (sm *subscriptionManager) unregisterForChangesLocked(fb data.FolderBranch) {
if sm.subscriptionCountByFolderBranch[fb] == 1 {
_ = sm.config.Notifier().UnregisterFromChanges(
[]data.FolderBranch{fb}, sm)
delete(sm.subscriptionCountByFolderBranch, fb)
return
}
sm.subscriptionCountByFolderBranch[fb]--
}
func (sm *subscriptionManager) subscribePath(ctx context.Context,
sid SubscriptionID, path string, topic keybase1.PathSubscriptionTopic,
deduplicateInterval *time.Duration, notifier SubscriptionNotifier) error {
parsedPath, err := parsePath(userPath(path))
if err != nil {
return err
}
fb, err := parsedPath.getFolderBranch(ctx, sm.config)
if err != nil {
return err
}
if fb == (data.FolderBranch{}) {
// ignore non-existent TLF.
// TODO: deal with this case HOTPOTP-501
return nil
}
nitp := getCleanInTlfPath(parsedPath)
ref := pathSubscriptionRef{
folderBranch: fb,
path: nitp,
}
sm.lock.Lock()
defer sm.lock.Unlock()
subscriptionIDSetter, err := sm.checkSubscriptionIDLocked(sid)
if err != nil {
return err
}
sm.registerForChangesLocked(ref.folderBranch)
if sm.pathSubscriptions[ref] == nil {
sm.pathSubscriptions[ref] = make(map[SubscriptionID]debouncedNotify)
}
limit := rate.Inf
if deduplicateInterval != nil {
limit = rate.Every(*deduplicateInterval)
}
sm.pathSubscriptions[ref][sid] = debounce(func() {
notifier.OnPathChange(sid, path, topic)
}, limit)
sm.pathSubscriptionIDToRef[sid] = ref
subscriptionIDSetter()
return nil
}
func (sm *subscriptionManager) subscribeNonPath(
ctx context.Context, sid SubscriptionID, topic keybase1.SubscriptionTopic,
deduplicateInterval *time.Duration, notifier SubscriptionNotifier) error {
sm.lock.Lock()
defer sm.lock.Unlock()
subscriptionIDSetter, err := sm.checkSubscriptionIDLocked(sid)
if err != nil {
return err
}
if sm.nonPathSubscriptions[topic] == nil {
sm.nonPathSubscriptions[topic] = make(map[SubscriptionID]debouncedNotify)
}
limit := rate.Inf
if deduplicateInterval != nil {
limit = rate.Every(*deduplicateInterval)
}
sm.nonPathSubscriptions[topic][sid] = debounce(func() {
notifier.OnNonPathChange(sid, topic)
}, limit)
sm.nonPathSubscriptionIDToTopic[sid] = topic
subscriptionIDSetter()
return nil
}
func (sm *subscriptionManager) unsubscribePath(
ctx context.Context, subscriptionID SubscriptionID) {
sm.lock.Lock()
defer sm.lock.Unlock()
ref, ok := sm.pathSubscriptionIDToRef[subscriptionID]
if !ok {
return
}
delete(sm.pathSubscriptionIDToRef, subscriptionID)
if (sm.pathSubscriptions[ref]) == nil {
return
}
if notifier, ok := sm.pathSubscriptions[ref][subscriptionID]; ok {
notifier.shutdown()
delete(sm.pathSubscriptions[ref], subscriptionID)
}
if len(sm.pathSubscriptions[ref]) == 0 {
sm.unregisterForChangesLocked(ref.folderBranch)
delete(sm.pathSubscriptions, ref)
}
delete(sm.subscriptionIDs, subscriptionID)
}
func (sm *subscriptionManager) unsubscribeNonPath(
ctx context.Context, subscriptionID SubscriptionID) {
sm.lock.Lock()
defer sm.lock.Unlock()
topic, ok := sm.nonPathSubscriptionIDToTopic[subscriptionID]
if !ok {
return
}
delete(sm.nonPathSubscriptionIDToTopic, subscriptionID)
if sm.nonPathSubscriptions[topic] == nil {
return
}
if notifier, ok := sm.nonPathSubscriptions[topic][subscriptionID]; ok {
notifier.shutdown()
delete(sm.nonPathSubscriptions[topic], subscriptionID)
}
// We are not deleting empty topics here because there are very few topics
// here, and they very likely need to be used soon, so I figured I'd just
// leave it there. The path subscriptions are different as they are
// referenced by path.
delete(sm.subscriptionIDs, subscriptionID)
}
func (sm *subscriptionManager) notifyRef(ref pathSubscriptionRef) {
if sm.pathSubscriptions[ref] == nil {
return
}
for _, notifier := range sm.pathSubscriptions[ref] {
// We are notify()-ing while holding a lock, but it's fine since the
// other side of the channel consumes it pretty fast, either by
// dropping deduplicated ones, or by doing the actual send in a
// separate goroutine.
notifier.notify()
}
}
func (sm *subscriptionManager) nodeChangeLocked(node Node) {
path, ok := node.GetPathPlaintextSansTlf()
if !ok {
return
}
cleanPath := cleanInTlfPath(path)
sm.notifyRef(pathSubscriptionRef{
folderBranch: node.GetFolderBranch(),
path: cleanPath,
})
// Do this for parent as well, so if "children" is subscribed on parent
// path, we'd trigger a notification too.
if parent, ok := getParentPath(cleanPath); ok {
sm.notifyRef(pathSubscriptionRef{
folderBranch: node.GetFolderBranch(),
path: parent,
})
}
}
// SubscribePath implements the Subscriber interface.
func (s subscriber) SubscribePath(ctx context.Context, sid SubscriptionID,
path string, topic keybase1.PathSubscriptionTopic,
deduplicateInterval *time.Duration) error {
return s.sm.subscribePath(ctx,
sid, path, topic, deduplicateInterval, s.notifier)
}
// SubscribeNonPath implements the Subscriber interface.
func (s subscriber) SubscribeNonPath(ctx context.Context, sid SubscriptionID,
topic keybase1.SubscriptionTopic,
deduplicateInterval *time.Duration) error {
return s.sm.subscribeNonPath(ctx,
sid, topic, deduplicateInterval, s.notifier)
}
// Unsubscribe implements the Subscriber interface.
func (s subscriber) Unsubscribe(ctx context.Context, sid SubscriptionID) {
s.sm.unsubscribePath(ctx, sid)
s.sm.unsubscribeNonPath(ctx, sid)
}
var _ SubscriptionManagerPublisher = (*subscriptionManager)(nil)
// PublishChange implements the SubscriptionManagerPublisher interface.
func (sm *subscriptionManager) PublishChange(topic keybase1.SubscriptionTopic) {
sm.lock.RLock()
defer sm.lock.RUnlock()
// When sync status changes, trigger notification for all paths so they
// reload to get new prefetch status. This is unfortunate but it's
// non-trivial to actually build notification around individuall path's
// prefetch status. Since GUI doesnt' have that many path notifications,
// this should be fine.
//
// TODO: Build it.
if topic == keybase1.SubscriptionTopic_OVERALL_SYNC_STATUS {
for _, subscriptions := range sm.pathSubscriptions {
for _, notifier := range subscriptions {
notifier.notify()
}
}
}
if sm.nonPathSubscriptions[topic] == nil {
return
}
for _, notifier := range sm.nonPathSubscriptions[topic] {
notifier.notify()
}
}
var _ Observer = (*subscriptionManager)(nil)
// LocalChange implements the Observer interface.
func (sm *subscriptionManager) LocalChange(ctx context.Context,
node Node, write WriteRange) {
sm.lock.RLock()
defer sm.lock.RUnlock()
// TODO HOTPOT-416: check topics
sm.nodeChangeLocked(node)
}
// BatchChanges implements the Observer interface.
func (sm *subscriptionManager) BatchChanges(ctx context.Context,
changes []NodeChange, allAffectedNodeIDs []NodeID) {
sm.lock.RLock()
defer sm.lock.RUnlock()
// TODO HOTPOT-416: check topics
for _, change := range changes {
sm.nodeChangeLocked(change.Node)
}
}
// TlfHandleChange implements the Observer interface.
func (sm *subscriptionManager) TlfHandleChange(ctx context.Context,
newHandle *tlfhandle.Handle) {
}