Skip to content

Commit 5c1567c

Browse files
ayang64odeke-em
authored andcommitted
net/http/pprof: use Request.Context, not the deprecated CloseNotifier
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: c1e37a0 GitHub-Pull-Request: #41756 Reviewed-on: https://go-review.googlesource.com/c/go/+/259157 Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Trust: Bryan C. Mills <[email protected]> Trust: Emmanuel Odeke <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 4922585 commit 5c1567c

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/net/http/pprof/pprof.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,10 @@ func Cmdline(w http.ResponseWriter, r *http.Request) {
9393
fmt.Fprintf(w, strings.Join(os.Args, "\x00"))
9494
}
9595

96-
func sleep(w http.ResponseWriter, d time.Duration) {
97-
var clientGone <-chan bool
98-
if cn, ok := w.(http.CloseNotifier); ok {
99-
clientGone = cn.CloseNotify()
100-
}
96+
func sleep(r *http.Request, d time.Duration) {
10197
select {
10298
case <-time.After(d):
103-
case <-clientGone:
99+
case <-r.Context().Done():
104100
}
105101
}
106102

@@ -142,7 +138,7 @@ func Profile(w http.ResponseWriter, r *http.Request) {
142138
fmt.Sprintf("Could not enable CPU profiling: %s", err))
143139
return
144140
}
145-
sleep(w, time.Duration(sec)*time.Second)
141+
sleep(r, time.Duration(sec)*time.Second)
146142
pprof.StopCPUProfile()
147143
}
148144

@@ -171,7 +167,7 @@ func Trace(w http.ResponseWriter, r *http.Request) {
171167
fmt.Sprintf("Could not enable tracing: %s", err))
172168
return
173169
}
174-
sleep(w, time.Duration(sec*float64(time.Second)))
170+
sleep(r, time.Duration(sec*float64(time.Second)))
175171
trace.Stop()
176172
}
177173

0 commit comments

Comments
 (0)