mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-12 21:18:03 +08:00
fix: shut it down more aggressively in grpc transport closing
This commit is contained in:
parent
b0bd4f4caf
commit
efa224373f
@ -1,7 +1,10 @@
|
|||||||
package gun
|
package gun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
@ -13,11 +16,31 @@ type clientConnPool struct {
|
|||||||
conns map[string][]*http2.ClientConn // key is host:port
|
conns map[string][]*http2.ClientConn // key is host:port
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type clientConn struct {
|
||||||
|
t *http.Transport
|
||||||
|
tconn net.Conn // usually *tls.Conn, except specialized impls
|
||||||
|
}
|
||||||
|
|
||||||
type efaceWords struct {
|
type efaceWords struct {
|
||||||
typ unsafe.Pointer
|
typ unsafe.Pointer
|
||||||
data unsafe.Pointer
|
data unsafe.Pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tlsConn interface {
|
||||||
|
net.Conn
|
||||||
|
NetConn() net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeClientConn(cc *http2.ClientConn) { // like forceCloseConn() in http2.ClientConn 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 (tw *TransportWrap) Close() error {
|
func (tw *TransportWrap) Close() error {
|
||||||
connPool := transportConnPool(tw.Transport)
|
connPool := transportConnPool(tw.Transport)
|
||||||
p := (*clientConnPool)((*efaceWords)(unsafe.Pointer(&connPool)).data)
|
p := (*clientConnPool)((*efaceWords)(unsafe.Pointer(&connPool)).data)
|
||||||
@ -25,7 +48,7 @@ func (tw *TransportWrap) Close() error {
|
|||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
for _, vv := range p.conns {
|
for _, vv := range p.conns {
|
||||||
for _, cc := range vv {
|
for _, cc := range vv {
|
||||||
cc.Close()
|
closeClientConn(cc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user