Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit number of the transformations threads #235

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

striker2000
Copy link

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.

@googlebot
Copy link
Collaborator

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).

📝 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.

@codecov
Copy link

codecov bot commented Jun 4, 2020

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%) ⬆️
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.

@striker2000
Copy link
Author

@googlebot I signed it!

@googlebot
Copy link
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@willnorris
Copy link
Owner

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 willnorris changed the base branch from master to main June 22, 2020 17:00
@mdkent
Copy link

mdkent commented Apr 1, 2021

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

@Jorgevillada
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

allow rate limiting number of concurrent requests
5 participants