-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
syeopite
wants to merge
3
commits into
iv-org:refactor-connection-pool
Choose a base branch
from
syeopite:client-rework
base: refactor-connection-pool
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syeopite
commented
Apr 10, 2025
end | ||
end | ||
|
||
class HTTPClient < HTTP::Client |
There was a problem hiding this comment.
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
ccbbc45
to
6ff616d
Compare
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pe
2CD5
nding reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 logicThe 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