flush buffer on body copy

This commit is contained in:
Vladislav Yarmak 2020-05-22 21:02:08 +03:00
parent a837129399
commit 53d049d30f
2 changed files with 18 additions and 2 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"io"
"net"
"fmt"
"time"
@ -68,7 +67,7 @@ func (s *ProxyHandler) HandleRequest(wr http.ResponseWriter, req *http.Request)
copyHeader(wr.Header(), resp.Header)
wr.WriteHeader(resp.StatusCode)
flush(wr)
io.Copy(wr, resp.Body)
copyBody(wr, resp.Body)
}
func (s *ProxyHandler) ServeHTTP(wr http.ResponseWriter, req *http.Request) {

View File

@ -11,6 +11,8 @@ import (
"bufio"
)
const COPY_BUF = 128 * 1024
func proxy(ctx context.Context, left, right net.Conn) {
wg := sync.WaitGroup{}
cpy := func (dst, src net.Conn) {
@ -91,3 +93,18 @@ func flush(flusher interface{}) bool {
f.Flush()
return true
}
func copyBody(wr io.Writer, body io.Reader) {
for {
buf := make([]byte, COPY_BUF)
bread, read_err := body.Read(buf)
var write_err error
if bread > 0 {
_, write_err = wr.Write(buf[:bread])
flush(wr)
}
if read_err != nil || write_err != nil {
break
}
}
}