mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-04-25 04:46:24 +02:00
Limit number of running transformation threads
This commit is contained in:
parent
731fa16921
commit
ba76dfa3b2
2 changed files with 15 additions and 0 deletions
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -125,6 +126,7 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
|
||||||
Transport: &TransformingTransport{
|
Transport: &TransformingTransport{
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
CachingClient: client,
|
CachingClient: client,
|
||||||
|
limiter: make(chan struct{}, runtime.NumCPU()),
|
||||||
log: func(format string, v ...any) {
|
log: func(format string, v ...any) {
|
||||||
if proxy.Verbose {
|
if proxy.Verbose {
|
||||||
proxy.logf(format, v...)
|
proxy.logf(format, v...)
|
||||||
|
|
@ -557,6 +559,9 @@ type TransformingTransport struct {
|
||||||
// responses are properly cached.
|
// responses are properly cached.
|
||||||
CachingClient *http.Client
|
CachingClient *http.Client
|
||||||
|
|
||||||
|
// limiter limits the number of concurrent transformations being processed.
|
||||||
|
limiter chan struct{}
|
||||||
|
|
||||||
log func(format string, v ...any)
|
log func(format string, v ...any)
|
||||||
|
|
||||||
updateCacheHeaders func(hdr http.Header)
|
updateCacheHeaders func(hdr http.Header)
|
||||||
|
|
@ -598,6 +603,15 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enforce limiter after we've checked if we can early return a 304 response,
|
||||||
|
// but before we read the response body and perform transformations.
|
||||||
|
if t.limiter != nil {
|
||||||
|
t.limiter <- struct{}{}
|
||||||
|
defer func() {
|
||||||
|
<-t.limiter
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
b, err := io.ReadAll(resp.Body)
|
b, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -735,6 +735,7 @@ func TestTransformingTransport(t *testing.T) {
|
||||||
tr := &TransformingTransport{
|
tr := &TransformingTransport{
|
||||||
Transport: &testTransport{},
|
Transport: &testTransport{},
|
||||||
CachingClient: client,
|
CachingClient: client,
|
||||||
|
limiter: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
client.Transport = tr
|
client.Transport = tr
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue