Skip to content

Limit number of the transformations threads#235

Closed
striker2000 wants to merge 3 commits into
willnorris:mainfrom
striker2000:master
Closed

Limit number of the transformations threads#235
striker2000 wants to merge 3 commits into
willnorris:mainfrom
striker2000:master

Conversation

@striker2000
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown

codecov Bot commented Jun 4, 2020

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.34%. Comparing base (3c7d08f) to head (b2a9772).
Report is 140 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@striker2000
Copy link
Copy Markdown
Contributor Author

@googlebot I signed it!

@googlebot
Copy link
Copy Markdown
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@willnorris
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown

@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