forked from keybase/client
/
syncer.go
79 lines (62 loc) · 1.71 KB
/
syncer.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"sync"
"golang.org/x/net/context"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
type Syncer interface {
Contextifier
sync.Locker
loadFromStorage(keybase1.UID) error
syncFromServer(keybase1.UID, SessionReader) error
store(keybase1.UID) error
needsLogin() bool
}
func RunSyncer(s Syncer, uid keybase1.UID, loggedIn bool, sr SessionReader) (err error) {
if uid.IsNil() {
return NotFoundError{"No UID given to syncer"}
}
// unnecessary for secret syncer, but possibly useful for tracker syncer.
s.Lock()
defer s.Unlock()
s.G().Log.Debug("+ Syncer.Load(%s)", uid)
defer func() {
s.G().Log.Debug("- Syncer.Load(%s) -> %s", uid, ErrToOk(err))
}()
if err = s.loadFromStorage(uid); err != nil {
return
}
if s.G().ConnectivityMonitor.IsConnected(context.Background()) == ConnectivityMonitorNo {
s.G().Log.Debug("| not connected, won't sync with server")
return
}
if s.needsLogin() && !loggedIn {
s.G().Log.Debug("| Won't sync with server since we're not logged in")
return
}
if err = s.syncFromServer(uid, sr); err != nil {
return
}
if err = s.store(uid); err != nil {
return
}
return
}
func RunSyncerCached(s Syncer, uid keybase1.UID) (err error) {
if uid.IsNil() {
return NotFoundError{"No UID given to syncer"}
}
// unnecessary for secret syncer, but possibly useful for tracker syncer.
s.Lock()
defer s.Unlock()
s.G().Log.Debug("+ SyncerCached.Load(%s)", uid)
defer func() {
s.G().Log.Debug("- SyncerCached.Load(%s) -> %s", uid, ErrToOk(err))
}()
if err = s.loadFromStorage(uid); err != nil {
return err
}
return nil
}