8000 Rework `HTTP::Client` logic by syeopite · Pull Request #5235 · iv-org/invidious · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rework HTTP::Client logic #5235

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

Draft
wants to merge 3 commits into
base: refactor-connection-pool
Choose a base branch
from

Conversation

syeopite
Copy link
Member
@syeopite syeopite commented Apr 10, 2025

Depends on #5081

I'm making this pull request target a branch that has the same commit history as #5081 to make it easier to review


This PR replaces the current force resolve implementation which patches stdlib to one that is based around a custom IO.

In addition to accommodate the lack of the automatic reconnect feature the connection pool now uses DB::Pool#retry

DB::Pool#retry allows us to cycle until we find a client that that still has an open connection, or to create a new one if necessary. The poisoned clients are then removed once identified by #retry's internal logic

The fact that clients with closed connections will now be slowly removed also means that Invidious will no longer use the same pattern of companion connections within the pool. The closed connections will cause new connections that will each select a random companion to connect to

Closes #5230
Closes #4662

@syeopite syeopite requested a review from a team as a code owner April 10, 2025 08:09
@syeopite syeopite requested review from SamantazFox and removed request for a team April 10, 2025 08:09
end
end

class HTTPClient < HTTP::Client
Copy link
Member Author

Choose a reason for hiding this comment

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

This does subclasses HTTP::Client but its more for the convenience of using the two protected class methods rather than any actual necessity

@syeopite syeopite marked this pull request as draft April 10, 2025 08:38
@syeopite syeopite force-pushed the refactor-connection-pool branch from ccbbc45 to 6ff616d Compare May 13, 2025 12:20
syeopite added 3 commits May 13, 2025 06:23
Implement force resolve via IO constructor

Use `DB::Pool#retry` in connection pool

`HTTP::Clients` created through its IO constructor cannot reconnect to
closed connections, and instead will result in an error on usage.

The `DB::Pool#retry` allows us to cycle until we find a client that
that still has an open connection, or to create a new one
if necessary. The poisoned clients are then removed once identified
by `#retry`'s internal logic

The fact that clients with closed connections will now be slowly removed
also means that Invidious will no longer use the same pattern of companion
connections within the pool; distributing routes more evenly.
The standard Crystal `HTTP::Client` creates a new SSL context for every
client/reconnection.  This is not ideal because creating a new SSL
context is an incredibly slow operation.

The standard practice instead is to cache or use a single SSL CTX
throughout the lifetime of the application. The OpenSSL context has been
designed with this in mind, and even explicitly recommends it. It even
works concurrently across many threads.

Reusing a single SSL context significantly speeds up the process of
starting and restarting clients. Which is incredibly important in the
case of the Invidious connection pool where new clients are often spun
up.

The only caveat is that a cached SSL context may become out of sync with
the system ca store. But for an Invidious instance following the best
practices of frequent restarts this is an non-issue.

See crystal-lang/crystal#15419
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.

1 participant
0