Skip to content

Commit efa2243

Browse files
committed
fix: shut it down more aggressively in grpc transport closing
1 parent b0bd4f4 commit efa2243

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

transport/gun/transport_close.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package gun
22

33
import (
4+
"net"
5+
"net/http"
46
"sync"
7+
"time"
58
"unsafe"
69

710
"golang.org/x/net/http2"
@@ -13,19 +16,39 @@ type clientConnPool struct {
1316
conns map[string][]*http2.ClientConn // key is host:port
1417
}
1518

19+
type clientConn struct {
20+
t *http.Transport
21+
tconn net.Conn // usually *tls.Conn, except specialized impls
22+
}
23+
1624
type efaceWords struct {
1725
typ unsafe.Pointer
1826
data unsafe.Pointer
1927
}
2028

29+
type tlsConn interface {
30+
net.Conn
31+
NetConn() net.Conn
32+
}
33+
34+
func closeClientConn(cc *http2.ClientConn) { // like forceCloseConn() in http2.ClientConn but also apply for tls-like conn
35+
if conn, ok := (*clientConn)(unsafe.Pointer(cc)).tconn.(tlsConn); ok {
36+
t := time.AfterFunc(time.Second, func() {
37+
_ = conn.NetConn().Close()
38+
})
39+
defer t.Stop()
40+
}
41+
_ = cc.Close()
42+
}
43+
2144
func (tw *TransportWrap) Close() error {
2245
connPool := transportConnPool(tw.Transport)
2346
p := (*clientConnPool)((*efaceWords)(unsafe.Pointer(&connPool)).data)
2447
p.mu.Lock()
2548
defer p.mu.Unlock()
2649
for _, vv := range p.conns {
2750
for _, cc := range vv {
28-
cc.Close()
51+
closeClientConn(cc)
2952
}
3053
}
3154
return nil

0 commit comments

Comments
 (0)