From 3700968623f7e5c576cca26296d08085649b8886 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 2 Jun 2020 20:06:12 +0300 Subject: [PATCH 1/3] Limit number of running transformation threads --- imageproxy.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/imageproxy.go b/imageproxy.go index 6721056ea..a78f2b6f4 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -31,6 +31,7 @@ import ( "net/http" "net/url" "path" + "runtime" "strings" "time" @@ -113,6 +114,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 ...interface{}) { if proxy.Verbose { proxy.logf(format, v...) @@ -427,6 +429,8 @@ type TransformingTransport struct { // responses are properly cached. CachingClient *http.Client + limiter chan struct{} + log func(format string, v ...interface{}) } @@ -440,6 +444,13 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er return t.Transport.RoundTrip(req) } + if t.limiter != nil { + t.limiter <- struct{}{} + defer func() { + <-t.limiter + }() + } + f := req.URL.Fragment req.URL.Fragment = "" resp, err := t.CachingClient.Do(req) From 3b325b0f9899ec59696b7260637b60d5dea5ed80 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 2 Jun 2020 20:08:03 +0300 Subject: [PATCH 2/3] Add a metric "http_requests_in_flight" --- imageproxy.go | 7 ++++++- metrics.go | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/imageproxy.go b/imageproxy.go index a78f2b6f4..5c253dae6 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -153,7 +153,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) } diff --git a/metrics.go b/metrics.go index e2d870a13..d77d9a35d 100644 --- a/metrics.go +++ b/metrics.go @@ -26,6 +26,11 @@ 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() { @@ -33,4 +38,5 @@ func init() { prometheus.MustRegister(metricServedFromCache) prometheus.MustRegister(metricRemoteErrors) prometheus.MustRegister(metricRequestDuration) + prometheus.MustRegister(metricRequestsInFlight) } From b2a9772484aaa65353330e28be112b386c0b0185 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 5 Jun 2020 01:52:35 +0300 Subject: [PATCH 3/3] Test the limiter in TestTransformingTransport --- imageproxy_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/imageproxy_test.go b/imageproxy_test.go index 33267f8f2..771eb885d 100644 --- a/imageproxy_test.go +++ b/imageproxy_test.go @@ -492,6 +492,7 @@ func TestTransformingTransport(t *testing.T) { tr := &TransformingTransport{ Transport: testTransport{}, CachingClient: client, + limiter: make(chan struct{}, 1), } client.Transport = tr