Skip to content

Commit ab555f3

Browse files
committed
http2: add internal function isNoCachedConnError to test for ErrNoCachedConn
In a given program there may be two separate copies of ErrNoCachedConn: the h2_bundle.go version in net/http, and the user's golang.org/x/net/http2 version. We need to be able to detect either in net/http. This CL adds a function to report whether an error value represents that type of error, and then a subsequent CL to net/http will use it instead of ==. Updates golang/go#22091 Change-Id: I86f1e20704eee29b8980707b700d7a290107dfd4 Reviewed-on: https://go-review.googlesource.com/87297 Reviewed-by: Tom Bergan <[email protected]>
1 parent 434ec0c commit ab555f3

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

http2/configure_transport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type noDialH2RoundTripper struct{ t *Transport }
7373

7474
func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
7575
res, err := rt.t.RoundTrip(req)
76-
if err == ErrNoCachedConn {
76+
if isNoCachedConnError(err) {
7777
return nil, http.ErrSkipAltProtocol
7878
}
7979
return res, err

http2/transport.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,26 @@ func (sew stickyErrWriter) Write(p []byte) (n int, err error) {
306306
return
307307
}
308308

309-
var ErrNoCachedConn = errors.New("http2: no cached connection was available")
309+
// noCachedConnError is the concrete type of ErrNoCachedConn, which
310+
// needs to be detected by net/http regardless of whether it's its
311+
// bundled version (in h2_bundle.go with a rewritten type name) or
312+
// from a user's x/net/http2. As such, as it has a unique method name
313+
// (IsHTTP2NoCachedConnError) that net/http sniffs for via func
314+
// isNoCachedConnError.
315+
type noCachedConnError struct{}
316+
317+
func (noCachedConnError) IsHTTP2NoCachedConnError() {}
318+
func (noCachedConnError) Error() string { return "http2: no cached connection was available" }
319+
320+
// isNoCachedConnError reports whether err is of type noCachedConnError
321+
// or its equivalent renamed type in net/http2's h2_bundle.go. Both types
322+
// may coexist in the same running program.
323+
func isNoCachedConnError(err error) bool {
324+
_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
325+
return ok
326+
}
327+
328+
var ErrNoCachedConn error = noCachedConnError{}
310329

311330
// RoundTripOpt are options for the Transport.RoundTripOpt method.
312331
type RoundTripOpt struct {

0 commit comments

Comments
 (0)