Repository: syncthing/syncthing
File: lib/connections/service.go (Go)

Code before:
			continue
		}
		if prio := df.Priority(); first || prio < bestDialerPrio {
			first = false
			bestDialerPrio = prio
		}
	}
	return bestDialerPrio
}

func (s *service) dialDevices(ctx context.Context, now time.Time, cfg config.Configuration, bestDialerPrio int, nextDial map[string]time.Time, initial bool) {
	for _, deviceCfg := range cfg.Devices {
		// Don't attempt to connect to ourselves...
		if deviceCfg.DeviceID == s.myID {
			continue
		}

		// Don't attempt to connect to paused devices...
		if deviceCfg.Paused {
			continue
		}

		// Don't connect to devices that are already connected using the
		// best possible dialer priority...
		connection, connected := s.model.Connection(deviceCfg.DeviceID)
		if connected && connection.Priority() == bestDialerPrio {
			continue
		}

		dialTargets := s.resolveDialTargets(ctx, now, cfg, deviceCfg, nextDial, initial)
		if conn, ok := s.dialParallel(ctx, deviceCfg.DeviceID, dialTargets); ok {
			s.conns <- conn
		}
	}
}

func (s *service) resolveDialTargets(ctx context.Context, now time.Time, cfg config.Configuration, deviceCfg config.DeviceConfiguration, nextDial map[string]time.Time, initial bool) []dialTarget {
	deviceID := deviceCfg.DeviceID
	connection, connected := s.model.Connection(deviceID)

	addrs := s.resolveDeviceAddrs(ctx, deviceCfg)
	l.Debugln("Resolved device", deviceID, "addresses:", addrs)

	dialTargets := make([]dialTarget, 0, len(addrs))
	for _, addr := range addrs {
		// Use a special key that is more than just the address, as you
		// might have two devices connected to the same relay
		nextDialKey := deviceID.String() + "/" + addr
		nextDialAt, ok := nextDial[nextDialKey]
		if ok && !initial && nextDialAt.After(now) {
			l.Debugf("Not dialing %s via %v as it's not time yet", deviceID, addr)

Review comment (suggestion):
Unrelated to this PR: This should be `>=`. If we e.g. listen on tcp but disabled dialing on tcp, we don't need to dial lower-prio methods when a tcp connection is already established.

Just saw that later on in the dialer loop we do use the `>=` condition between dialer prio and current connection prio.

Code after:
func (s *service) bestDialerPriority(cfg config.Configuration) int {
	bestDialerPriority := worstDialerPriority
	for _, df := range dialers {
		if df.Valid(cfg) != nil {
			continue
		}
		if prio := df.Priority(); prio < bestDialerPriority {
			bestDialerPriority = prio
		}
	}
	return bestDialerPriority
}

func (s *service) dialDevices(ctx context.Context, now time.Time, cfg config.Configuration, bestDialerPriority int, nextDialAt map[string]time.Time, initial bool) {
	for _, deviceCfg := range cfg.Devices {
		// Don't attempt to connect to ourselves...
		if deviceCfg.DeviceID == s.myID {
			continue
		}

		// Don't attempt to connect to paused devices...
		if deviceCfg.Paused {
			continue
		}

		// See if we are already connected and, if so, what our cutoff is
		// for dialer priority.
		priorityCutoff := worstDialerPriority
		connection, connected := s.model.Connection(deviceCfg.DeviceID)
		if connected {
			priorityCutoff = connection.Priority()
			if bestDialerPriority >= priorityCutoff {
				// Our best dialer is not any better than what we already
				// have, so nothing to do here.
				continue
			}
		}

		dialTargets := s.resolveDialTargets(ctx, now, cfg, deviceCfg, nextDialAt, initial, priorityCutoff)
		if conn, ok := s.dialParallel(ctx, deviceCfg.DeviceID, dialTargets); ok {
			s.conns <- conn
		}
	}
}

func (s *service) resolveDialTargets(ctx context.Context, now time.Time, cfg config.Configuration, deviceCfg config.DeviceConfiguration, nextDialAt map[string]time.Time, initial bool, priorityCutoff int) []dialTarget {
	deviceID := deviceCfg.DeviceID

	addrs := s.resolveDeviceAddrs(ctx, deviceCfg)
	l.Debugln("Resolved device", deviceID, "addresses:", addrs)