Skip to content

Commit 190047c

Browse files
committed
fix: grpc transport not apply dial timeout
1 parent 24a9ff6 commit 190047c

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

transport/gun/gun.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/metacubex/mihomo/common/buf"
2323
"github.com/metacubex/mihomo/common/pool"
2424
tlsC "github.com/metacubex/mihomo/component/tls"
25+
C "github.com/metacubex/mihomo/constant"
2526

2627
"golang.org/x/net/http2"
2728
)
@@ -224,11 +225,9 @@ func (g *Conn) SetDeadline(t time.Time) error {
224225
}
225226

226227
func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config, Fingerprint string, realityConfig *tlsC.RealityConfig) *TransportWrap {
227-
closed := &atomic.Bool{}
228228
dialFunc := func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
229-
if closed.Load() {
230-
return nil, errors.New("transport closed")
231-
}
229+
ctx, cancel := context.WithTimeout(ctx, C.DefaultTLSTimeout)
230+
defer cancel()
232231
pconn, err := dialFn(ctx, network, addr)
233232
if err != nil {
234233
return nil, err
@@ -291,9 +290,12 @@ func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config, Fingerprint string, re
291290
DisableCompression: true,
292291
PingTimeout: 0,
293292
}
293+
294+
ctx, cancel := context.WithCancel(context.Background())
294295
wrap := &TransportWrap{
295296
Transport: transport,
296-
closed: closed,
297+
ctx: ctx,
298+
cancel: cancel,
297299
}
298300
return wrap
299301
}
@@ -320,6 +322,7 @@ func StreamGunWithTransport(transport *TransportWrap, cfg *Config) (net.Conn, er
320322
ProtoMinor: 0,
321323
Header: defaultHeader,
322324
}
325+
request = request.WithContext(transport.ctx)
323326

324327
conn := &Conn{
325328
initFn: func() (io.ReadCloser, netAddr, error) {

transport/gun/transport.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
package gun
22

33
import (
4+
"context"
45
"net"
5-
6-
"github.com/metacubex/mihomo/common/atomic"
6+
"sync"
77

88
"golang.org/x/net/http2"
99
)
1010

1111
type TransportWrap struct {
1212
*http2.Transport
13-
closed *atomic.Bool
13+
ctx context.Context
14+
cancel context.CancelFunc
15+
closeOnce sync.Once
16+
}
17+
18+
func (tw *TransportWrap) Close() error {
19+
tw.closeOnce.Do(func() {
20+
tw.cancel()
21+
closeTransport(tw.Transport)
22+
})
23+
return nil
1424
}
1525

1626
type netAddr struct {

transport/gun/transport_close.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ func closeClientConn(cc *http2.ClientConn) { // like forceCloseConn() in http2.C
4545
_ = cc.Close()
4646
}
4747

48-
func (tw *TransportWrap) Close() error {
49-
if tw.closed.Swap(true) {
50-
return nil // already closed
51-
}
52-
connPool := transportConnPool(tw.Transport)
48+
func closeTransport(tr *http2.Transport) {
49+
connPool := transportConnPool(tr)
5350
p := (*clientConnPool)((*efaceWords)(unsafe.Pointer(&connPool)).data)
5451
p.mu.Lock()
5552
defer p.mu.Unlock()
@@ -61,7 +58,6 @@ func (tw *TransportWrap) Close() error {
6158
// cleanup
6259
p.conns = make(map[string][]*http2.ClientConn)
6360
p.keys = make(map[*http2.ClientConn][]string)
64-
return nil
6561
}
6662

6763
//go:linkname transportConnPool golang.org/x/net/http2.(*Transport).connPool

0 commit comments

Comments
 (0)