Skip to content

Commit 8429df8

Browse files
committed
Perform an initial benchmark to use servers with the lowest latency
(initially according to the certificate rtt)
1 parent 9dcd370 commit 8429df8

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

dnscrypt-proxy/certs.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ type CertInfo struct {
2121
CryptoConstruction CryptoConstruction
2222
}
2323

24-
func FetchCurrentCert(proxy *Proxy, proto string, pk ed25519.PublicKey, serverAddress string, providerName string) (CertInfo, error) {
24+
func FetchCurrentCert(proxy *Proxy, proto string, pk ed25519.PublicKey, serverAddress string, providerName string) (CertInfo, int, error) {
2525
if len(pk) != ed25519.PublicKeySize {
26-
return CertInfo{}, errors.New("Invalid public key length")
26+
return CertInfo{}, 0, errors.New("Invalid public key length")
2727
}
2828
if !strings.HasSuffix(providerName, ".") {
2929
providerName = providerName + "."
3030
}
3131
query := new(dns.Msg)
3232
query.SetQuestion(providerName, dns.TypeTXT)
3333
client := dns.Client{Net: proto, UDPSize: uint16(MaxDNSUDPPacketSize)}
34-
in, _, err := client.Exchange(query, serverAddress)
34+
in, rtt, err := client.Exchange(query, serverAddress)
3535
if err != nil {
36-
return CertInfo{}, err
36+
return CertInfo{}, 0, err
3737
}
3838
now := uint32(time.Now().Unix())
3939
certInfo := CertInfo{CryptoConstruction: UndefinedConstruction}
@@ -108,12 +108,12 @@ func FetchCurrentCert(proxy *Proxy, proto string, pk ed25519.PublicKey, serverAd
108108
certInfo.CryptoConstruction = cryptoConstruction
109109
copy(certInfo.ServerPk[:], serverPk[:])
110110
copy(certInfo.MagicQuery[:], binCert[104:112])
111-
dlog.Noticef("[%v] Valid cert (crypto version %d) found", providerName, cryptoConstruction)
111+
dlog.Noticef("[%v] Valid cert (crypto version %d) found - rtt: %dms", providerName, cryptoConstruction, rtt.Nanoseconds()/1000000)
112112
}
113113
if certInfo.CryptoConstruction == UndefinedConstruction {
114-
return certInfo, errors.New("No useable certificate found")
114+
return certInfo, 0, errors.New("No useable certificate found")
115115
}
116-
return certInfo, nil
116+
return certInfo, int(rtt.Nanoseconds() / 1000000), nil
117117
}
118118

119119
func isDigit(b byte) bool { return b >= '0' && b <= '9' }

dnscrypt-proxy/serversInfo.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type ServerInfo struct {
5353
TCPAddr *net.TCPAddr
5454
lastActionTS time.Time
5555
rtt ewma.MovingAverage
56+
initialRtt int
5657
}
5758

5859
type ServersInfo struct {
@@ -106,6 +107,21 @@ func (serversInfo *ServersInfo) refresh(proxy *Proxy) (int, error) {
106107
liveServers++
107108
}
108109
}
110+
serversInfo.Lock()
111+
inner := serversInfo.inner
112+
innerLen := len(inner)
113+
for i := 0; i < innerLen; i++ {
114+
for j := i + 1; j < innerLen; j++ {
115+
if inner[j].initialRtt < inner[i].initialRtt {
116+
inner[j], inner[i] = inner[i], inner[j]
117+
}
118+
}
119+
}
120+
serversInfo.inner = inner
121+
if innerLen > 1 {
122+
dlog.Noticef("Server with the lowest initial latency: %s (rtt: %dms)", inner[0].Name, inner[0].initialRtt)
123+
}
124+
serversInfo.Unlock()
109125
return liveServers, err
110126
}
111127

@@ -140,7 +156,7 @@ func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, stamp
140156
if err != nil || len(serverPk) != ed25519.PublicKeySize {
141157
dlog.Fatalf("Unsupported public key: [%v]", serverPk)
142158
}
143-
certInfo, err := FetchCurrentCert(proxy, proxy.mainProto, serverPk, stamp.serverAddrStr, stamp.providerName)
159+
certInfo, rtt, err := FetchCurrentCert(proxy, proxy.mainProto, serverPk, stamp.serverAddrStr, stamp.providerName)
144160
if err != nil {
145161
return ServerInfo{}, err
146162
}
@@ -161,6 +177,7 @@ func (serversInfo *ServersInfo) fetchServerInfo(proxy *Proxy, name string, stamp
161177
Timeout: proxy.timeout,
162178
UDPAddr: remoteUDPAddr,
163179
TCPAddr: remoteTCPAddr,
180+
initialRtt: rtt,
164181
}
165182
return serverInfo, nil
166183
}

0 commit comments

Comments
 (0)