aboutsummaryrefslogtreecommitdiff
path: root/src/net/http
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2022-01-07 10:11:08 -0800
committerDamien Neil <dneil@google.com>2022-03-24 21:51:49 +0000
commita10a209b23f877c80d8a5f3ebda1ce4b492ac3a9 (patch)
tree69932581eaa4f059a4107af15f6e5f5714204e17 /src/net/http
parentb88600f926b6a1fb6de6e3a8cc1f0db9132bddb4 (diff)
downloadgo-a10a209b23f877c80d8a5f3ebda1ce4b492ac3a9.tar.xz
net/http/httputil: ignore CloseNotify when a non-background context is present
If the http.Request passed to ReverseProxy.ServeHTTP has a context with a non-nil Done channel, don't watch the ResponseWriter's CloseNotify channel. Avoids starting an extra background goroutine in the common case. Change-Id: I1328f3e02d3025caa0f446a2f20dfc14ef604c64 Reviewed-on: https://go-review.googlesource.com/c/go/+/376415 Trust: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Trust: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net/http')
-rw-r--r--src/net/http/httputil/reverseproxy.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/net/http/httputil/reverseproxy.go b/src/net/http/httputil/reverseproxy.go
index 319e2a3f3f..b7244134f0 100644
--- a/src/net/http/httputil/reverseproxy.go
+++ b/src/net/http/httputil/reverseproxy.go
@@ -218,7 +218,18 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
ctx := req.Context()
- if cn, ok := rw.(http.CloseNotifier); ok {
+ if ctx.Done() != nil {
+ // CloseNotifier predates context.Context, and has been
+ // entirely superseded by it. If the request contains
+ // a Context that carries a cancelation signal, don't
+ // bother spinning up a goroutine to watch the CloseNotify
+ // channel (if any).
+ //
+ // If the request Context has a nil Done channel (which
+ // means it is either context.Background, or a custom
+ // Context implementation with no cancelation signal),
+ // then consult the CloseNotifier if available.
+ } else if cn, ok := rw.(http.CloseNotifier); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()