From 8480429cbb56ea728bd0be51cdc6d4682a4f168e Mon Sep 17 00:00:00 2001
From: Pablo Lopez
Date: Wed, 26 Jun 2024 16:03:44 +0300
Subject: [PATCH] fix_: force process online/offline state hanlding after
computer back from sleep (#5422)
* fix_: force process online/offline state hanlding after computer back from sleep
* fix_: naming variables
---
protocol/messenger.go | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/protocol/messenger.go b/protocol/messenger.go
index a764470ee..478f96113 100644
--- a/protocol/messenger.go
+++ b/protocol/messenger.go
@@ -1459,13 +1459,20 @@ func (m *Messenger) handleENSVerificationSubscription(c chan []*ens.Verification
// watchConnectionChange checks the connection status and call handleConnectionChange when this changes
func (m *Messenger) watchConnectionChange() {
state := m.Online()
+ // lastCheck, sleepDetention and keepAlive helps us recognizing when computer was offline because of sleep, lid closed, etc.
+ lastCheck := time.Now().Unix()
+ sleepDetentionInSecs := int64(20)
+ keepAlivePeriod := 15 * time.Second // must be lower than sleepDetentionInSecs
processNewState := func(newState bool) {
- if state == newState {
+ now := time.Now().Unix()
+ force := now-lastCheck > sleepDetentionInSecs
+ lastCheck = now
+ if !force && state == newState {
return
}
state = newState
- m.logger.Debug("connection changed", zap.Bool("online", state))
+ m.logger.Debug("connection changed", zap.Bool("online", state), zap.Bool("force", force))
m.handleConnectionChange(state)
}
@@ -1484,10 +1491,14 @@ func (m *Messenger) watchConnectionChange() {
subscribedConnectionStatus := func(subscription *types.ConnStatusSubscription) {
defer subscription.Unsubscribe()
+ ticker := time.NewTicker(keepAlivePeriod)
+ defer ticker.Stop()
for {
select {
case status := <-subscription.C:
processNewState(status.IsOnline)
+ case <-ticker.C:
+ processNewState(m.Online())
case <-m.quit:
return
}