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
This commit is contained in:
Pablo Lopez 2024-06-26 16:03:44 +03:00 committed by GitHub
parent 49eaabaca5
commit 8480429cbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 2 deletions

View File

@ -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
}