fix(mailservers)_: Do not add backoff if user action triggered store node disconnection (#5602)

fix(mailservers)_: ToggleUseMailservers should connect/disconnect from active store node

fixes status-im/status-desktop#15032
This commit is contained in:
Andrey Bocharnikov 2024-07-30 00:56:42 +07:00 committed by GitHub
parent 62f342c2e9
commit 5dcff278d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 12 deletions

View File

@ -98,7 +98,7 @@ func (m *Messenger) connectToNewMailserverAndWait() error {
}
// If pinned mailserver is not nil, no need to disconnect and wait for it to be available
if pinnedMailserver == nil {
m.disconnectActiveMailserver()
m.disconnectActiveMailserver(graylistBackoff)
}
return m.findNewMailserver()
@ -1091,12 +1091,11 @@ func (m *Messenger) ToggleUseMailservers(value bool) error {
return err
}
m.disconnectActiveMailserver(backoffByUserAction)
if value {
m.cycleMailservers()
return nil
}
m.disconnectActiveMailserver()
return nil
}
@ -1106,6 +1105,7 @@ func (m *Messenger) SetPinnedMailservers(mailservers map[string]string) error {
return err
}
m.disconnectActiveMailserver(backoffByUserAction)
m.cycleMailservers()
return nil
}

View File

@ -27,6 +27,7 @@ import (
const defaultBackoff = 10 * time.Second
const graylistBackoff = 3 * time.Minute
const backoffByUserAction = 0
const isAndroidEmulator = runtime.GOOS == "android" && runtime.GOARCH == "amd64"
const findNearestMailServer = !isAndroidEmulator
const overrideDNS = runtime.GOOS == "android" || runtime.GOOS == "ios"
@ -99,10 +100,10 @@ func (m *Messenger) StartMailserverCycle(mailservers []mailservers.Mailserver) e
func (m *Messenger) DisconnectActiveMailserver() {
m.mailserverCycle.Lock()
defer m.mailserverCycle.Unlock()
m.disconnectActiveMailserver()
m.disconnectActiveMailserver(graylistBackoff)
}
func (m *Messenger) disconnectMailserver() error {
func (m *Messenger) disconnectMailserver(backoffDuration time.Duration) error {
if m.mailserverCycle.activeMailserver == nil {
m.logger.Info("no active mailserver")
return nil
@ -112,13 +113,14 @@ func (m *Messenger) disconnectMailserver() error {
pInfo, ok := m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID]
if ok {
pInfo.status = disconnected
pInfo.canConnectAfter = time.Now().Add(graylistBackoff)
pInfo.canConnectAfter = time.Now().Add(backoffDuration)
m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID] = pInfo
} else {
m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID] = peerStatus{
status: disconnected,
mailserver: *m.mailserverCycle.activeMailserver,
canConnectAfter: time.Now().Add(graylistBackoff),
canConnectAfter: time.Now().Add(backoffDuration),
}
}
m.mailPeersMutex.Unlock()
@ -137,8 +139,8 @@ func (m *Messenger) disconnectMailserver() error {
return nil
}
func (m *Messenger) disconnectActiveMailserver() {
err := m.disconnectMailserver()
func (m *Messenger) disconnectActiveMailserver(backoffDuration time.Duration) {
err := m.disconnectMailserver(backoffDuration)
if err != nil {
m.logger.Error("failed to disconnect mailserver", zap.Error(err))
}
@ -149,10 +151,21 @@ func (m *Messenger) cycleMailservers() {
m.logger.Info("Automatically switching mailserver")
if m.mailserverCycle.activeMailserver != nil {
m.disconnectActiveMailserver()
m.disconnectActiveMailserver(graylistBackoff)
}
err := m.findNewMailserver()
useMailserver, err := m.settings.CanUseMailservers()
if err != nil {
m.logger.Error("failed to get use mailservers", zap.Error(err))
return
}
if !useMailserver {
m.logger.Info("Skipping mailserver search due to useMailserver being false")
return
}
err = m.findNewMailserver()
if err != nil {
m.logger.Error("Error getting new mailserver", zap.Error(err))
}
@ -608,7 +621,7 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e
signal.SendMailserverNotWorking()
m.penalizeMailserver(m.mailserverCycle.activeMailserver.ID)
m.disconnectActiveMailserver()
m.disconnectActiveMailserver(graylistBackoff)
}
}

View File

@ -342,3 +342,15 @@ func (s *MessengerStoreNodeCommunitySuite) TestSetStorenodeForCommunity_fetchMes
})
s.Require().NoError(err)
}
func (s *MessengerStoreNodeCommunitySuite) TestToggleUseMailservers() {
// Enable use of mailservers
err := s.owner.ToggleUseMailservers(true)
s.Require().NoError(err)
s.Require().NotNil(s.owner.mailserverCycle.activeMailserver)
// Disable use of mailservers
err = s.owner.ToggleUseMailservers(false)
s.Require().NoError(err)
s.Require().Nil(s.owner.mailserverCycle.activeMailserver)
}