mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
c6334e61aa
This adds the ability to have multiple concurrent connections to a single device. This is primarily useful when the network has multiple physical links for aggregated bandwidth. A single connection will never see a higher rate than a single link can give, but multiple connections are load-balanced over multiple links. It is also incidentally useful for older multi-core CPUs, where bandwidth could be limited by the TLS performance of a single CPU core -- using multiple connections achieves concurrency in the required crypto calculations... Co-authored-by: Simon Frei <freisim93@gmail.com> Co-authored-by: tomasz1986 <twilczynski@naver.com> Co-authored-by: bt90 <btom1990@googlemail.com>
98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
// Copyright (C) 2016 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package connections
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
"github.com/syncthing/syncthing/lib/connections/registry"
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
func init() {
|
|
factory := &tcpDialerFactory{}
|
|
for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
|
|
dialers[scheme] = factory
|
|
}
|
|
}
|
|
|
|
type tcpDialer struct {
|
|
commonDialer
|
|
registry *registry.Registry
|
|
}
|
|
|
|
func (d *tcpDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
|
|
uri = fixupPort(uri, config.DefaultTCPPort)
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
conn, err := dialer.DialContextReusePortFunc(d.registry)(timeoutCtx, uri.Scheme, uri.Host)
|
|
if err != nil {
|
|
return internalConn{}, err
|
|
}
|
|
|
|
err = dialer.SetTCPOptions(conn)
|
|
if err != nil {
|
|
l.Debugln("Dial (BEP/tcp): setting tcp options:", err)
|
|
}
|
|
|
|
err = dialer.SetTrafficClass(conn, d.trafficClass)
|
|
if err != nil {
|
|
l.Debugln("Dial (BEP/tcp): setting traffic class:", err)
|
|
}
|
|
|
|
tc := tls.Client(conn, d.tlsCfg)
|
|
err = tlsTimedHandshake(tc)
|
|
if err != nil {
|
|
tc.Close()
|
|
return internalConn{}, err
|
|
}
|
|
|
|
priority := d.wanPriority
|
|
isLocal := d.lanChecker.isLAN(conn.RemoteAddr())
|
|
if isLocal {
|
|
priority = d.lanPriority
|
|
}
|
|
|
|
return newInternalConn(tc, connTypeTCPClient, isLocal, priority), nil
|
|
}
|
|
|
|
type tcpDialerFactory struct{}
|
|
|
|
func (tcpDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config, registry *registry.Registry, lanChecker *lanChecker) genericDialer {
|
|
return &tcpDialer{
|
|
commonDialer: commonDialer{
|
|
trafficClass: opts.TrafficClass,
|
|
reconnectInterval: time.Duration(opts.ReconnectIntervalS) * time.Second,
|
|
tlsCfg: tlsCfg,
|
|
lanChecker: lanChecker,
|
|
lanPriority: opts.ConnectionPriorityTCPLAN,
|
|
wanPriority: opts.ConnectionPriorityTCPWAN,
|
|
allowsMultiConns: true,
|
|
},
|
|
registry: registry,
|
|
}
|
|
}
|
|
|
|
func (tcpDialerFactory) AlwaysWAN() bool {
|
|
return false
|
|
}
|
|
|
|
func (tcpDialerFactory) Valid(_ config.Configuration) error {
|
|
// Always valid
|
|
return nil
|
|
}
|
|
|
|
func (tcpDialerFactory) String() string {
|
|
return "TCP Dialer"
|
|
}
|