From 9cde11f8e096ed92fb874ecbba69c1c79b48b707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveirinha?= Date: Mon, 11 Apr 2022 17:58:18 +0100 Subject: [PATCH] TUN-6038: Reduce buffer size used for proxying data The buffer size was big to support a compression feature that we don't use anymore. As such, we can now reduce this and be more efficient with memory usage. --- proxy/pool.go | 29 ----------------------------- proxy/proxy.go | 9 ++------- 2 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 proxy/pool.go diff --git a/proxy/pool.go b/proxy/pool.go deleted file mode 100644 index fe2cf4a5..00000000 --- a/proxy/pool.go +++ /dev/null @@ -1,29 +0,0 @@ -package proxy - -import ( - "sync" -) - -type bufferPool struct { - // A bufferPool must not be copied after first use. - // https://golang.org/pkg/sync/#Pool - buffers sync.Pool -} - -func newBufferPool(bufferSize int) *bufferPool { - return &bufferPool{ - buffers: sync.Pool{ - New: func() interface{} { - return make([]byte, bufferSize) - }, - }, - } -} - -func (p *bufferPool) Get() []byte { - return p.buffers.Get().([]byte) -} - -func (p *bufferPool) Put(buf []byte) { - p.buffers.Put(buf) -} diff --git a/proxy/proxy.go b/proxy/proxy.go index 5ec8743b..3ab3c370 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -12,6 +12,7 @@ import ( "github.com/rs/zerolog" "github.com/cloudflare/cloudflared/carrier" + "github.com/cloudflare/cloudflared/cfio" "github.com/cloudflare/cloudflared/connection" "github.com/cloudflare/cloudflared/ingress" tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs" @@ -32,7 +33,6 @@ type Proxy struct { warpRouting *ingress.WarpRoutingService tags []tunnelpogs.Tag log *zerolog.Logger - bufferPool *bufferPool } // NewOriginProxy returns a new instance of the Proxy struct. @@ -46,7 +46,6 @@ func NewOriginProxy( ingressRules: ingressRules, tags: tags, log: log, - bufferPool: newBufferPool(512 * 1024), } if warpRoutingEnabled { proxy.warpRouting = ingress.NewWarpRoutingService() @@ -218,11 +217,7 @@ func (p *Proxy) proxyHTTPRequest( p.log.Debug().Msg("Detected Server-Side Events from Origin") p.writeEventStream(w, resp.Body) } else { - // Use CopyBuffer, because Copy only allocates a 32KiB buffer, and cross-stream - // compression generates dictionary on first write - buf := p.bufferPool.Get() - defer p.bufferPool.Put(buf) - _, _ = io.CopyBuffer(w, resp.Body, buf) + _, _ = cfio.Copy(w, resp.Body) } p.logOriginResponse(resp, fields)