Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion imageproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/url"
"path"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -125,6 +126,7 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
Transport: &TransformingTransport{
Transport: transport,
CachingClient: client,
limiter: make(chan struct{}, runtime.NumCPU()),
log: func(format string, v ...any) {
if proxy.Verbose {
proxy.logf(format, v...)
Expand Down Expand Up @@ -221,7 +223,12 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

timer := prometheus.NewTimer(metricRequestDuration)
defer timer.ObserveDuration()
metricRequestsInFlight.Inc()
defer func() {
timer.ObserveDuration()
metricRequestsInFlight.Dec()
}()

h.ServeHTTP(w, r)
}

Expand Down Expand Up @@ -557,6 +564,9 @@ type TransformingTransport struct {
// responses are properly cached.
CachingClient *http.Client

// limiter limits the number of concurrent transformations being processed.
limiter chan struct{}

log func(format string, v ...any)

updateCacheHeaders func(hdr http.Header)
Expand Down Expand Up @@ -598,6 +608,15 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
}, 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)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions imageproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ func TestTransformingTransport(t *testing.T) {
tr := &TransformingTransport{
Transport: &testTransport{},
CachingClient: client,
limiter: make(chan struct{}, 1),
}
client.Transport = tr

Expand Down
6 changes: 6 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ var (
Name: "request_duration_seconds",
Help: "Request response times",
})
metricRequestsInFlight = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "http",
Name: "requests_in_flight",
Help: "Number of requests in flight",
})
)

func init() {
prometheus.MustRegister(metricTransformationDuration)
prometheus.MustRegister(metricServedFromCache)
prometheus.MustRegister(metricRemoteErrors)
prometheus.MustRegister(metricRequestDuration)
prometheus.MustRegister(metricRequestsInFlight)
}
Loading