Commit 153d4a5a authored by Leonard Hecker's avatar Leonard Hecker

proxy: Improved handling of bufferPool

parent 20483c23
...@@ -27,15 +27,28 @@ import ( ...@@ -27,15 +27,28 @@ import (
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
var defaultDialer = &net.Dialer{ var (
Timeout: 30 * time.Second, defaultDialer = &net.Dialer{
KeepAlive: 30 * time.Second, Timeout: 30 * time.Second,
} KeepAlive: 30 * time.Second,
}
var bufferPool = sync.Pool{New: createBuffer} bufferPool = sync.Pool{New: createBuffer}
)
func createBuffer() interface{} { func createBuffer() interface{} {
return make([]byte, 32*1024) return make([]byte, 0, 32*1024)
}
func pooledIoCopy(dst io.Writer, src io.Reader) {
buf := bufferPool.Get().([]byte)
defer bufferPool.Put(buf)
// CopyBuffer only uses buf up to its length and panics if it's 0.
// Due to that we extend buf's length to its capacity here and
// ensure it's always non-zero.
bufCap := cap(buf)
io.CopyBuffer(dst, src, buf[0:bufCap:bufCap])
} }
// onExitFlushLoop is a callback set by tests to detect the state of the // onExitFlushLoop is a callback set by tests to detect the state of the
...@@ -234,10 +247,8 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, ...@@ -234,10 +247,8 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
} }
defer backendConn.Close() defer backendConn.Close()
go func() { go pooledIoCopy(backendConn, conn) // write tcp stream to backend
io.Copy(backendConn, conn) // write tcp stream to backend. pooledIoCopy(conn, backendConn) // read tcp stream from backend
}()
io.Copy(conn, backendConn) // read tcp stream from backend.
} else { } else {
defer res.Body.Close() defer res.Body.Close()
for _, h := range hopHeaders { for _, h := range hopHeaders {
...@@ -252,9 +263,6 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, ...@@ -252,9 +263,6 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
} }
func (rp *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { func (rp *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
buf := bufferPool.Get().([]byte)
defer bufferPool.Put(buf)
if rp.FlushInterval != 0 { if rp.FlushInterval != 0 {
if wf, ok := dst.(writeFlusher); ok { if wf, ok := dst.(writeFlusher); ok {
mlw := &maxLatencyWriter{ mlw := &maxLatencyWriter{
...@@ -267,10 +275,7 @@ func (rp *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { ...@@ -267,10 +275,7 @@ func (rp *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
dst = mlw dst = mlw
} }
} }
pooledIoCopy(dst, src)
// `CopyBuffer` only uses `buf` up to it's length and
// panics if it's 0 => Extend it's length up to it's capacity.
io.CopyBuffer(dst, src, buf[:cap(buf)])
} }
// skip these headers if they already exist. // skip these headers if they already exist.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment