From 4aa2199d5b4a2e86fa33f87c1a4f3c4da44ac079 Mon Sep 17 00:00:00 2001 From: MikolajTwarog <43782609+MikolajTwarog@users.noreply.github.com> Date: Mon, 20 Apr 2020 08:23:38 +0200 Subject: [PATCH] lib/connections: Accept new connections in place of old ones (fixes #5224) (#6548) --- lib/connections/service.go | 7 ++++--- lib/protocol/protocol.go | 10 +++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/connections/service.go b/lib/connections/service.go index 1120311b8..e24cdbdb3 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -46,8 +46,9 @@ var ( ) const ( - perDeviceWarningIntv = 15 * time.Minute - tlsHandshakeTimeout = 10 * time.Second + perDeviceWarningIntv = 15 * time.Minute + tlsHandshakeTimeout = 10 * time.Second + minConnectionReplaceAge = 10 * time.Second ) // From go/src/crypto/tls/cipher_suites.go @@ -276,7 +277,7 @@ func (s *service) handle(ctx context.Context) { ct, connected := s.model.Connection(remoteID) // Lower priority is better, just like nice etc. - if connected && ct.Priority() > c.priority { + if connected && (ct.Priority() > c.priority || time.Since(ct.Statistics().StartedAt) > minConnectionReplaceAge) { l.Debugf("Switching connections %s (existing: %s new: %s)", remoteID, ct, c) } else if connected { // We should not already be connected to the other party. TODO: This diff --git a/lib/protocol/protocol.go b/lib/protocol/protocol.go index 59b185812..f59695a25 100644 --- a/lib/protocol/protocol.go +++ b/lib/protocol/protocol.go @@ -145,9 +145,10 @@ type Connection interface { } type rawConnection struct { - id DeviceID - name string - receiver Model + id DeviceID + name string + receiver Model + startTime time.Time cr *countingReader cw *countingWriter @@ -236,6 +237,7 @@ func (c *rawConnection) Start() { go c.writerLoop() go c.pingSender() go c.pingReceiver() + c.startTime = time.Now() } func (c *rawConnection) ID() DeviceID { @@ -958,6 +960,7 @@ type Statistics struct { At time.Time InBytesTotal int64 OutBytesTotal int64 + StartedAt time.Time } func (c *rawConnection) Statistics() Statistics { @@ -965,6 +968,7 @@ func (c *rawConnection) Statistics() Statistics { At: time.Now(), InBytesTotal: c.cr.Tot(), OutBytesTotal: c.cw.Tot(), + StartedAt: c.startTime, } }