Skip to content

Commit

Permalink
Use buffer pool for proxy copying data (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Sep 1, 2024
2 parents 81cfa52 + 2c0b6d0 commit 9126f7f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#538](https://github.com/spegel-org/spegel/pull/538) Replace mock OCI client with in memory client.
- [#552](https://github.com/spegel-org/spegel/pull/552) Add support for VerticalPodAutoscaler in the Helm chart.
- [#556](https://github.com/spegel-org/spegel/pull/556) Add configuration for revisionHistoryLimit in the Helm Chart.
- [#573](https://github.com/spegel-org/spegel/pull/573) Use buffer pool for proxy copying data.

### Changed

Expand Down
26 changes: 26 additions & 0 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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) {
//nolint: staticcheck // false positive
p.pool.Put(b)
}
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 @@ -28,6 +29,7 @@ const (
)

type Registry struct {
bufferPool *buffer.BufferPool
log logr.Logger
throttler *throttle.Throttler
ociClient oci.Client
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 9126f7f

Please sign in to comment.