Skip to content

Commit

Permalink
Check flusher before flush (#817)
Browse files Browse the repository at this point in the history
* Check flusher before flush

* gofmt
  • Loading branch information
kostyay authored Dec 19, 2020
1 parent a217aa7 commit 42df706
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions go/grpcweb/grpc_web_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (w *grpcWebResponse) Flush() {
if w.wroteHeaders || w.wroteBody {
// Work around the fact that WriteHeader and a call to Flush would have caused a 200 response.
// This is the case when there is no payload.
w.wrapped.(http.Flusher).Flush()
flushWriter(w.wrapped)
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ func (w *grpcWebResponse) finishRequest(req *http.Request) {
w.copyTrailersToPayload()
} else {
w.WriteHeader(http.StatusOK)
w.wrapped.(http.Flusher).Flush()
flushWriter(w.wrapped)
}
}

Expand All @@ -102,7 +102,7 @@ func (w *grpcWebResponse) copyTrailersToPayload() {
binary.BigEndian.PutUint32(trailerGrpcDataHeader[1:5], uint32(trailerBuffer.Len()))
w.wrapped.Write(trailerGrpcDataHeader)
w.wrapped.Write(trailerBuffer.Bytes())
w.wrapped.(http.Flusher).Flush()
flushWriter(w.wrapped)
}

func extractTrailingHeaders(src http.Header, flushed http.Header) http.Header {
Expand Down Expand Up @@ -157,5 +157,14 @@ func (w *base64ResponseWriter) Flush() {
grpclog.Errorf("ignoring error Flushing base64 encoder: %v", err)
}
w.newEncoder()
w.wrapped.(http.Flusher).Flush()
flushWriter(w.wrapped)
}

func flushWriter(w http.ResponseWriter) {
f, ok := w.(http.Flusher)
if !ok {
return
}

f.Flush()
}

0 comments on commit 42df706

Please sign in to comment.