lib/connections: Add KCP support (fixes #804)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3489
This commit is contained in:
Audrius Butkevicius
2017-03-07 12:44:16 +00:00
committed by Jakob Borg
parent 151004d645
commit 0da0774ce4
181 changed files with 30946 additions and 106 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/xtaci/kcp-go"
)
func BenchmarkRequestsRawTCP(b *testing.B) {
@@ -28,9 +29,46 @@ func BenchmarkRequestsRawTCP(b *testing.B) {
benchmarkRequestsConnPair(b, conn0, conn1)
}
func BenchmarkRequestsTLSoTCP(b *testing.B) {
func BenchmarkRequestsRawKCP(b *testing.B) {
// Benchmarks the rate at which we can serve requests over a single,
// TLS encrypted TCP channel over the loopback interface.
// unencrypted KCP channel over the loopback interface.
// Get a connected KCP pair
conn0, conn1, err := getKCPConnectionPair()
if err != nil {
b.Fatal(err)
}
defer conn0.Close()
defer conn1.Close()
// Bench it
benchmarkRequestsConnPair(b, conn0, conn1)
}
func BenchmarkRequestsTLSoTCP(b *testing.B) {
conn0, conn1, err := getTCPConnectionPair()
if err != nil {
b.Fatal(err)
}
defer conn0.Close()
defer conn1.Close()
benchmarkRequestsTLS(b, conn0, conn1)
}
func BenchmarkRequestsTLSoKCP(b *testing.B) {
conn0, conn1, err := getKCPConnectionPair()
if err != nil {
b.Fatal(err)
}
defer conn0.Close()
defer conn1.Close()
benchmarkRequestsTLS(b, conn0, conn1)
}
func benchmarkRequestsTLS(b *testing.B, conn0, conn1 net.Conn) {
// Benchmarks the rate at which we can serve requests over a single,
// TLS encrypted channel over the loopback interface.
// Load a certificate, skipping this benchmark if it doesn't exist
cert, err := tls.LoadX509KeyPair("../../test/h1/cert.pem", "../../test/h1/key.pem")
@@ -39,18 +77,9 @@ func BenchmarkRequestsTLSoTCP(b *testing.B) {
return
}
// Get a connected TCP pair
conn0, conn1, err := getTCPConnectionPair()
if err != nil {
b.Fatal(err)
}
/// TLSify them
conn0, conn1 = negotiateTLS(cert, conn0, conn1)
defer conn0.Close()
defer conn1.Close()
// Bench it
benchmarkRequestsConnPair(b, conn0, conn1)
}
@@ -137,6 +166,35 @@ func getTCPConnectionPair() (net.Conn, net.Conn, error) {
return conn0, conn1, nil
}
func getKCPConnectionPair() (net.Conn, net.Conn, error) {
lst, err := kcp.Listen("127.0.0.1:0")
if err != nil {
return nil, nil, err
}
var conn0 net.Conn
var err0 error
done := make(chan struct{})
go func() {
conn0, err0 = lst.Accept()
close(done)
}()
// Dial the connection
conn1, err := kcp.Dial(lst.Addr().String())
if err != nil {
return nil, nil, err
}
// Check any error from accept
<-done
if err0 != nil {
return nil, nil, err0
}
return conn0, conn1, nil
}
func negotiateTLS(cert tls.Certificate, conn0, conn1 net.Conn) (net.Conn, net.Conn) {
cfg := &tls.Config{
Certificates: []tls.Certificate{cert},