mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-03 15:17:25 +00:00
lib/connections: Try not to deadlock (fixes #2987)
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/2991
This commit is contained in:
parent
893cc025f9
commit
ea54525a33
@ -65,11 +65,8 @@ type Service struct {
|
|||||||
writeRateLimit *ratelimit.Bucket
|
writeRateLimit *ratelimit.Bucket
|
||||||
readRateLimit *ratelimit.Bucket
|
readRateLimit *ratelimit.Bucket
|
||||||
|
|
||||||
lastRelayCheck map[protocol.DeviceID]time.Time
|
mut sync.RWMutex
|
||||||
|
connType map[protocol.DeviceID]model.ConnectionType
|
||||||
mut sync.RWMutex
|
|
||||||
connType map[protocol.DeviceID]model.ConnectionType
|
|
||||||
relaysEnabled bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnectionService(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, mappings []*nat.Mapping,
|
func NewConnectionService(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, mappings []*nat.Mapping,
|
||||||
@ -88,9 +85,7 @@ func NewConnectionService(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model
|
|||||||
tlsDefaultCommonName: tlsDefaultCommonName,
|
tlsDefaultCommonName: tlsDefaultCommonName,
|
||||||
lans: lans,
|
lans: lans,
|
||||||
|
|
||||||
connType: make(map[protocol.DeviceID]model.ConnectionType),
|
connType: make(map[protocol.DeviceID]model.ConnectionType),
|
||||||
relaysEnabled: cfg.Options().RelaysEnabled,
|
|
||||||
lastRelayCheck: make(map[protocol.DeviceID]time.Time),
|
|
||||||
}
|
}
|
||||||
cfg.Subscribe(service)
|
cfg.Subscribe(service)
|
||||||
|
|
||||||
@ -284,9 +279,12 @@ next:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) connect() {
|
func (s *Service) connect() {
|
||||||
|
lastRelayCheck := make(map[protocol.DeviceID]time.Time)
|
||||||
|
|
||||||
delay := time.Second
|
delay := time.Second
|
||||||
for {
|
for {
|
||||||
l.Debugln("Reconnect loop")
|
l.Debugln("Reconnect loop")
|
||||||
|
relaysEnabled := s.cfg.Options().RelaysEnabled
|
||||||
nextDevice:
|
nextDevice:
|
||||||
for deviceID, deviceCfg := range s.cfg.Devices() {
|
for deviceID, deviceCfg := range s.cfg.Devices() {
|
||||||
if deviceID == s.myID {
|
if deviceID == s.myID {
|
||||||
@ -299,7 +297,6 @@ func (s *Service) connect() {
|
|||||||
paused := s.model.IsPaused(deviceID)
|
paused := s.model.IsPaused(deviceID)
|
||||||
connected := s.model.ConnectedTo(deviceID)
|
connected := s.model.ConnectedTo(deviceID)
|
||||||
ct, ok := s.connType[deviceID]
|
ct, ok := s.connType[deviceID]
|
||||||
relaysEnabled := s.relaysEnabled
|
|
||||||
s.mut.RUnlock()
|
s.mut.RUnlock()
|
||||||
|
|
||||||
if paused {
|
if paused {
|
||||||
@ -338,14 +335,14 @@ func (s *Service) connect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reconIntv := time.Duration(s.cfg.Options().RelayReconnectIntervalM) * time.Minute
|
reconIntv := time.Duration(s.cfg.Options().RelayReconnectIntervalM) * time.Minute
|
||||||
if last, ok := s.lastRelayCheck[deviceID]; ok && time.Since(last) < reconIntv {
|
if last, ok := lastRelayCheck[deviceID]; ok && time.Since(last) < reconIntv {
|
||||||
l.Debugln("Skipping connecting via relay to", deviceID, "last checked at", last)
|
l.Debugln("Skipping connecting via relay to", deviceID, "last checked at", last)
|
||||||
continue nextDevice
|
continue nextDevice
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Debugln("Trying relay connections to", deviceID, relays)
|
l.Debugln("Trying relay connections to", deviceID, relays)
|
||||||
|
|
||||||
s.lastRelayCheck[deviceID] = time.Now()
|
lastRelayCheck[deviceID] = time.Now()
|
||||||
|
|
||||||
for _, addr := range relays {
|
for _, addr := range relays {
|
||||||
if conn := s.connectViaRelay(deviceID, addr); conn != nil {
|
if conn := s.connectViaRelay(deviceID, addr); conn != nil {
|
||||||
@ -478,10 +475,6 @@ func (s *Service) VerifyConfiguration(from, to config.Configuration) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
|
func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
|
||||||
s.mut.Lock()
|
|
||||||
s.relaysEnabled = to.Options.RelaysEnabled
|
|
||||||
s.mut.Unlock()
|
|
||||||
|
|
||||||
// We require a restart if a device as been removed.
|
// We require a restart if a device as been removed.
|
||||||
|
|
||||||
newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
|
newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
|
||||||
|
@ -1042,13 +1042,6 @@ func (m *Model) AddConnection(conn Connection, hello protocol.HelloMessage) {
|
|||||||
|
|
||||||
l.Infof(`Device %s client is "%s %s" named "%s"`, deviceID, hello.ClientName, hello.ClientVersion, hello.DeviceName)
|
l.Infof(`Device %s client is "%s %s" named "%s"`, deviceID, hello.ClientName, hello.ClientVersion, hello.DeviceName)
|
||||||
|
|
||||||
device, ok := m.cfg.Devices()[deviceID]
|
|
||||||
if ok && (device.Name == "" || m.cfg.Options().OverwriteNames) {
|
|
||||||
device.Name = hello.DeviceName
|
|
||||||
m.cfg.SetDevice(device)
|
|
||||||
m.cfg.Save()
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.Start()
|
conn.Start()
|
||||||
|
|
||||||
cm := m.generateClusterConfig(deviceID)
|
cm := m.generateClusterConfig(deviceID)
|
||||||
@ -1062,6 +1055,13 @@ func (m *Model) AddConnection(conn Connection, hello protocol.HelloMessage) {
|
|||||||
m.fmut.RUnlock()
|
m.fmut.RUnlock()
|
||||||
m.pmut.Unlock()
|
m.pmut.Unlock()
|
||||||
|
|
||||||
|
device, ok := m.cfg.Devices()[deviceID]
|
||||||
|
if ok && (device.Name == "" || m.cfg.Options().OverwriteNames) {
|
||||||
|
device.Name = hello.DeviceName
|
||||||
|
m.cfg.SetDevice(device)
|
||||||
|
m.cfg.Save()
|
||||||
|
}
|
||||||
|
|
||||||
m.deviceWasSeen(deviceID)
|
m.deviceWasSeen(deviceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user