Skip to content

Commit

Permalink
Write headers manually for streaming response writers instead of call…
Browse files Browse the repository at this point in the history
…ing Flush()

Signed-off-by: Christopher Petito <47751006+krissetto@users.noreply.github.com>
  • Loading branch information
krissetto committed Apr 14, 2024
1 parent 8d5d655 commit 64b33e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions api/server/httputils/write_log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"sort"

Expand All @@ -17,11 +18,15 @@ import (
// WriteLogStream writes an encoded byte stream of log messages from the
// messages channel, multiplexing them with a stdcopy.Writer if mux is true
func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMessage, config *container.LogsOptions, mux bool) {
// Used instead of the Flush() we had previously, so we don't set the header twice because of the otel wrapper
// see here: https://github.com/moby/moby/issues/47448
if rw, ok := w.(http.ResponseWriter); ok {
rw.WriteHeader(http.StatusOK)
}

wf := ioutils.NewWriteFlusher(w)
defer wf.Close()

wf.Flush()

outStream := io.Writer(wf)
errStream := outStream
sysErrStream := errStream
Expand Down
8 changes: 7 additions & 1 deletion daemon/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"net/http"
"runtime"
"time"

Expand Down Expand Up @@ -46,9 +47,14 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c

outStream := config.OutStream
if config.Stream {
// Used instead of the Flush() we had previously, so we don't set the header twice because of the otel wrapper
// see here: https://github.com/moby/moby/issues/47448
if rw, ok := outStream.(http.ResponseWriter); ok {
rw.WriteHeader(http.StatusOK)
}

wf := ioutils.NewWriteFlusher(outStream)
defer wf.Close()
wf.Flush()
outStream = wf
}

Expand Down

0 comments on commit 64b33e2

Please sign in to comment.