Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix_: add ticker to check peers and update connection status #5689

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 61 additions & 55 deletions wakuv2/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,69 +1352,19 @@ func (w *Waku) Start() error {

go func() {
defer w.wg.Done()

ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-w.ctx.Done():
return

case <-w.topicHealthStatusChan:
// TODO: https://github.com/status-im/status-go/issues/4628

case <-w.connectionNotifChan:

isOnline := len(w.node.Host().Network().Peers()) > 0

if w.cfg.LightClient {
// TODO: Temporary changes for lightNodes to have health check based on connected peers.
//This needs to be enhanced to be based on healthy Filter and lightPush peers available for each shard.
//This would get fixed as part of https://github.com/waku-org/go-waku/issues/1114

subs := w.node.FilterLightnode().Subscriptions()
w.logger.Debug("filter subs count", zap.Int("count", len(subs)))

//TODO: needs fixing, right now invoking everytime.
//Trigger FilterManager to take care of any pending filter subscriptions
//TODO: Pass pubsubTopic based on topicHealth notif received.
go w.filterManager.onConnectionStatusChange("", isOnline)

}
w.connStatusMu.Lock()

latestConnStatus := types.ConnStatus{
IsOnline: isOnline,
Peers: FormatPeerStats(w.node),
}

w.logger.Debug("peer stats",
zap.Int("peersCount", len(latestConnStatus.Peers)),
zap.Any("stats", latestConnStatus))
for k, subs := range w.connStatusSubscriptions {
if !subs.Send(latestConnStatus) {
delete(w.connStatusSubscriptions, k)
}
}

w.connStatusMu.Unlock()

if w.onPeerStats != nil {
w.onPeerStats(latestConnStatus)
}

if w.statusTelemetryClient != nil {
w.statusTelemetryClient.PushPeerCount(w.PeerCount())
}

//TODO: analyze if we need to discover and connect to peers with peerExchange loop enabled.
if !w.onlineChecker.IsOnline() && isOnline {
if err := w.discoverAndConnectPeers(); err != nil {
w.logger.Error("failed to add wakuv2 peers", zap.Error(err))
}
}

w.ConnectionChanged(connection.State{
Offline: !latestConnStatus.IsOnline,
})
w.checkForConnectionChanges()
case <-ticker.C:
w.checkForConnectionChanges()
}
}
}()
Expand Down Expand Up @@ -1458,6 +1408,62 @@ func (w *Waku) Start() error {
return nil
}

func (w *Waku) checkForConnectionChanges() {

isOnline := len(w.node.Host().Network().Peers()) > 0

if w.cfg.LightClient {
// TODO: Temporary changes for lightNodes to have health check based on connected peers.
//This needs to be enhanced to be based on healthy Filter and lightPush peers available for each shard.
//This would get fixed as part of https://github.com/waku-org/go-waku/issues/1114

subs := w.node.FilterLightnode().Subscriptions()
w.logger.Debug("filter subs count", zap.Int("count", len(subs)))

//TODO: needs fixing, right now invoking everytime.
//Trigger FilterManager to take care of any pending filter subscriptions
//TODO: Pass pubsubTopic based on topicHealth notif received.
go w.filterManager.onConnectionStatusChange("", isOnline)

}
w.connStatusMu.Lock()

latestConnStatus := types.ConnStatus{
IsOnline: isOnline,
Peers: FormatPeerStats(w.node),
}

w.logger.Debug("peer stats",
zap.Int("peersCount", len(latestConnStatus.Peers)),
zap.Any("stats", latestConnStatus))
for k, subs := range w.connStatusSubscriptions {
if !subs.Send(latestConnStatus) {
delete(w.connStatusSubscriptions, k)
}
}

w.connStatusMu.Unlock()

if w.onPeerStats != nil {
w.onPeerStats(latestConnStatus)
}

if w.statusTelemetryClient != nil {
w.statusTelemetryClient.PushPeerCount(w.PeerCount())
}

//TODO: analyze if we need to discover and connect to peers with peerExchange loop enabled.
if !w.onlineChecker.IsOnline() && isOnline {
if err := w.discoverAndConnectPeers(); err != nil {
w.logger.Error("failed to add wakuv2 peers", zap.Error(err))
}
}

w.ConnectionChanged(connection.State{
Offline: !latestConnStatus.IsOnline,
})
}

func (w *Waku) setupRelaySubscriptions() error {
if w.cfg.LightClient {
return nil
Expand Down