Skip to content

Commit

Permalink
Use a buffer pool when proxying
Browse files Browse the repository at this point in the history
Improves performance by reducing allocations.
  • Loading branch information
kevinmcconnell committed Jul 26, 2024
1 parent ab0ecfb commit a44867a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/server/proxy_buffer_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package server

import "sync"

func NewBufferPool(bufferSize int64) *BufferPool {
return &BufferPool{
pool: sync.Pool{
New: func() interface{} {
return make([]byte, bufferSize)
},
},
}
}

type BufferPool struct {
pool sync.Pool
}

func (b *BufferPool) Get() []byte {
return b.pool.Get().([]byte)
}

func (b *BufferPool) Put(content []byte) {
b.pool.Put(content)
}
1 change: 1 addition & 0 deletions internal/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
DefaultHealthCheckTimeout = time.Second * 5

MaxIdleConnsPerHost = 100
ProxyBufferSize = 32 * KB

DefaultTargetTimeout = time.Second * 10
DefaultMaxRequestMemoryBufferSize = 1 * MB
Expand Down
3 changes: 3 additions & 0 deletions internal/server/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ func (t *Target) recordTargetNameForRequest(req *http.Request) {
}

func (t *Target) createProxyHandler() http.Handler {
bufferPool := NewBufferPool(ProxyBufferSize)

return &httputil.ReverseProxy{
BufferPool: bufferPool,
Rewrite: t.Rewrite,
ErrorHandler: t.handleProxyError,
Transport: &http.Transport{
Expand Down

0 comments on commit a44867a

Please sign in to comment.