Upgrade quic-go to v0.36.1

quic-go has made breaking changes since v0.35.0, includes implementing
`CloseIdleConnections`.
Now, the local listener UDPConn are reused, and don't pile up. But,
1 instance (IPv4/IPv6) persists for each connected server.
This commit is contained in:
YX Hao 2023-07-03 18:37:42 +08:00
parent 16b2c84147
commit 89ccc59f0e
112 changed files with 5632 additions and 2620 deletions

View file

@ -57,7 +57,6 @@ type AltSupport struct {
type XTransport struct {
transport *http.Transport
h3Transport *http3.RoundTripper
h3UDPConn *net.UDPConn
keepAlive time.Duration
timeout time.Duration
cachedIPs CachedIPs
@ -138,15 +137,7 @@ func (xTransport *XTransport) loadCachedIP(host string) (ip net.IP, expired bool
func (xTransport *XTransport) rebuildTransport() {
dlog.Debug("Rebuilding transport")
if xTransport.transport != nil {
if xTransport.h3Transport != nil {
xTransport.h3Transport.Close()
if xTransport.h3UDPConn != nil {
xTransport.h3UDPConn.Close()
xTransport.h3UDPConn = nil
}
} else {
xTransport.transport.CloseIdleConnections()
}
xTransport.transport.CloseIdleConnections()
}
timeout := xTransport.timeout
transport := &http.Transport{
@ -272,27 +263,35 @@ func (xTransport *XTransport) rebuildTransport() {
host, port := ExtractHostAndPort(addrStr, stamps.DefaultPort)
ipOnly := host
cachedIP, _ := xTransport.loadCachedIP(host)
network := "udp4"
if cachedIP != nil {
if ipv4 := cachedIP.To4(); ipv4 != nil {
ipOnly = ipv4.String()
} else {
ipOnly = "[" + cachedIP.String() + "]"
network = "udp6"
}
} else {
dlog.Debugf("[%s] IP address was not cached in H3 DialContext", host)
dlog.Debugf("[%s] IP address was not cached in H3 context", host)
if xTransport.useIPv6 {
if xTransport.useIPv4 {
network = "udp"
} else {
network = "udp6"
}
}
}
addrStr = ipOnly + ":" + strconv.Itoa(port)
udpAddr, err := net.ResolveUDPAddr("udp", addrStr)
udpAddr, err := net.ResolveUDPAddr(network, addrStr)
if err != nil {
return nil, err
}
if xTransport.h3UDPConn == nil {
xTransport.h3UDPConn, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return nil, err
}
udpConn, err := net.ListenUDP(network, nil)
if err != nil {
return nil, err
}
return quic.DialEarlyContext(ctx, xTransport.h3UDPConn, udpAddr, host, tlsCfg, cfg)
tlsCfg.ServerName = host
return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg)
}}
xTransport.h3Transport = h3Transport
}
@ -545,13 +544,8 @@ func (xTransport *XTransport) Fetch(
err = errors.New(resp.Status)
}
} else {
if hasAltSupport {
dlog.Debugf("HTTP client error: [%v] - closing H3 connections", err)
xTransport.h3Transport.Close()
} else {
dlog.Debugf("HTTP client error: [%v] - closing idle connections", err)
xTransport.transport.CloseIdleConnections()
}
dlog.Debugf("HTTP client error: [%v] - closing idle connections", err)
xTransport.transport.CloseIdleConnections()
}
statusCode := 503
if resp != nil {