Use HTTP pools for image requests to YouTube

This commit is contained in:
syeopite 2023-12-08 18:20:17 -08:00
parent c5fdd9ea65
commit 480e073fa9
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
3 changed files with 28 additions and 7 deletions

View File

@ -92,6 +92,14 @@ SOFTWARE = {
YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size) YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
# Image request pool
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
# Mapping of subdomain => YoutubeConnectionPool
# This is needed as we may need to access arbitrary subdomains of ytimg
YTIMG_POOLS = {} of String => YoutubeConnectionPool
# CLI # CLI
Kemal.config.extra_options do |parser| Kemal.config.extra_options do |parser|
parser.banner = "Usage: invidious [arguments]" parser.banner = "Usage: invidious [arguments]"

View File

@ -32,7 +32,7 @@ module Invidious::Routes::Images
} }
begin begin
HTTP::Client.get("https://yt3.ggpht.com#{url}") do |resp| GGPHT_POOL.client &.get(url) do |resp|
return request_proc.call(resp) return request_proc.call(resp)
end end
rescue ex rescue ex
@ -80,7 +80,7 @@ module Invidious::Routes::Images
} }
begin begin
HTTP::Client.get("https://#{authority}.ytimg.com#{url}") do |resp| get_ytimg_pool(authority).client &.get(url) do |resp|
return request_proc.call(resp) return request_proc.call(resp)
end end
rescue ex rescue ex
@ -119,7 +119,7 @@ module Invidious::Routes::Images
} }
begin begin
HTTP::Client.get("https://i9.ytimg.com#{url}") do |resp| get_ytimg_pool("i9").client &.get(url) do |resp|
return request_proc.call(resp) return request_proc.call(resp)
end end
rescue ex rescue ex
@ -165,8 +165,7 @@ module Invidious::Routes::Images
if name == "maxres.jpg" if name == "maxres.jpg"
build_thumbnails(id).each do |thumb| build_thumbnails(id).each do |thumb|
thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg" thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg"
# This can likely be optimized into a (small) pool sometime in the future. if get_ytimg_pool("i9").client &.head(thumbnail_resource_path).status_code == 200
if HTTP::Client.head("https://i.ytimg.com#{thumbnail_resource_path}").status_code == 200
name = thumb[:url] + ".jpg" name = thumb[:url] + ".jpg"
break break
end end
@ -199,8 +198,7 @@ module Invidious::Routes::Images
} }
begin begin
# This can likely be optimized into a (small) pool sometime in the future. get_ytimg_pool("i").client &.get(url) do |resp|
HTTP::Client.get("https://i.ytimg.com#{url}") do |resp|
return request_proc.call(resp) return request_proc.call(resp)
end end
rescue ex rescue ex

View File

@ -77,3 +77,18 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false, &)
client.close client.close
end end
end end
# 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 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 = YoutubeConnectionPool.new(URI.parse("https://#{subdomain}.ytimg.com"), capacity: CONFIG.pool_size)
YTIMG_POOLS[subdomain] = pool
return pool
end
end