lib/connections: Don't look at devices that are already optimally connected
Just an optimization. Required exposing the priority from the factory, so made that an interface with an extra method instead of just a func type. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3071
This commit is contained in:
committed by
Audrius Butkevicius
parent
31f64186ae
commit
d77d8ff803
@@ -21,7 +21,7 @@ import (
|
|||||||
const relayPriority = 200
|
const relayPriority = 200
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dialers["relay"] = newRelayDialer
|
dialers["relay"] = relayDialerFactory{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type relayDialer struct {
|
type relayDialer struct {
|
||||||
@@ -74,9 +74,15 @@ func (d *relayDialer) String() string {
|
|||||||
return "Relay Dialer"
|
return "Relay Dialer"
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRelayDialer(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
type relayDialerFactory struct{}
|
||||||
|
|
||||||
|
func (relayDialerFactory) New(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
||||||
return &relayDialer{
|
return &relayDialer{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
tlsCfg: tlsCfg,
|
tlsCfg: tlsCfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (relayDialerFactory) Priority() int {
|
||||||
|
return relayPriority
|
||||||
|
}
|
||||||
|
|||||||
@@ -240,6 +240,13 @@ func (s *Service) connect() {
|
|||||||
delay := time.Second
|
delay := time.Second
|
||||||
sleep := time.Second
|
sleep := time.Second
|
||||||
|
|
||||||
|
bestDialerPrio := 1<<31 - 1 // worse prio won't build on 32 bit
|
||||||
|
for _, df := range dialers {
|
||||||
|
if prio := df.Priority(); prio < bestDialerPrio {
|
||||||
|
bestDialerPrio = prio
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
l.Debugln("Reconnect loop")
|
l.Debugln("Reconnect loop")
|
||||||
|
|
||||||
@@ -257,13 +264,18 @@ func (s *Service) connect() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Debugln("Reconnect loop for", deviceID)
|
|
||||||
|
|
||||||
connected := s.model.ConnectedTo(deviceID)
|
connected := s.model.ConnectedTo(deviceID)
|
||||||
s.curConMut.Lock()
|
s.curConMut.Lock()
|
||||||
ct := s.currentConnection[deviceID]
|
ct := s.currentConnection[deviceID]
|
||||||
s.curConMut.Unlock()
|
s.curConMut.Unlock()
|
||||||
|
|
||||||
|
if connected && ct.Priority == bestDialerPrio {
|
||||||
|
// Things are already as good as they can get.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Debugln("Reconnect loop for", deviceID)
|
||||||
|
|
||||||
var addrs []string
|
var addrs []string
|
||||||
for _, addr := range deviceCfg.Addresses {
|
for _, addr := range deviceCfg.Addresses {
|
||||||
if addr == "dynamic" {
|
if addr == "dynamic" {
|
||||||
@@ -292,7 +304,7 @@ func (s *Service) connect() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
dialer := dialerFactory(s.cfg, s.tlsCfg)
|
dialer := dialerFactory.New(s.cfg, s.tlsCfg)
|
||||||
|
|
||||||
nextDialAt, ok := nextDial[uri.String()]
|
nextDialAt, ok := nextDial[uri.String()]
|
||||||
// See below for comments on this delay >= sleep check
|
// See below for comments on this delay >= sleep check
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ type Connection struct {
|
|||||||
protocol.Connection
|
protocol.Connection
|
||||||
}
|
}
|
||||||
|
|
||||||
type dialerFactory func(*config.Wrapper, *tls.Config) genericDialer
|
type dialerFactory interface {
|
||||||
|
New(*config.Wrapper, *tls.Config) genericDialer
|
||||||
|
Priority() int
|
||||||
|
}
|
||||||
|
|
||||||
type genericDialer interface {
|
type genericDialer interface {
|
||||||
Dial(protocol.DeviceID, *url.URL) (IntermediateConnection, error)
|
Dial(protocol.DeviceID, *url.URL) (IntermediateConnection, error)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const tcpPriority = 10
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
|
for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
|
||||||
dialers[scheme] = newTCPDialer
|
dialers[scheme] = tcpDialerFactory{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,9 +67,15 @@ func (d *tcpDialer) String() string {
|
|||||||
return "TCP Dialer"
|
return "TCP Dialer"
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTCPDialer(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
type tcpDialerFactory struct{}
|
||||||
|
|
||||||
|
func (tcpDialerFactory) New(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
||||||
return &tcpDialer{
|
return &tcpDialer{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
tlsCfg: tlsCfg,
|
tlsCfg: tlsCfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tcpDialerFactory) Priority() int {
|
||||||
|
return tcpPriority
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user