Skip to content

Proxy: Use connection pools for images - #4326

Merged
SamantazFox merged 6 commits into
iv-org:masterfrom
syeopite:image-pool
Oct 30, 2024
Merged

SamantazFox merged 6 commits into
iv-org:masterfrom
syeopite:image-pool

Conversation

@syeopite

@syeopite syeopite commented Dec 9, 2023

Copy link
Copy Markdown
Member

Theoretically this should improve memory usage and performance by quite a bit as we aren't creating a new HTTP::Client and in a turn a new connection for every image we request from YouTube.

This should be tested extensively, especially in IPv6 only environment.

Closes #4009

@syeopite
syeopite requested a review from a team as a code owner December 9, 2023 02:46
@syeopite
syeopite requested review from unixfox and removed request for a team December 9, 2023 02:46
@unixfox

unixfox commented Dec 18, 2023

Copy link
Copy Markdown
Member

Can't test as I have an external proxy for that. But sounds good

Comment thread src/invidious.cr Outdated
return pool
else
LOGGER.info("ytimg_pool: Creating a new HTTP pool for \"https://#{subdomain}.ytimg.com\"")
pool = YoutubeConnectionPool.new(URI.parse("https://#{subdomain}.ytimg.com"), capacity: CONFIG.pool_size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about keeping that many open connections on small instances. Maybe we could bypass the pooling system if CONFIG.pool_size == 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the pool not automatically clean up connections?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so? It seems that released connections are pushed to the idle list, but I'm not sure what causes them to be closed:
https://github.com/crystal-lang/crystal-db/blob/3eaac85a5d4b7bee565b55dcb584e84e29fc5567/src/db/pool.cr#L151-L185

@syeopite syeopite Apr 25, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that's a problem...

Regarding the bypass though, shouldn't that already be what DB:Pool does when pool_size equals 0? Invidious seems to be able to handle requests fine when CONFIG.pool_size is set to zero at least

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YoutubeConnectionPool sets the max_pool_size and max_idle_pool_size to the value of capacity. So meaning the pool will start out with 0 clients, and create more up to capacity, but then it'll keep all of them around forever.

There is also not currently a way to set a timeout on idle connections. I.e. that would expire idle connections after some period of time. Is some discussion related to this in crystal-lang/crystal-db#47.

In the meantime it might be worth allowing to customize the max_idle_pool_size vs always assuming it should be CONFIG.pool_size big. E.g. have a higher max_pool_size to handle bursts, but then keep max_idle_pool_size set to something smaller for normal traffic. 1 might be a good starting point, could run some benchmarks to figure out a good value.

Comment thread src/invidious/routes/images.cr
Comment thread src/invidious/routes/images.cr
@syeopite

syeopite commented Jan 8, 2024

Copy link
Copy Markdown
Member Author

I wonder if something similar to get_ytimg_pool should also be used for all the googlevideo requests. We should probably be reducing all these one-off clients Invidious creates.

Comment thread src/invidious/routes/images.cr
Comment thread src/invidious/routes/images.cr Outdated
@SamantazFox

Copy link
Copy Markdown
Member

I wonder if something similar to get_ytimg_pool should also be used for all the googlevideo requests. We should probably be reducing all these one-off clients Invidious creates.

It would have to be somewhat different. There are so many video servers that keeping connections pools to every single one would be wasteful. But keeping the connection open per client (= almost for each video) would be nice, though I have no idea how to do that efficiently....

@SamantazFox SamantazFox added the need-testing This feature needs to be deployed and tested to see if it's working, and doesn't break something label Oct 8, 2024
@SamantazFox SamantazFox added ready and removed need-testing This feature needs to be deployed and tested to see if it's working, and doesn't break something labels Oct 29, 2024
@SamantazFox SamantazFox changed the title Use connection pools when requesting images from YouTube Proxy: Use connection pools for images Oct 30, 2024
@SamantazFox
SamantazFox merged commit 9957da2 into iv-org:master Oct 30, 2024
@SamantazFox

Copy link
Copy Markdown
Member

Thanks for your work :)

@syeopite
syeopite deleted the image-pool branch October 30, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Consider using pools for the various image requests to YouTube

4 participants