diff --git a/protocol/messenger_mailserver.go b/protocol/messenger_mailserver.go index 9de221065..c9e0a8eb6 100644 --- a/protocol/messenger_mailserver.go +++ b/protocol/messenger_mailserver.go @@ -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 } diff --git a/protocol/messenger_mailserver_cycle.go b/protocol/messenger_mailserver_cycle.go index fe8342ea5..630d228fe 100644 --- a/protocol/messenger_mailserver_cycle.go +++ b/protocol/messenger_mailserver_cycle.go @@ -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) } } diff --git a/protocol/messenger_storenode_comunity_test.go b/protocol/messenger_storenode_comunity_test.go index ad0e2f921..8216fdbde 100644 --- a/protocol/messenger_storenode_comunity_test.go +++ b/protocol/messenger_storenode_comunity_test.go @@ -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) +}