]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/pprof: use Request.Context, not the deprecated CloseNotifier
authorAyan George <ayan@ayan.net>
Tue, 6 Oct 2020 18:40:40 +0000 (18:40 +0000)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Wed, 7 Oct 2020 18:56:58 +0000 (18:56 +0000)
Prior to this commit, the profiling code had a sleep() function that
waits and unblocks on either time.After() or a channel provided by an
http.CloseNotifier derived from a supplied http.ResponseWriter.

According to the documentation, http.CloseNotifier is deprecated:

  Deprecated: the CloseNotifier interface predates Go's context package.
  New code should use Request.Context instead.

This patch does just that -- sleep() now takes an *http.Request and uses
http.Request.Context() to signal when a request has been cancelled.

Change-Id: I98702314addf494f5743a4f99172dc607389dbb8
GitHub-Last-Rev: c1e37a03ca28417ed5833618d3eeddb2eecccd09
GitHub-Pull-Request: golang/go#41756
Reviewed-on: https://go-review.googlesource.com/c/go/+/259157
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/net/http/pprof/pprof.go

index 81df0448e94d100ab4f3df92875e114375f52a2c..5ff7fdc3deedbb7b776131f6e3a9a885116a89b0 100644 (file)
@@ -93,14 +93,10 @@ func Cmdline(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, strings.Join(os.Args, "\x00"))
 }
 
-func sleep(w http.ResponseWriter, d time.Duration) {
-       var clientGone <-chan bool
-       if cn, ok := w.(http.CloseNotifier); ok {
-               clientGone = cn.CloseNotify()
-       }
+func sleep(r *http.Request, d time.Duration) {
        select {
        case <-time.After(d):
-       case <-clientGone:
+       case <-r.Context().Done():
        }
 }
 
@@ -142,7 +138,7 @@ func Profile(w http.ResponseWriter, r *http.Request) {
                        fmt.Sprintf("Could not enable CPU profiling: %s", err))
                return
        }
-       sleep(w, time.Duration(sec)*time.Second)
+       sleep(r, time.Duration(sec)*time.Second)
        pprof.StopCPUProfile()
 }
 
@@ -171,7 +167,7 @@ func Trace(w http.ResponseWriter, r *http.Request) {
                        fmt.Sprintf("Could not enable tracing: %s", err))
                return
        }
-       sleep(w, time.Duration(sec*float64(time.Second)))
+       sleep(r, time.Duration(sec*float64(time.Second)))
        trace.Stop()
 }