github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/syncer.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "fmt" 8 "sync" 9 10 keybase1 "github.com/keybase/client/go/protocol/keybase1" 11 "golang.org/x/net/context" 12 ) 13 14 type Syncer interface { 15 Contextifier 16 sync.Locker 17 loadFromStorage(m MetaContext, u keybase1.UID, useExpiration bool) error 18 syncFromServer(m MetaContext, u keybase1.UID, forceReload bool) error 19 store(m MetaContext, u keybase1.UID) error 20 needsLogin(m MetaContext) bool 21 } 22 23 func RunSyncer(m MetaContext, s Syncer, uid keybase1.UID, loggedIn bool, forceReload bool) (err error) { 24 if uid.IsNil() { 25 return NotFoundError{"No UID given to syncer"} 26 } 27 28 // unnecessary for secret syncer, but possibly useful for tracker syncer. 29 s.Lock() 30 defer s.Unlock() 31 32 defer m.Trace(fmt.Sprintf("RunSyncer(%s)", uid), &err)() 33 34 if err = s.loadFromStorage(m, uid, true); err != nil { 35 return 36 } 37 38 if m.G().ConnectivityMonitor.IsConnected(context.Background()) == ConnectivityMonitorNo { 39 m.Debug("| not connected, won't sync with server") 40 return 41 } 42 43 if s.needsLogin(m) && !loggedIn { 44 m.Debug("| Won't sync with server since we're not logged in") 45 return 46 } 47 if err = s.syncFromServer(m, uid, forceReload); err != nil { 48 return 49 } 50 if err = s.store(m, uid); err != nil { 51 return 52 } 53 54 return 55 } 56 57 func RunSyncerCached(m MetaContext, s Syncer, uid keybase1.UID) (err error) { 58 if uid.IsNil() { 59 return NotFoundError{"No UID given to syncer"} 60 } 61 62 // unnecessary for secret syncer, but possibly useful for tracker syncer. 63 s.Lock() 64 defer s.Unlock() 65 66 defer m.Trace(fmt.Sprintf("RunSyncerCached(%s)", uid), &err)() 67 68 return s.loadFromStorage(m, uid, false) 69 }