@@ -46,18 +46,17 @@ type DialOptions struct {
46
46
CompressionThreshold int
47
47
}
48
48
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
+
50
52
var o DialOptions
51
53
if opts != nil {
52
54
o = * opts
53
55
}
54
56
if o .HTTPClient == nil {
55
57
o .HTTPClient = http .DefaultClient
56
58
} else if opts .HTTPClient .Timeout > 0 {
57
- var cancel context.CancelFunc
58
-
59
59
ctx , cancel = context .WithTimeout (ctx , opts .HTTPClient .Timeout )
60
- defer cancel ()
61
60
62
61
newClient := * opts .HTTPClient
63
62
newClient .Timeout = 0
@@ -66,7 +65,8 @@ func (opts *DialOptions) cloneWithDefaults() *DialOptions {
66
65
if o .HTTPHeader == nil {
67
66
o .HTTPHeader = http.Header {}
68
67
}
69
- return & o
68
+
69
+ return ctx , cancel , & o
70
70
}
71
71
72
72
// Dial performs a WebSocket handshake on url.
@@ -89,7 +89,11 @@ func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Respon
89
89
func dial (ctx context.Context , urls string , opts * DialOptions , rand io.Reader ) (_ * Conn , _ * http.Response , err error ) {
90
90
defer errd .Wrap (& err , "failed to WebSocket dial" )
91
91
92
- opts = opts .cloneWithDefaults ()
92
+ var cancel context.CancelFunc
93
+ ctx , cancel , opts = opts .cloneWithDefaults (ctx )
94
+ if cancel != nil {
95
+ defer cancel ()
96
+ }
93
97
94
98
secWebSocketKey , err := secWebSocketKey (rand )
95
99
if err != nil {
@@ -246,7 +250,8 @@ func verifyServerExtensions(copts *compressionOptions, h http.Header) (*compress
246
250
return nil , fmt .Errorf ("WebSocket protcol violation: unsupported extensions from server: %+v" , exts [1 :])
247
251
}
248
252
249
- copts = & * copts
253
+ _copts := * copts
254
+ copts = & _copts
250
255
251
256
for _ , p := range ext .params {
252
257
switch p {
0 commit comments