Add support for setting max idle http pool size

This commit is contained in:
syeopite 2024-11-11 15:24:01 -08:00
parent 9892604758
commit 6ff4024244
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
4 changed files with 49 additions and 9 deletions

View File

@ -145,7 +145,7 @@ https_only: false
#disable_proxy: false
##
## Size of the HTTP pool used to connect to youtube. Each
## Max size of the HTTP pool used to connect to youtube. Each
## domain ('youtube.com', 'ytimg.com', ...) has its own pool.
##
## Accepted values: a positive integer
@ -154,6 +154,19 @@ https_only: false
#pool_size: 100
##
## Idle size of the HTTP pool used to connect to youtube. Each
## domain ('youtube.com', 'ytimg.com', ...) has its own pool.
##
## When unset this value has the same value as pool_size
##
## Accepted values: a positive integer
## Default: <none> (internally this means that it has the same value as pool_size)
##
#idle pool_size: 100
##
## Additional cookies to be sent when requesting the youtube API.
##

View File

@ -91,11 +91,15 @@ SOFTWARE = {
"branch" => "#{CURRENT_BRANCH}",
}
YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
YT_POOL = YoutubeConnectionPool.new(YT_URL, max_capacity: CONFIG.pool_size, idle_capacity: CONFIG.idle_pool_size)
# Image request pool
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
GGPHT_POOL = YoutubeConnectionPool.new(
URI.parse("https://yt3.ggpht.com"),
max_capacity: CONFIG.pool_size,
idle_capacity: CONFIG.idle_pool_size
)
# CLI
Kemal.config.extra_options do |parser|

View File

@ -138,8 +138,12 @@ class Config
property port : Int32 = 3000
# Host to bind (overridden by command line argument)
property host_binding : String = "0.0.0.0"
# Pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool of `pool_size`)
# Max pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool)
property pool_size : Int32 = 100
# Idle pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool)
property idle_pool_size : Int32? = nil
# HTTP Proxy configuration
property http_proxy : HTTPProxyConfig? = nil

View File

@ -4,11 +4,24 @@ private YTIMG_POOLS = {} of String => YoutubeConnectionPool
struct YoutubeConnectionPool
property! url : URI
property! capacity : Int32
property! max_capacity : Int32
property! idle_capacity : Int32
property! timeout : Float64
property pool : DB::Pool(HTTP::Client)
def initialize(url : URI, @capacity = 5, @timeout = 5.0)
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
@ -33,10 +46,12 @@ struct YoutubeConnectionPool
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: capacity,
max_idle_pool_size: capacity,
max_pool_size: max_capacity,
max_idle_pool_size: idle_capacity,
checkout_timeout: timeout
)
@ -108,7 +123,11 @@ def get_ytimg_pool(subdomain)
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)
pool = YoutubeConnectionPool.new(
URI.parse("https://#{subdomain}.ytimg.com"),
max_capacity: CONFIG.pool_size,
idle_capacity: CONFIG.idle_pool_size
)
YTIMG_POOLS[subdomain] = pool
return pool