fix: improve messenger offline detection (#4703)

This commit is contained in:
Igor Sirotin 2024-02-13 13:12:59 +00:00 committed by GitHub
parent 1ea2bd99d4
commit 5149976ce0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 12 deletions

View File

@ -1470,24 +1470,65 @@ func (m *Messenger) handleENSVerificationSubscription(c chan []*ens.Verification
// watchConnectionChange checks the connection status and call handleConnectionChange when this changes
func (m *Messenger) watchConnectionChange() {
m.logger.Debug("watching connection changes")
state := m.Online()
m.handleConnectionChange(state)
go func() {
for {
select {
case <-time.After(200 * time.Millisecond):
newState := m.Online()
if state != newState {
state := false
processNewState := func(newState bool) {
if state == newState {
return
}
state = newState
m.logger.Debug("connection changed", zap.Bool("online", state))
m.handleConnectionChange(state)
}
pollConnectionStatus := func() {
func() {
for {
select {
case <-time.After(200 * time.Millisecond):
processNewState(m.Online())
case <-m.quit:
return
}
}
}()
}
subscribedConnectionStatus := func(subscription *types.ConnStatusSubscription) {
defer subscription.Unsubscribe()
for {
select {
case status := <-subscription.C:
processNewState(status.IsOnline)
case <-m.quit:
return
}
}
}
m.logger.Debug("watching connection changes")
m.Online()
m.handleConnectionChange(state)
waku, err := m.node.GetWakuV2(nil)
if err != nil {
// No waku v2, we can't watch connection changes
// Instead we will poll the connection status.
m.logger.Warn("using WakuV1, can't watch connection changes, this might be have side-effects")
go pollConnectionStatus()
return
}
subscription, err := waku.SubscribeToConnStatusChanges()
if err != nil {
// Log error and fallback to polling
m.logger.Error("failed to subscribe to connection status changes", zap.Error(err))
go pollConnectionStatus()
return
}
go subscribedConnectionStatus(subscription)
}
// watchChatsAndCommunitiesToUnmute regularly checks for chats and communities that should be unmuted