emit ConnStatus always to get an update when peers change connectivity (#46)

* emit ConnStatus event always to get an update when peers change
connectivity
* use peers map to determine peer count
This commit is contained in:
RichΛrd 2021-08-31 13:27:42 -04:00 committed by Vitaliy Vlasov
parent 53b3d19948
commit 52b46d6869
1 changed files with 6 additions and 5 deletions

View File

@ -163,8 +163,7 @@ func (w *WakuNode) processHostEvent(e interface{}) {
newHasHistory := w.HasHistory() newHasHistory := w.HasHistory()
log.Info("###ConnStatus isOnline: ", isOnline, "/", newIsOnline, " hasHistory: ", log.Info("###ConnStatus isOnline: ", isOnline, "/", newIsOnline, " hasHistory: ",
hasHistory, "/", newHasHistory) hasHistory, "/", newHasHistory)
if w.connStatusChan != nil && if w.connStatusChan != nil {
(isOnline != newIsOnline || hasHistory != newHasHistory) {
// Creating a copy of the current peers map // Creating a copy of the current peers map
w.peersMutex.Lock() w.peersMutex.Lock()
@ -174,8 +173,10 @@ func (w *WakuNode) processHostEvent(e interface{}) {
} }
w.peersMutex.Unlock() w.peersMutex.Unlock()
log.Info("New ConnStatus: ", ConnStatus{IsOnline: newIsOnline, HasHistory: newHasHistory, Peers: p}) connStatus := ConnStatus{IsOnline: newIsOnline, HasHistory: newHasHistory, Peers: p}
w.connStatusChan <- ConnStatus{IsOnline: newIsOnline, HasHistory: newHasHistory, Peers: p}
log.Info("New ConnStatus: ", connStatus)
w.connStatusChan <- connStatus
} }
} }
@ -728,7 +729,7 @@ func (w *WakuNode) ClosePeerById(id peer.ID) error {
} }
func (w *WakuNode) PeerCount() int { func (w *WakuNode) PeerCount() int {
return len(w.host.Network().Peers()) return len(w.peers)
} }
func (w *WakuNode) startKeepAlive(t time.Duration) { func (w *WakuNode) startKeepAlive(t time.Duration) {