Skip to content

Commit

Permalink
flush buffer on body copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed May 22, 2020
1 parent a837129 commit 53d049d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io"
"net"
"fmt"
"time"
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 53d049d

Please sign in to comment.