git » go-net » commit 815d315

context/ctxhttp: remove pre-Go 1.5 support

author Dave Day
2016-04-20 06:37:57 UTC
committer Dave Day
2016-04-21 00:36:51 UTC
parent fb93926129b8ec0056f2f458b1f519654814edf0

context/ctxhttp: remove pre-Go 1.5 support

The Go 1.4 (and earlier) support for request cancellation is racy and
complicates the implementation of ctxhttp. This change simply inlines
the existing 1.5+ cancellation logic directly, and removes support for
older versions.

Change-Id: I7df9f0648dd0e571366374d079e2976050464ca3
Reviewed-on: https://go-review.googlesource.com/22302
Reviewed-by: David Symonds <dsymonds@golang.org>
Run-TryBot: David Symonds <dsymonds@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>

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.
 		}