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
This commit is contained in:
Audrius Butkevicius 2016-08-07 12:20:37 +00:00 committed by Jakob Borg
parent ea87bcefd6
commit a4f052ad31
1 changed files with 8 additions and 9 deletions

View File

@ -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
}