Skip to content

Commit

Permalink
dial.go: Use timeout on HTTPClient properly
Browse files Browse the repository at this point in the history
Closes #341
  • Loading branch information
nhooyr committed Oct 13, 2023
1 parent 2598ea2 commit b4b86b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
31 changes: 31 additions & 0 deletions conn_test.go
Expand Up @@ -264,6 +264,37 @@ func TestConn(t *testing.T) {
err = c1.Close(websocket.StatusNormalClosure, "")
assert.Success(t, err)
})

t.Run("HTTPClient.Timeout", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, &websocket.DialOptions{
HTTPClient: &http.Client{Timeout: time.Second*5},
}, nil)

tt.goEchoLoop(c2)

c1.SetReadLimit(1 << 30)

exp := xrand.String(xrand.Int(131072))

werr := xsync.Go(func() error {
return wsjson.Write(tt.ctx, c1, exp)
})

var act interface{}
err := wsjson.Read(tt.ctx, c1, &act)
assert.Success(t, err)
assert.Equal(t, "read msg", exp, act)

select {
case err := <-werr:
assert.Success(t, err)
case <-tt.ctx.Done():
t.Fatal(tt.ctx.Err())
}

err = c1.Close(websocket.StatusNormalClosure, "")
assert.Success(t, err)
})
}

func TestWasm(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions dial.go
Expand Up @@ -59,12 +59,13 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context
}
if o.HTTPClient == nil {
o.HTTPClient = http.DefaultClient
} else if opts.HTTPClient.Timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, opts.HTTPClient.Timeout)
}
if o.HTTPClient.Timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, o.HTTPClient.Timeout)

newClient := *opts.HTTPClient
newClient := *o.HTTPClient
newClient.Timeout = 0
opts.HTTPClient = &newClient
o.HTTPClient = &newClient
}
if o.HTTPHeader == nil {
o.HTTPHeader = http.Header{}
Expand Down

0 comments on commit b4b86b9

Please sign in to comment.