author | Dave Day
<djd@golang.org> 2016-04-20 06:37:57 UTC |
committer | Dave Day
<djd@golang.org> 2016-04-21 00:36:51 UTC |
parent | fb93926129b8ec0056f2f458b1f519654814edf0 |
context/ctxhttp/cancelreq.go | +0 | -19 |
context/ctxhttp/cancelreq_go14.go | +0 | -23 |
context/ctxhttp/ctxhttp.go | +5 | -4 |
diff --git a/context/ctxhttp/cancelreq.go b/context/ctxhttp/cancelreq.go deleted file mode 100644 index e3170e3..0000000 --- a/context/ctxhttp/cancelreq.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build go1.5 - -package ctxhttp - -import "net/http" - -func canceler(client *http.Client, req *http.Request) func() { - // TODO(djd): Respect any existing value of req.Cancel. - ch := make(chan struct{}) - req.Cancel = ch - - return func() { - close(ch) - } -} diff --git a/context/ctxhttp/cancelreq_go14.go b/context/ctxhttp/cancelreq_go14.go deleted file mode 100644 index 56bcbad..0000000 --- a/context/ctxhttp/cancelreq_go14.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build !go1.5 - -package ctxhttp - -import "net/http" - -type requestCanceler interface { - CancelRequest(*http.Request) -} - -func canceler(client *http.Client, req *http.Request) func() { - rc, ok := client.Transport.(requestCanceler) - if !ok { - return func() {} - } - return func() { - rc.CancelRequest(req) - } -} diff --git a/context/ctxhttp/ctxhttp.go b/context/ctxhttp/ctxhttp.go index a7ed8d8..e45feec 100644 --- a/context/ctxhttp/ctxhttp.go +++ b/context/ctxhttp/ctxhttp.go @@ -30,8 +30,9 @@ func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Resp client = http.DefaultClient } - // Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go. - cancel := canceler(client, req) + // TODO(djd): Respect any existing value of req.Cancel. + cancel := make(chan struct{}) + req.Cancel = cancel type responseAndError struct { resp *http.Response @@ -55,7 +56,7 @@ func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Resp select { case <-ctx.Done(): testHookContextDoneBeforeHeaders() - cancel() + close(cancel) // Clean up after the goroutine calling client.Do: go func() { if r := <-result; r.resp != nil { @@ -76,7 +77,7 @@ func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Resp go func() { select { case <-ctx.Done(): - cancel() + close(cancel) case <-c: // The response's Body is closed. }