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.
This commit is contained in:
João Oliveirinha 2022-04-11 17:58:18 +01:00
parent d1a4710aa2
commit 9cde11f8e0
2 changed files with 2 additions and 36 deletions

View File

@ -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)
}

View File

@ -12,6 +12,7 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/cloudflare/cloudflared/carrier" "github.com/cloudflare/cloudflared/carrier"
"github.com/cloudflare/cloudflared/cfio"
"github.com/cloudflare/cloudflared/connection" "github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/ingress" "github.com/cloudflare/cloudflared/ingress"
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs" tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
@ -32,7 +33,6 @@ type Proxy struct {
warpRouting *ingress.WarpRoutingService warpRouting *ingress.WarpRoutingService
tags []tunnelpogs.Tag tags []tunnelpogs.Tag
log *zerolog.Logger log *zerolog.Logger
bufferPool *bufferPool
} }
// NewOriginProxy returns a new instance of the Proxy struct. // NewOriginProxy returns a new instance of the Proxy struct.
@ -46,7 +46,6 @@ func NewOriginProxy(
ingressRules: ingressRules, ingressRules: ingressRules,
tags: tags, tags: tags,
log: log, log: log,
bufferPool: newBufferPool(512 * 1024),
} }
if warpRoutingEnabled { if warpRoutingEnabled {
proxy.warpRouting = ingress.NewWarpRoutingService() proxy.warpRouting = ingress.NewWarpRoutingService()
@ -218,11 +217,7 @@ func (p *Proxy) proxyHTTPRequest(
p.log.Debug().Msg("Detected Server-Side Events from Origin") p.log.Debug().Msg("Detected Server-Side Events from Origin")
p.writeEventStream(w, resp.Body) p.writeEventStream(w, resp.Body)
} else { } else {
// Use CopyBuffer, because Copy only allocates a 32KiB buffer, and cross-stream _, _ = cfio.Copy(w, resp.Body)
// compression generates dictionary on first write
buf := p.bufferPool.Get()
defer p.bufferPool.Put(buf)
_, _ = io.CopyBuffer(w, resp.Body, buf)
} }
p.logOriginResponse(resp, fields) p.logOriginResponse(resp, fields)