Skip to content
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

fix: superfluous response.WriteHeader call #724

Closed
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
16 changes: 7 additions & 9 deletions prometheus/promhttp/delegator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (r *responseWriterDelegator) Written() int64 {
}

func (r *responseWriterDelegator) WriteHeader(code int) {
// Avoid superfluous response.WriteHeader call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full stop

if r.wroteHeader {
return
}
r.status = code
r.wroteHeader = true
r.ResponseWriter.WriteHeader(code)
Expand All @@ -64,9 +68,7 @@ func (r *responseWriterDelegator) WriteHeader(code int) {
func (r *responseWriterDelegator) Write(b []byte) (int, error) {
// If applicable, call WriteHeader here so that observeWriteHeader is
// handled appropriately.
if !r.wroteHeader {
r.WriteHeader(http.StatusOK)
}
r.WriteHeader(http.StatusOK)
n, err := r.ResponseWriter.Write(b)
r.written += int64(n)
return n, err
Expand All @@ -86,9 +88,7 @@ func (d closeNotifierDelegator) CloseNotify() <-chan bool {
func (d flusherDelegator) Flush() {
// If applicable, call WriteHeader here so that observeWriteHeader is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "If applicable" is now a bit confusing (as there is no if statement anymore). How about changing this and the other comments to "Call WriteHeader here so that observeWriteHeader is handled appropriately if still needed."

// handled appropriately.
if !d.wroteHeader {
d.WriteHeader(http.StatusOK)
}
d.WriteHeader(http.StatusOK)
d.ResponseWriter.(http.Flusher).Flush()
}
func (d hijackerDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
Expand All @@ -97,9 +97,7 @@ func (d hijackerDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
func (d readerFromDelegator) ReadFrom(re io.Reader) (int64, error) {
// If applicable, call WriteHeader here so that observeWriteHeader is
// handled appropriately.
if !d.wroteHeader {
d.WriteHeader(http.StatusOK)
}
d.WriteHeader(http.StatusOK)
n, err := d.ResponseWriter.(io.ReaderFrom).ReadFrom(re)
d.written += n
return n, err
Expand Down