From 142d17ebad60728886c9c7d91ee51bea217cbddd Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Fri, 25 Nov 2022 11:04:28 +0800 Subject: [PATCH] fix: don't close tuic when read timeout --- transport/tuic/client.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/transport/tuic/client.go b/transport/tuic/client.go index 82ff4c9a..8f91ef63 100644 --- a/transport/tuic/client.go +++ b/transport/tuic/client.go @@ -214,22 +214,26 @@ func (t *Client) DialContext(ctx context.Context, metadata *C.Metadata, dialFn f if err != nil { return nil, err } - defer func() { - t.deferQuicConn(quicConn, err) + stream, err := func() (quic.Stream, error) { + defer func() { + t.deferQuicConn(quicConn, err) + }() + buf := &bytes.Buffer{} + err = NewConnect(NewAddress(metadata)).WriteTo(buf) + if err != nil { + return nil, err + } + stream, err := quicConn.OpenStream() + if err != nil { + return nil, err + } + _, err = buf.WriteTo(stream) + if err != nil { + return nil, err + } + return stream, err }() - buf := &bytes.Buffer{} - err = NewConnect(NewAddress(metadata)).WriteTo(buf) - if err != nil { - return nil, err - } - stream, err := quicConn.OpenStream() - if err != nil { - return nil, err - } - _, err = buf.WriteTo(stream) - if err != nil { - return nil, err - } + if t.RequestTimeout > 0 { _ = stream.SetReadDeadline(time.Now().Add(time.Duration(t.RequestTimeout) * time.Millisecond)) }