flush buffer on body copy
This commit is contained in:
parent
a837129399
commit
53d049d30f
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
@ -68,7 +67,7 @@ func (s *ProxyHandler) HandleRequest(wr http.ResponseWriter, req *http.Request)
|
||||||
copyHeader(wr.Header(), resp.Header)
|
copyHeader(wr.Header(), resp.Header)
|
||||||
wr.WriteHeader(resp.StatusCode)
|
wr.WriteHeader(resp.StatusCode)
|
||||||
flush(wr)
|
flush(wr)
|
||||||
io.Copy(wr, resp.Body)
|
copyBody(wr, resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProxyHandler) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
|
func (s *ProxyHandler) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
|
||||||
|
|
17
utils.go
17
utils.go
|
@ -11,6 +11,8 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const COPY_BUF = 128 * 1024
|
||||||
|
|
||||||
func proxy(ctx context.Context, left, right net.Conn) {
|
func proxy(ctx context.Context, left, right net.Conn) {
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
cpy := func (dst, src net.Conn) {
|
cpy := func (dst, src net.Conn) {
|
||||||
|
@ -91,3 +93,18 @@ func flush(flusher interface{}) bool {
|
||||||
f.Flush()
|
f.Flush()
|
||||||
return true
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue