From a4f052ad31fa160ad376598a920a5938699f58b9 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Sun, 7 Aug 2016 12:20:37 +0000 Subject: [PATCH] lib/connections: Fix connection switching It seems that it would be impossible to drop down to relay after establishing a direct connection Also, we should not drop the existing connection until after we've passed the validation steps, and it seems it's being dropped in two places unnecesserily at the moment. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3480 --- lib/connections/service.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/connections/service.go b/lib/connections/service.go index 0cfa6137..06dea0d5 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -193,15 +193,17 @@ next: // If we have a relay connection, and the new incoming connection is // not a relay connection, we should drop that, and prefer the this one. + connected := s.model.ConnectedTo(remoteID) s.curConMut.Lock() ct, ok := s.currentConnection[remoteID] s.curConMut.Unlock() + priorityKnown := ok && connected // Lower priority is better, just like nice etc. - if ok && ct.Priority > c.Priority { + if priorityKnown && ct.Priority > c.Priority { l.Debugln("Switching connections", remoteID) s.model.Close(remoteID, protocol.ErrSwitchingConnections) - } else if s.model.ConnectedTo(remoteID) { + } else if connected { // We should not already be connected to the other party. TODO: This // could use some better handling. If the old connection is dead but // hasn't timed out yet we may want to drop *that* connection and keep @@ -306,10 +308,11 @@ func (s *Service) connect() { connected := s.model.ConnectedTo(deviceID) s.curConMut.Lock() - ct := s.currentConnection[deviceID] + ct, ok := s.currentConnection[deviceID] s.curConMut.Unlock() + priorityKnown := ok && connected - if connected && ct.Priority == bestDialerPrio { + if priorityKnown && ct.Priority == bestDialerPrio { // Things are already as good as they can get. continue } @@ -357,7 +360,7 @@ func (s *Service) connect() { continue } - if connected && dialerFactory.Priority() >= ct.Priority { + if priorityKnown && dialerFactory.Priority() >= ct.Priority { l.Debugf("Not dialing using %s as priority is less than current connection (%d >= %d)", dialerFactory, dialerFactory.Priority(), ct.Priority) continue } @@ -372,10 +375,6 @@ func (s *Service) connect() { continue } - if connected { - s.model.Close(deviceID, protocol.ErrSwitchingConnections) - } - s.conns <- conn continue nextDevice }