Query only once mailservers per cycle

This commit is contained in:
Andrea Maria Piana 2023-05-05 12:36:32 +01:00
parent 57b2432290
commit fe75054218
1 changed files with 9 additions and 8 deletions

View File

@ -391,12 +391,7 @@ func (m *Messenger) isActiveMailserverAvailable() bool {
return mailserverStatus == connected return mailserverStatus == connected
} }
func (m *Messenger) mailserverAddressToID(uniqueID string) (string, error) { func (m *Messenger) mailserverAddressToID(uniqueID string, allMailservers []mailservers.Mailserver) (string, error) {
allMailservers, err := m.allMailservers()
if err != nil {
return "", err
}
for _, ms := range allMailservers { for _, ms := range allMailservers {
if uniqueID == ms.UniqueID() { if uniqueID == ms.UniqueID() {
return ms.ID, nil return ms.ID, nil
@ -451,6 +446,12 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e
m.logger.Debug("peers info", zap.Any("peer-info", m.mailserverCycle.peers)) m.logger.Debug("peers info", zap.Any("peer-info", m.mailserverCycle.peers))
m.mailPeersMutex.Lock() m.mailPeersMutex.Lock()
allMailservers, err := m.allMailservers()
if err != nil {
return err
}
for pID, pInfo := range m.mailserverCycle.peers { for pID, pInfo := range m.mailserverCycle.peers {
if pInfo.status == disconnected { if pInfo.status == disconnected {
continue continue
@ -460,7 +461,7 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e
found := false found := false
for _, connectedPeer := range connectedPeers { for _, connectedPeer := range connectedPeers {
id, err := m.mailserverAddressToID(connectedPeer.UniqueID) id, err := m.mailserverAddressToID(connectedPeer.UniqueID, allMailservers)
if err != nil { if err != nil {
m.logger.Error("failed to convert id to hex", zap.Error(err)) m.logger.Error("failed to convert id to hex", zap.Error(err))
return err return err
@ -486,7 +487,7 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e
// not available error // not available error
if m.mailserverCycle.activeMailserver != nil { if m.mailserverCycle.activeMailserver != nil {
for _, connectedPeer := range connectedPeers { for _, connectedPeer := range connectedPeers {
id, err := m.mailserverAddressToID(connectedPeer.UniqueID) id, err := m.mailserverAddressToID(connectedPeer.UniqueID, allMailservers)
if err != nil { if err != nil {
m.logger.Error("failed to convert id to hex", zap.Error(err)) m.logger.Error("failed to convert id to hex", zap.Error(err))
return err return err