-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Refactor connection pool #5081
Open
syeopite
wants to merge
15
commits into
iv-org:master
Choose a base branch
from
syeopite:connection-pool-improvements
base: master
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.
Open
Refactor connection pool #5081
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6ff4024
Add support for setting max idle http pool size
syeopite 7116603
Refactor pool checkout logic
syeopite b044727
Move client logic file to connection subfolder
syeopite c026a7b
Move ytimg pool logic to Invidious::ConnectionPool
syeopite 07caf93
Add config to set connection pool checkout timeout
syeopite 5d60356
Typo
syeopite e3313d3
Remove redundant pool.release
syeopite 39afb33
Delete broken clients from the pool explicitly
syeopite 540dfe2
Improve documentation of idle pool size
syeopite 75eb8b8
Connection pool: ensure response is fully read
syeopite 621c6ab
Release client only when it still exists
syeopite aab9719
Pool: Refactor logic for request methods
syeopite 6b3f665
Pool: remove redundant properties
syeopite 4c1db6e
Simplify namespace calls to ConnectionPool
syeopite bdcd9af
Update comment on reiniting proxy of pooled client
syeopite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
module Invidious::ConnectionPool | ||
struct Pool | ||
property url : URI | ||
property pool : DB::Pool(HTTP::Client) | ||
|
||
def initialize( | ||
url : URI, | ||
*, | ||
max_capacity : Int32 = 5, | ||
idle_capacity : Int32? = nil, | ||
timeout : Float64 = 5.0 | ||
) | ||
if idle_capacity.nil? | ||
idle_capacity = max_capacity | ||
end | ||
|
||
@url = url | ||
|
||
options = DB::Pool::Options.new( | ||
initial_pool_size: 0, | ||
max_pool_size: max_capacity, | ||
max_idle_pool_size: idle_capacity, | ||
checkout_timeout: timeout | ||
) | ||
|
||
@pool = DB::Pool(HTTP::Client).new(options) do | ||
next make_client(url, force_resolve: true) | ||
end | ||
end | ||
|
||
{% for method in %w[get post put patch delete head options] %} | ||
def {{method.id}}(*args, **kwargs, &) | ||
self.client do | client | | ||
client.{{method.id}}(*args, **kwargs) do | response | | ||
|
||
result = yield response | ||
return result | ||
|
||
ensure | ||
response.body_io?.try &. skip_to_end | ||
end | ||
end | ||
end | ||
|
||
def {{method.id}}(*args, **kwargs) | ||
{{method.id}}(*args, **kwargs) do | response | | ||
return response | ||
ensure | ||
response.body_io?.try &. skip_to_end | ||
end | ||
end | ||
{% end %} | ||
|
||
# Checks out a client in the pool | ||
private def client(&) | ||
# If a client has been deleted from the pool | ||
# we won't try to release it | ||
client_exists_in_pool = true | ||
|
||
http_client = pool.checkout | ||
|
||
# When the HTTP::Client connection is closed, the automatic reconnection | ||
# feature will create a new IO to connect to the server with | ||
# | ||
# This new TCP IO will be a direct connection to the server and will not go | ||
# through the proxy. As such we'll need to reinitialize the proxy connection | ||
|
||
http_client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy | ||
|
||
response = yield http_client | ||
rescue ex : DB::PoolTimeout | ||
# Failed to checkout a client | ||
raise ConnectionPool::Error.new(ex.message, cause: ex) | ||
rescue ex | ||
# An error occurred with the client itself. | ||
# Delete the client from the pool and close the connection | ||
if http_client | ||
client_exists_in_pool = false | ||
@pool.delete(http_client) | ||
http_client.close | ||
end | ||
|
||
# Raise exception for outer methods to handle | ||
raise ConnectionPool::Error.new(ex.message, cause: ex) | ||
ensure | ||
pool.release(http_client) if http_client && client_exists_in_pool | ||
end | ||
end | ||
|
||
class Error < Exception | ||
end | ||
|
||
# Mapping of subdomain => Invidious::ConnectionPool::Pool | ||
# This is needed as we may need to access arbitrary subdomains of ytimg | ||
private YTIMG_POOLS = {} of String => ConnectionPool::Pool | ||
|
||
# Fetches a HTTP pool for the specified subdomain of ytimg.com | ||
# | ||
# Creates a new one when the specified pool for the subdomain does not exist | ||
def self.get_ytimg_pool(subdomain) | ||
if pool = YTIMG_POOLS[subdomain]? | ||
return pool | ||
else | ||
LOGGER.info("ytimg_pool: Creating a new HTTP pool for \"https://#{subdomain}.ytimg.com\"") | ||
pool = ConnectionPool::Pool.new( | ||
URI.parse("https://#{subdomain}.ytimg.com"), | ||
max_capacity: CONFIG.pool_size, | ||
idle_capacity: CONFIG.idle_pool_size, | ||
timeout: CONFIG.pool_checkout_timeout | ||
) | ||
YTIMG_POOLS[subdomain] = pool | ||
|
||
return pool | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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 pending 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.
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.
Is it even useful for us to add the cause of the ConnectionPool exception here when we know that the original error is a
DB::PoolTimeout
? All it does really is produce a lengthy backtrace that can potentially confuse users