fix: improve messenger offline detection (#4703)
This commit is contained in:
parent
1ea2bd99d4
commit
5149976ce0
|
@ -1470,24 +1470,65 @@ func (m *Messenger) handleENSVerificationSubscription(c chan []*ens.Verification
|
||||||
|
|
||||||
// watchConnectionChange checks the connection status and call handleConnectionChange when this changes
|
// watchConnectionChange checks the connection status and call handleConnectionChange when this changes
|
||||||
func (m *Messenger) watchConnectionChange() {
|
func (m *Messenger) watchConnectionChange() {
|
||||||
m.logger.Debug("watching connection changes")
|
state := false
|
||||||
state := m.Online()
|
|
||||||
m.handleConnectionChange(state)
|
processNewState := func(newState bool) {
|
||||||
go func() {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-time.After(200 * time.Millisecond):
|
case status := <-subscription.C:
|
||||||
newState := m.Online()
|
processNewState(status.IsOnline)
|
||||||
if state != newState {
|
|
||||||
state = newState
|
|
||||||
m.logger.Debug("connection changed", zap.Bool("online", state))
|
|
||||||
m.handleConnectionChange(state)
|
|
||||||
}
|
|
||||||
case <-m.quit:
|
case <-m.quit:
|
||||||
return
|
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
|
// watchChatsAndCommunitiesToUnmute regularly checks for chats and communities that should be unmuted
|
||||||
|
|
Loading…
Reference in New Issue