fix: only evaluate connected peers when active mailserver is set

This commit is contained in:
Richard Ramos 2023-01-18 20:11:24 -04:00 committed by RichΛrd
parent c2a8dd8a0c
commit f0cd807763
1 changed files with 39 additions and 30 deletions

View File

@ -479,43 +479,52 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e
m.mailserverCycle.peers[pID] = pInfo m.mailserverCycle.peers[pID] = pInfo
} }
m.mailPeersMutex.Unlock()
for _, connectedPeer := range connectedPeers { // Only evaluate connected peers once a mailserver has been set
id, err := m.mailserverAddressToID(connectedPeer.UniqueID) // otherwise
if err != nil { if m.mailserverCycle.activeMailserver != nil {
m.logger.Error("failed to convert id to hex", zap.Error(err)) for _, connectedPeer := range connectedPeers {
return err id, err := m.mailserverAddressToID(connectedPeer.UniqueID)
} if err != nil {
if id == "" { m.logger.Error("failed to convert id to hex", zap.Error(err))
continue return err
} }
pInfo, ok := m.mailserverCycle.peers[id] if id == "" {
continue
if !ok || pInfo.status != connected {
m.logger.Info("peer connected", zap.String("peer", connectedPeer.UniqueID))
pInfo.status = connected
if pInfo.canConnectAfter.Before(time.Now()) {
pInfo.canConnectAfter = time.Now().Add(defaultBackoff)
} }
if m.mailserverCycle.activeMailserver != nil && id == m.mailserverCycle.activeMailserver.ID { m.mailPeersMutex.Lock()
m.mailserverCycle.activeMailserver.FailedRequests = 0 pInfo, ok := m.mailserverCycle.peers[id]
m.logger.Info("mailserver available", zap.String("address", connectedPeer.UniqueID)) if !ok || pInfo.status != connected {
m.EmitMailserverAvailable() m.logger.Info("peer connected", zap.String("peer", connectedPeer.UniqueID))
signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID) pInfo.status = connected
} if pInfo.canConnectAfter.Before(time.Now()) {
// Query mailserver pInfo.canConnectAfter = time.Now().Add(defaultBackoff)
go func() {
_, err := m.performMailserverRequest(m.RequestAllHistoricMessages)
if err != nil {
m.logger.Error("could not perform mailserver request", zap.Error(err))
} }
}() m.mailserverCycle.peers[id] = pInfo
m.mailPeersMutex.Unlock()
m.mailserverCycle.peers[id] = pInfo if id == m.mailserverCycle.activeMailserver.ID {
m.mailserverCycle.activeMailserver.FailedRequests = 0
m.logger.Info("mailserver available", zap.String("address", connectedPeer.UniqueID))
m.EmitMailserverAvailable()
signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID)
}
// Query mailserver
go func() {
_, err := m.performMailserverRequest(m.RequestAllHistoricMessages)
if err != nil {
m.logger.Error("could not perform mailserver request", zap.Error(err))
}
}()
} else {
m.mailPeersMutex.Unlock()
}
} }
} }
m.mailPeersMutex.Unlock()
// Check whether we want to disconnect the mailserver // Check whether we want to disconnect the mailserver
if m.mailserverCycle.activeMailserver != nil { if m.mailserverCycle.activeMailserver != nil {
if m.mailserverCycle.activeMailserver.FailedRequests >= mailserverMaxFailedRequests { if m.mailserverCycle.activeMailserver.FailedRequests >= mailserverMaxFailedRequests {