Skip to content

Commit dbaf6f8

Browse files
committed
Create context with http client timeout
1 parent 1b61eb0 commit dbaf6f8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

dial.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type DialOptions struct {
2525
// HTTPClient is used for the connection.
2626
// Its Transport must return writable bodies for WebSocket handshakes.
2727
// http.Transport does beginning with Go 1.12.
28-
// Non-zero timeout will be ignored, see https://github.com/nhooyr/websocket/issues/67.
2928
HTTPClient *http.Client
3029

3130
// HTTPHeader specifies the HTTP headers included in the handshake request.
@@ -75,7 +74,11 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
7574
if opts.HTTPClient == nil {
7675
opts.HTTPClient = http.DefaultClient
7776
} else if opts.HTTPClient.Timeout > 0 {
78-
// remove timeout
77+
var cancel context.CancelFunc
78+
79+
ctx, cancel = context.WithTimeout(ctx, opts.HTTPClient.Timeout)
80+
defer cancel()
81+
7982
opts.HTTPClient = &http.Client{
8083
Transport: opts.HTTPClient.Transport,
8184
CheckRedirect: opts.HTTPClient.CheckRedirect,

0 commit comments

Comments
 (0)