github.com/theQRL/go-zond@v0.2.1/zond/downloader/api.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package downloader 18 19 import ( 20 "context" 21 "sync" 22 "time" 23 24 "github.com/theQRL/go-zond" 25 "github.com/theQRL/go-zond/event" 26 "github.com/theQRL/go-zond/rpc" 27 ) 28 29 // DownloaderAPI provides an API which gives information about the current 30 // synchronisation status. It offers only methods that operates on data that 31 // can be available to anyone without security risks. 32 type DownloaderAPI struct { 33 d *Downloader 34 mux *event.TypeMux 35 installSyncSubscription chan chan interface{} 36 uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest 37 } 38 39 // NewDownloaderAPI creates a new DownloaderAPI. The API has an internal event loop that 40 // listens for events from the downloader through the global event mux. In case it receives one of 41 // these events it broadcasts it to all syncing subscriptions that are installed through the 42 // installSyncSubscription channel. 43 func NewDownloaderAPI(d *Downloader, m *event.TypeMux) *DownloaderAPI { 44 api := &DownloaderAPI{ 45 d: d, 46 mux: m, 47 installSyncSubscription: make(chan chan interface{}), 48 uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest), 49 } 50 51 go api.eventLoop() 52 53 return api 54 } 55 56 // eventLoop runs a loop until the event mux closes. It will install and uninstall 57 // new sync subscriptions and broadcasts sync status updates to the installed sync 58 // subscriptions. 59 // 60 // The sync status pushed to subscriptions can be a stream like: 61 // >>> {Syncing: true, Progress: {...}} 62 // >>> {false} 63 // 64 // If the node is already synced up, then only a single event subscribers will 65 // receive is {false}. 66 func (api *DownloaderAPI) eventLoop() { 67 var ( 68 sub = api.mux.Subscribe(StartEvent{}) 69 syncSubscriptions = make(map[chan interface{}]struct{}) 70 checkInterval = time.Second * 60 71 checkTimer = time.NewTimer(checkInterval) 72 73 // status flags 74 started bool 75 done bool 76 77 getProgress = func() zond.SyncProgress { 78 return api.d.Progress() 79 } 80 ) 81 defer checkTimer.Stop() 82 83 for { 84 select { 85 case i := <-api.installSyncSubscription: 86 syncSubscriptions[i] = struct{}{} 87 if done { 88 i <- false 89 } 90 case u := <-api.uninstallSyncSubscription: 91 delete(syncSubscriptions, u.c) 92 close(u.uninstalled) 93 case event := <-sub.Chan(): 94 if event == nil { 95 return 96 } 97 98 switch event.Data.(type) { 99 case StartEvent: 100 started = true 101 } 102 case <-checkTimer.C: 103 if !started { 104 checkTimer.Reset(checkInterval) 105 continue 106 } 107 prog := getProgress() 108 if !prog.Done() { 109 notification := &SyncingResult{ 110 Syncing: true, 111 Status: prog, 112 } 113 for c := range syncSubscriptions { 114 c <- notification 115 } 116 checkTimer.Reset(checkInterval) 117 continue 118 } 119 for c := range syncSubscriptions { 120 c <- false 121 } 122 done = true 123 } 124 } 125 } 126 127 // Syncing provides information when this node starts synchronising with the Zond network and when it's finished. 128 func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error) { 129 notifier, supported := rpc.NotifierFromContext(ctx) 130 if !supported { 131 return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported 132 } 133 134 rpcSub := notifier.CreateSubscription() 135 136 go func() { 137 statuses := make(chan interface{}) 138 sub := api.SubscribeSyncStatus(statuses) 139 defer sub.Unsubscribe() 140 141 for { 142 select { 143 case status := <-statuses: 144 notifier.Notify(rpcSub.ID, status) 145 case <-rpcSub.Err(): 146 return 147 } 148 } 149 }() 150 151 return rpcSub, nil 152 } 153 154 // SyncingResult provides information about the current synchronisation status for this node. 155 type SyncingResult struct { 156 Syncing bool `json:"syncing"` 157 Status zond.SyncProgress `json:"status"` 158 } 159 160 // uninstallSyncSubscriptionRequest uninstalls a syncing subscription in the API event loop. 161 type uninstallSyncSubscriptionRequest struct { 162 c chan interface{} 163 uninstalled chan interface{} 164 } 165 166 // SyncStatusSubscription represents a syncing subscription. 167 type SyncStatusSubscription struct { 168 api *DownloaderAPI // register subscription in event loop of this api instance 169 c chan interface{} // channel where events are broadcasted to 170 unsubOnce sync.Once // make sure unsubscribe logic is executed once 171 } 172 173 // Unsubscribe uninstalls the subscription from the DownloadAPI event loop. 174 // The status channel that was passed to subscribeSyncStatus isn't used anymore 175 // after this method returns. 176 func (s *SyncStatusSubscription) Unsubscribe() { 177 s.unsubOnce.Do(func() { 178 req := uninstallSyncSubscriptionRequest{s.c, make(chan interface{})} 179 s.api.uninstallSyncSubscription <- &req 180 181 for { 182 select { 183 case <-s.c: 184 // drop new status events until uninstall confirmation 185 continue 186 case <-req.uninstalled: 187 return 188 } 189 } 190 }) 191 } 192 193 // SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates. 194 // The given channel must receive interface values, the result can either. 195 func (api *DownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription { 196 api.installSyncSubscription <- status 197 return &SyncStatusSubscription{api: api, c: status} 198 }