select {
case v := <-dialc:
// Our dial finished.
- if trace != nil && trace.GotConn != nil && v.pc != nil && v.pc.alt == nil {
- trace.GotConn(httptrace.GotConnInfo{Conn: v.pc.conn})
+ if v.pc != nil {
+ if trace != nil && trace.GotConn != nil && v.pc.alt == nil {
+ trace.GotConn(httptrace.GotConnInfo{Conn: v.pc.conn})
+ }
+ return v.pc, nil
}
- return v.pc, v.err
+ // Our dial failed. See why to return a nicer error
+ // value.
+ select {
+ case <-req.Cancel:
+ case <-req.Context().Done():
+ case <-cancelc:
+ default:
+ // It wasn't an error due to cancelation, so
+ // return the original error message:
+ return nil, v.err
+ }
+ // It was an error due to cancelation, so prioritize that
+ // error value. (Issue 16049)
+ return nil, errRequestCanceledConn
case pc := <-idleConnCh:
// Another request finished first and its net.Conn
// became available before our dial. Or somebody