fix_: add ticker to check peers and update connection status (#5682)

This commit is contained in:
Prem Chaitanya Prathi 2024-08-13 13:58:18 +05:30 committed by GitHub
parent 9175e45e61
commit c08dedb77d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 38 deletions

View File

@ -1091,51 +1091,18 @@ func (w *Waku) Start() error {
w.wg.Add(1)
go func() {
defer w.wg.Done()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-w.ctx.Done():
return
case <-ticker.C:
w.checkForConnectionChanges()
case <-w.topicHealthStatusChan:
// TODO: https://github.com/status-im/status-go/issues/4628
case <-w.connectionNotifChan:
isOnline := len(w.node.Host().Network().Peers()) > 0
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 {
connFailures := FormatPeerConnFailures(w.node)
w.statusTelemetryClient.PushPeerCount(w.ctx, w.PeerCount())
w.statusTelemetryClient.PushPeerConnFailures(w.ctx, connFailures)
}
w.ConnectionChanged(connection.State{
Type: w.state.Type, //setting state type as previous one since there won't be a change here
Offline: !latestConnStatus.IsOnline,
})
w.checkForConnectionChanges()
}
}
}()
@ -1204,6 +1171,44 @@ func (w *Waku) Start() error {
return nil
}
func (w *Waku) checkForConnectionChanges() {
isOnline := len(w.node.Host().Network().Peers()) > 0
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 {
connFailures := FormatPeerConnFailures(w.node)
w.statusTelemetryClient.PushPeerCount(w.ctx, w.PeerCount())
w.statusTelemetryClient.PushPeerConnFailures(w.ctx, connFailures)
}
w.ConnectionChanged(connection.State{
Type: w.state.Type, //setting state type as previous one since there won't be a change here
Offline: !latestConnStatus.IsOnline,
})
}
func (w *Waku) confirmMessagesSent() {
w.messageSentCheck = publish.NewMessageSentCheck(w.ctx, w.node.Store(), w.node.Timesource(), w.logger)
go w.messageSentCheck.Start()