Skip to content

Commit 45a43cb

Browse files
authored
Merge pull request #99 from mutablelogic/v5
Added flush for text streaming
2 parents 9affafe + 8078639 commit 45a43cb

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pkg/logger/responsewriter.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,36 @@ func (rw *responseWriter) Write(b []byte) (int, error) {
6767
// The status will be StatusOK if WriteHeader has not been called yet
6868
rw.WriteHeader(http.StatusOK)
6969
}
70-
size, err := rw.ResponseWriter.Write(b)
71-
rw.size += size
72-
return size, err
70+
n, err := rw.ResponseWriter.Write(b)
71+
rw.size += n
72+
73+
// Flush
74+
if flusher, ok := rw.ResponseWriter.(http.Flusher); ok {
75+
flusher.Flush()
76+
}
77+
78+
// Return bytes written and any error encountered
79+
return n, err
7380
}
7481

7582
// ReadFrom exposes underlying http.ResponseWriter to io.Copy and if it implements
7683
// io.ReaderFrom, it can take advantage of optimizations such as sendfile, io.Copy
7784
// with sync.Pool's buffer which is in http.(*response).ReadFrom and so on.
78-
func (rw *responseWriter) ReadFrom(r io.Reader) (n int64, err error) {
85+
func (rw *responseWriter) ReadFrom(r io.Reader) (int64, error) {
7986
if !rw.Written() {
8087
// The status will be StatusOK if WriteHeader has not been called yet
8188
rw.WriteHeader(http.StatusOK)
8289
}
83-
n, err = io.Copy(rw.ResponseWriter, r)
90+
n, err := io.Copy(rw.ResponseWriter, r)
8491
rw.size += int(n)
85-
return
92+
93+
// Flush
94+
if flusher, ok := rw.ResponseWriter.(http.Flusher); ok {
95+
flusher.Flush()
96+
}
97+
98+
// Return bytes written and any error encountered
99+
return n, err
86100
}
87101

88102
// Satisfy http.ResponseController support (Go 1.20+)

0 commit comments

Comments
 (0)