-
-
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.
+198
−100
Open
Refactor connection pool #5081
Changes from 9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
module Invidious::ConnectionPool | ||
struct Pool | ||
property! url : URI | ||
property! max_capacity : Int32 | ||
property! idle_capacity : Int32 | ||
property! timeout : Float64 | ||
syeopite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
else | ||
@idle_capacity = idle_capacity | ||
end | ||
|
||
@url = url | ||
|
||
@pool = build_pool() | ||
end | ||
|
||
# Checks out a client in the pool | ||
def client(&) | ||
pool.checkout do |http_client| | ||
# Proxy needs to be reinstated every time we get a client from the pool | ||
http_client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy | ||
|
||
response = yield http_client | ||
|
||
return response | ||
rescue ex | ||
# Prevent broken client from being checked back into the pool | ||
pool.delete(http_client) | ||
raise ConnectionPool::Error.new(ex.message, cause: ex) | ||
end | ||
syeopite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rescue ex : DB::PoolTimeout | ||
# Failed to checkout a client | ||
raise ConnectionPool::Error.new(ex.message, cause: ex) | ||
end | ||
|
||
private def build_pool | ||
# We call the getter for the instance variables instead of using them directly | ||
# because the getters defined by property! ensures that the value is not a nil | ||
options = DB::Pool::Options.new( | ||
initial_pool_size: 0, | ||
max_pool_size: max_capacity, | ||
max_idle_pool_size: idle_capacity, | ||
checkout_timeout: timeout | ||
) | ||
|
||
DB::Pool(HTTP::Client).new(options) do | ||
next make_client(url, force_resolve: true) | ||
end | ||
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 => Invidious::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 = Invidious::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
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.
Oddly, the Crystal compiler didn't complain: there is a type mismatch between this property (
Int32
) andPool.timeout
(Float64
).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.
Oops I didn't notice that
But I think that's an intended feature of Crystal. The compiler allows you to use an integer in place of a float but not the other way around.