Skip to content

Commit 8500ebe

Browse files
striker2000willnorris
authored andcommitted
Limit number of running transformation threads
1 parent 731fa16 commit 8500ebe

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

imageproxy.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http"
2121
"net/url"
2222
"path"
23+
"runtime"
2324
"strings"
2425
"time"
2526

@@ -125,6 +126,7 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
125126
Transport: &TransformingTransport{
126127
Transport: transport,
127128
CachingClient: client,
129+
limiter: make(chan struct{}, runtime.NumCPU()),
128130
log: func(format string, v ...any) {
129131
if proxy.Verbose {
130132
proxy.logf(format, v...)
@@ -557,6 +559,8 @@ type TransformingTransport struct {
557559
// responses are properly cached.
558560
CachingClient *http.Client
559561

562+
limiter chan struct{}
563+
560564
log func(format string, v ...any)
561565

562566
updateCacheHeaders func(hdr http.Header)
@@ -576,6 +580,13 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
576580
return resp, err
577581
}
578582

583+
if t.limiter != nil {
584+
t.limiter <- struct{}{}
585+
defer func() {
586+
<-t.limiter
587+
}()
588+
}
589+
579590
f := req.URL.Fragment
580591
req.URL.Fragment = ""
581592
resp, err := t.CachingClient.Do(req)

imageproxy_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ func TestTransformingTransport(t *testing.T) {
735735
tr := &TransformingTransport{
736736
Transport: &testTransport{},
737737
CachingClient: client,
738+
limiter: make(chan struct{}, 1),
738739
}
739740
client.Transport = tr
740741

0 commit comments

Comments
 (0)