Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

feat: add buffer pool #133

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/proxy/buffer_pool.go
Original file line number Diff line number Diff line change
@@ -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)
},
},
}
}
2 changes: 2 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (h *Proxy) AddTarget(target TargetConfig) error {
return err
}

proxy.BufferPool = newBufferPool()

h.targets = append(
h.targets,
&HTTPTarget{
Expand Down