imageproxy icon indicating copy to clipboard operation
imageproxy copied to clipboard

Limit number of the transformations threads

Open striker2000 opened this issue 5 years ago • 7 comments

When a search bot requests many images at the same time and these images are not in the cache, all transformation goroutines start in parallel causing the huge memory allocation, that leads to the termination of the process by OOM.

This fix is adding the limiter in the TransformingTransport that limits the number of running transformation threads to the number of logical CPUs. The metric http_requests_in_flight is adding as well to monitor the length of the requests queue.

Fixes #200.

striker2000 avatar Jun 04 '20 22:06 striker2000

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar Jun 04 '20 22:06 googlebot

Codecov Report

Merging #235 into master will increase coverage by 0.17%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #235      +/-   ##
==========================================
+ Coverage   89.16%   89.34%   +0.17%     
==========================================
  Files           6        6              
  Lines         674      685      +11     
==========================================
+ Hits          601      612      +11     
  Misses         50       50              
  Partials       23       23              
Impacted Files Coverage Δ
imageproxy.go 85.00% <100.00%> (+0.55%) :arrow_up:
metrics.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 3c7d08f...b2a9772. Read the comment docs.

codecov[bot] avatar Jun 04 '20 22:06 codecov[bot]

@googlebot I signed it!

striker2000 avatar Jun 04 '20 22:06 striker2000

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar Jun 04 '20 22:06 googlebot

Hey @striker2000, thanks for this patch! Sorry it's taken a couple of week to get back to you.

I think this is certainly a good approach; my only reservation is about where exactly inside of TransformingTransport.RoundTrip that we block. I haven't had a chance to profile this code to know exactly where the out of memory issue in #200 is occurring. If it's inside the cache, then where you have the limiter check is probably right. But if it's actually happening during the transform step, then we probably want to block right before calling Transform. That way, the should304 call can still return 304 responses without tying up the limiter.

Does that make sense?

willnorris avatar Jun 19 '20 22:06 willnorris

We're seeing fairly frequent OOM's (~10 a day) which are likely related to transformations. Is anyone using this patch successfully?

mdkent avatar Apr 01 '21 22:04 mdkent

@mdkent thsi PR has a problem with inflight request.

I have updated with the current main branch and this PR. https://github.com/Jorgevillada/imageproxy

error copying response: readfrom tcp 127.0.0.1:8080->127.0.0.1:53150: write tcp 127.0.0.1:8080->127.0.0.1:53150: write: broken pipe

Some request never finish. image with main branch, the behavior with the ram is weird with invalid requests. image

Jorgevillada avatar Oct 30 '21 23:10 Jorgevillada