Skip to content

Commit

Permalink
Use buffer pool for proxy copying data
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba committed Sep 1, 2024
1 parent 81cfa52 commit 1e3b734
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package buffer

import "sync"

type BufferPool struct {
pool *sync.Pool
}

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

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

func (p *BufferPool) Put(b []byte) {
p.pool.Put(b)

Check failure on line 24 in internal/buffer/buffer.go

View workflow job for this annotation

GitHub Actions / lint

SA6002: argument should be pointer-like to avoid allocations (staticcheck)
}
16 changes: 16 additions & 0 deletions internal/buffer/buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package buffer

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBufferPool(t *testing.T) {
t.Parallel()

bufferPool := NewBufferPool()
b := bufferPool.Get()
require.Len(t, b, 32*1024)
bufferPool.Put(b)
}
4 changes: 4 additions & 0 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/go-logr/logr"

"github.com/spegel-org/spegel/internal/buffer"
"github.com/spegel-org/spegel/internal/mux"
"github.com/spegel-org/spegel/pkg/metrics"
"github.com/spegel-org/spegel/pkg/oci"
Expand All @@ -37,6 +38,7 @@ type Registry struct {
resolveRetries int
resolveTimeout time.Duration
resolveLatestTag bool
bufferPool *buffer.BufferPool
}

type Option func(*Registry)
Expand Down Expand Up @@ -90,6 +92,7 @@ func NewRegistry(ociClient oci.Client, router routing.Router, opts ...Option) *R
resolveRetries: 3,
resolveTimeout: 20 * time.Millisecond,
resolveLatestTag: true,
bufferPool: buffer.NewBufferPool(),
}
for _, opt := range opts {
opt(r)
Expand Down Expand Up @@ -280,6 +283,7 @@ func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, ref re
Host: ipAddr.String(),
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.BufferPool = r.bufferPool
proxy.Transport = r.transport
proxy.ErrorHandler = func(_ http.ResponseWriter, _ *http.Request, err error) {
log.Error(err, "request to mirror failed", "attempt", mirrorAttempts)
Expand Down

0 comments on commit 1e3b734

Please sign in to comment.