Skip to content

Commit 84466c0

Browse files
committed
fixup! Merge pull request #261 from swithek/copy-http-client
1 parent 5bd4335 commit 84466c0

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

dial.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -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
8989
func 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 {
@@ -246,7 +250,8 @@ func verifyServerExtensions(copts *compressionOptions, h http.Header) (*compress
246250
return nil, fmt.Errorf("WebSocket protcol violation: unsupported extensions from server: %+v", exts[1:])
247251
}
248252

249-
copts = &*copts
253+
_copts := *copts
254+
copts = &_copts
250255

251256
for _, p := range ext.params {
252257
switch p {

internal/test/wstest/pipe.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions)
2424
if dialOpts == nil {
2525
dialOpts = &websocket.DialOptions{}
2626
}
27-
dialOpts = &*dialOpts
27+
_dialOpts := *dialOpts
28+
dialOpts = &_dialOpts
2829
dialOpts.HTTPClient = &http.Client{
2930
Transport: tt,
3031
}

0 commit comments

Comments
 (0)