1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2026-10-10 04:03:11 +08:00

chore: move magic close http2 connections function to own fork

This commit is contained in:
wwqgtxx
2026-05-08 17:26:30 +08:00
parent 028b480ae6
commit f333359c8a
4 changed files with 9 additions and 68 deletions
+6 -2
View File
@@ -10,12 +10,16 @@ type closeIdleTransport interface {
CloseIdleConnections()
}
type closeHttp2Connections interface {
CloseHttp2Connections()
}
func CloseTransport(roundTripper http.RoundTripper) {
if tr, ok := roundTripper.(closeIdleTransport); ok {
tr.CloseIdleConnections() // for *http.Transport
}
if tr, ok := roundTripper.(*http.Http2Transport); ok {
closeHttp2Transport(tr) // for *http2.Transport
if tr, ok := roundTripper.(closeHttp2Connections); ok {
tr.CloseHttp2Connections() // for *http.Transport in our own fork
}
if tr, ok := roundTripper.(io.Closer); ok {
_ = tr.Close() // for *http3.Transport
-63
View File
@@ -1,63 +0,0 @@
package httputils
import (
"net"
"sync"
"time"
"unsafe"
"github.com/metacubex/http"
)
type clientConnPool struct {
t *http.Http2Transport
mu sync.Mutex
conns map[string][]*http.Http2ClientConn // key is host:port
dialing map[string]unsafe.Pointer // currently in-flight dials
keys map[*http.Http2ClientConn][]string
addConnCalls map[string]unsafe.Pointer // in-flight addConnIfNeeded calls
}
type clientConn struct {
t *http.Transport
tconn net.Conn // usually *tls.Conn, except specialized impls
}
type efaceWords struct {
typ unsafe.Pointer
data unsafe.Pointer
}
type tlsConn interface {
net.Conn
NetConn() net.Conn
}
func closeClientConn(cc *http.Http2ClientConn) { // like forceCloseConn() in http.Http2ClientConn but also apply for tls-like conn
if conn, ok := (*clientConn)(unsafe.Pointer(cc)).tconn.(tlsConn); ok {
t := time.AfterFunc(time.Second, func() {
_ = conn.NetConn().Close()
})
defer t.Stop()
}
_ = cc.Close()
}
func closeHttp2Transport(tr *http.Http2Transport) {
connPool := transportConnPool(tr)
p := (*clientConnPool)((*efaceWords)(unsafe.Pointer(&connPool)).data)
p.mu.Lock()
defer p.mu.Unlock()
for _, vv := range p.conns {
for _, cc := range vv {
closeClientConn(cc)
}
}
// cleanup
p.conns = make(map[string][]*http.Http2ClientConn)
p.keys = make(map[*http.Http2ClientConn][]string)
}
//go:linkname transportConnPool github.com/metacubex/http.(*http2Transport).connPool
func transportConnPool(t *http.Http2Transport) http.Http2ClientConnPool
+1 -1
View File
@@ -21,7 +21,7 @@ require (
github.com/metacubex/edwards25519 v1.2.0
github.com/metacubex/fswatch v0.1.1
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759
github.com/metacubex/http v0.1.3
github.com/metacubex/http v0.1.4
github.com/metacubex/kcp-go v0.0.0-20260105040817-550693377604
github.com/metacubex/mhurl v0.1.0
github.com/metacubex/mlkem v0.1.0
+2 -2
View File
@@ -103,8 +103,8 @@ github.com/metacubex/hkdf v0.1.0 h1:fPA6VzXK8cU1foc/TOmGCDmSa7pZbxlnqhl3RNsthaA=
github.com/metacubex/hkdf v0.1.0/go.mod h1:3seEfds3smgTAXqUGn+tgEJH3uXdsUjOiduG/2EtvZ4=
github.com/metacubex/hpke v0.1.0 h1:gu2jUNhraehWi0P/z5HX2md3d7L1FhPQE6/Q0E9r9xQ=
github.com/metacubex/hpke v0.1.0/go.mod h1:vfDm6gfgrwlXUxKDkWbcE44hXtmc1uxLDm2BcR11b3U=
github.com/metacubex/http v0.1.3 h1:gTDIKAxObYYstqN7MmVts8Z/TVqRV0leo/jxlY9WI4M=
github.com/metacubex/http v0.1.3/go.mod h1:Nxx0zZAo2AhRfanyL+fmmK6ACMtVsfpwIl1aFAik2Eg=
github.com/metacubex/http v0.1.4 h1:gU1y8WlqslzxGmedW2ah74fGL6wJYU2HXUkCMJtCa2A=
github.com/metacubex/http v0.1.4/go.mod h1:Nxx0zZAo2AhRfanyL+fmmK6ACMtVsfpwIl1aFAik2Eg=
github.com/metacubex/kcp-go v0.0.0-20260105040817-550693377604 h1:hJwCVlE3ojViC35MGHB+FBr8TuIf3BUFn2EQ1VIamsI=
github.com/metacubex/kcp-go v0.0.0-20260105040817-550693377604/go.mod h1:lpmN3m269b3V5jFCWtffqBLS4U3QQoIid9ugtO+OhVc=
github.com/metacubex/mhurl v0.1.0 h1:ZdW4Zxe3j3uJ89gNytOazHu6kbHn5owutN/VfXOI8GE=