@@ -46,18 +46,17 @@ type DialOptions struct {
4646 CompressionThreshold int
4747}
4848
49- func (opts * DialOptions ) cloneWithDefaults () * DialOptions {
49+ func (opts * DialOptions ) cloneWithDefaults (ctx context.Context ) (context.Context , context.CancelFunc , * DialOptions ) {
50+ var cancel context.CancelFunc
51+
5052 var o DialOptions
5153 if opts != nil {
5254 o = * opts
5355 }
5456 if o .HTTPClient == nil {
5557 o .HTTPClient = http .DefaultClient
5658 } else if opts .HTTPClient .Timeout > 0 {
57- var cancel context.CancelFunc
58-
5959 ctx , cancel = context .WithTimeout (ctx , opts .HTTPClient .Timeout )
60- defer cancel ()
6160
6261 newClient := * opts .HTTPClient
6362 newClient .Timeout = 0
@@ -66,7 +65,8 @@ func (opts *DialOptions) cloneWithDefaults() *DialOptions {
6665 if o .HTTPHeader == nil {
6766 o .HTTPHeader = http.Header {}
6867 }
69- return & o
68+
69+ return ctx , cancel , & o
7070}
7171
7272// Dial performs a WebSocket handshake on url.
@@ -89,7 +89,11 @@ func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Respon
8989func dial (ctx context.Context , urls string , opts * DialOptions , rand io.Reader ) (_ * Conn , _ * http.Response , err error ) {
9090 defer errd .Wrap (& err , "failed to WebSocket dial" )
9191
92- opts = opts .cloneWithDefaults ()
92+ var cancel context.CancelFunc
93+ ctx , cancel , opts = opts .cloneWithDefaults (ctx )
94+ if cancel != nil {
95+ defer cancel ()
96+ }
9397
9498 secWebSocketKey , err := secWebSocketKey (rand )
9599 if err != nil {
0 commit comments