diff --git a/internal/proxy/buffer_pool.go b/internal/proxy/buffer_pool.go new file mode 100644 index 0000000..6f362a1 --- /dev/null +++ b/internal/proxy/buffer_pool.go @@ -0,0 +1,30 @@ +package proxy + +import ( + "net/http/httputil" + "sync" +) + +const DefaultMaxBufferSize = 1024 * 32 // MB + +type bufferPool struct { + pool sync.Pool +} + +func (b *bufferPool) Get() []byte { + return b.pool.Get().([]byte) +} + +func (b *bufferPool) Put(bytes []byte) { + b.pool.Put(bytes) // nolint:staticcheck +} + +func newBufferPool() httputil.BufferPool { + return &bufferPool{ + pool: sync.Pool{ + New: func() interface{} { + return make([]byte, DefaultMaxBufferSize) + }, + }, + } +} diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 7c81ea1..0e30fec 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -85,6 +85,8 @@ func (h *Proxy) AddTarget(target TargetConfig) error { return err } + proxy.BufferPool = newBufferPool() + h.targets = append( h.targets, &HTTPTarget{