mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 18:39:58 +00:00
TUN-5138: Switch to QUIC on auto protocol based on threshold
This commit is contained in:

committed by
Nuno Diegues

parent
5a3c0fdffa
commit
ceb509ee98
@@ -1,45 +1,50 @@
|
||||
package edgediscovery
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
protocolRecord = "protocol.argotunnel.com"
|
||||
protocolRecord = "protocol-v2.argotunnel.com"
|
||||
)
|
||||
|
||||
var (
|
||||
errNoProtocolRecord = fmt.Errorf("No TXT record found for %s to determine connection protocol", protocolRecord)
|
||||
)
|
||||
|
||||
func HTTP2Percentage() (int32, error) {
|
||||
// ProtocolPercent represents a single Protocol Percentage combination.
|
||||
type ProtocolPercent struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Percentage int32 `json:"percentage"`
|
||||
}
|
||||
|
||||
// ProtocolPercents represents the preferred distribution ratio of protocols when protocol isn't specified.
|
||||
type ProtocolPercents []ProtocolPercent
|
||||
|
||||
// GetPercentage returns the threshold percentage of a single protocol.
|
||||
func (p ProtocolPercents) GetPercentage(protocol string) int32 {
|
||||
for _, protocolPercent := range p {
|
||||
if strings.ToLower(protocolPercent.Protocol) == strings.ToLower(protocol) {
|
||||
return protocolPercent.Percentage
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ProtocolPercentage returns the ratio of protocols and a specification ratio for their selection.
|
||||
func ProtocolPercentage() (ProtocolPercents, error) {
|
||||
records, err := net.LookupTXT(protocolRecord)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return 0, errNoProtocolRecord
|
||||
return nil, errNoProtocolRecord
|
||||
}
|
||||
return parseHTTP2Precentage(records[0])
|
||||
}
|
||||
|
||||
// The record looks like http2=percentage
|
||||
func parseHTTP2Precentage(record string) (int32, error) {
|
||||
const key = "http2"
|
||||
slices := strings.Split(record, "=")
|
||||
if len(slices) != 2 {
|
||||
return 0, fmt.Errorf("Malformed TXT record %s, expect http2=percentage", record)
|
||||
}
|
||||
if slices[0] != key {
|
||||
return 0, fmt.Errorf("Incorrect key %s, expect %s", slices[0], key)
|
||||
}
|
||||
percentage, err := strconv.ParseInt(slices[1], 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(percentage), nil
|
||||
|
||||
var protocolsWithPercent ProtocolPercents
|
||||
err = json.Unmarshal([]byte(records[0]), &protocolsWithPercent)
|
||||
return protocolsWithPercent, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user