Skip to content

net/http/pprof: configure WriteDeadline #64360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/net/http/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ func sleep(r *http.Request, d time.Duration) {
}
}

func durationExceedsWriteTimeout(r *http.Request, seconds float64) bool {
func configureWriteDeadline(w http.ResponseWriter, r *http.Request, seconds float64) {
srv, ok := r.Context().Value(http.ServerContextKey).(*http.Server)
return ok && srv.WriteTimeout != 0 && seconds >= srv.WriteTimeout.Seconds()
if ok && srv.WriteTimeout > 0 {
timeout := srv.WriteTimeout + time.Duration(seconds*float64(time.Second))

rc := http.NewResponseController(w)
rc.SetWriteDeadline(time.Now().Add(timeout))
}
}

func serveError(w http.ResponseWriter, status int, txt string) {
Expand All @@ -137,10 +142,7 @@ func Profile(w http.ResponseWriter, r *http.Request) {
sec = 30
}

if durationExceedsWriteTimeout(r, float64(sec)) {
serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout")
return
}
configureWriteDeadline(w, r, float64(sec))

// Set Content Type assuming StartCPUProfile will work,
// because if it does it starts writing.
Expand All @@ -166,10 +168,7 @@ func Trace(w http.ResponseWriter, r *http.Request) {
sec = 1
}

if durationExceedsWriteTimeout(r, sec) {
serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout")
return
}
configureWriteDeadline(w, r, sec)

// Set Content Type assuming trace.Start will work,
// because if it does it starts writing.
Expand Down Expand Up @@ -273,15 +272,14 @@ func (name handler) serveDeltaProfile(w http.ResponseWriter, r *http.Request, p
serveError(w, http.StatusBadRequest, `invalid value for "seconds" - must be a positive integer`)
return
}
// 'name' should be a key in profileSupportsDelta.
if !profileSupportsDelta[name] {
serveError(w, http.StatusBadRequest, `"seconds" parameter is not supported for this profile type`)
return
}
// 'name' should be a key in profileSupportsDelta.
if durationExceedsWriteTimeout(r, float64(sec)) {
serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout")
return
}

configureWriteDeadline(w, r, float64(sec))

debug, _ := strconv.Atoi(r.FormValue("debug"))
if debug != 0 {
serveError(w, http.StatusBadRequest, "seconds and debug params are incompatible")
Expand Down