Skip to content

Commit

Permalink
Use make_client instead of calling HTTP::Client (#4709)
Browse files Browse the repository at this point in the history
No related issue
  • Loading branch information
SamantazFox committed Nov 8, 2024
2 parents 792d0d5 + 6e39b9b commit 42da254
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/invidious/routes/api/v1/search.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ module Invidious::Routes::API::V1::Search
query = env.params.query["q"]? || ""

begin
client = HTTP::Client.new("suggestqueries-clients6.youtube.com")
client.before_request { |r| add_yt_headers(r) }

client = make_client(URI.parse("https://suggestqueries-clients6.youtube.com"), force_youtube_headers: true)
url = "/complete/search?client=youtube&hl=en&gl=#{region}&q=#{URI.encode_www_form(query)}&gs_ri=youtube&ds=yt"

response = client.get(url).body
Expand Down
19 changes: 7 additions & 12 deletions src/invidious/yt_backend/connection_pool.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ struct YoutubeConnectionPool
rescue ex
conn.close

conn = HTTP::Client.new(url)
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
conn.family = CONFIG.force_resolve
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
conn = make_client(url, force_resolve: true)

response = yield conn
ensure
pool.release(conn)
Expand All @@ -38,11 +35,7 @@ struct YoutubeConnectionPool

private def build_pool
DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
conn = HTTP::Client.new(url)
conn.family = CONFIG.force_resolve
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
conn
next make_client(url, force_resolve: true)
end
end
end
Expand All @@ -62,15 +55,17 @@ def add_yt_headers(request)
end
end

def make_client(url : URI, region = nil, force_resolve : Bool = false)
def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_headers : Bool = false)
client = HTTP::Client.new(url)
client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy

# Force the usage of a specific configured IP Family
if force_resolve
client.family = CONFIG.force_resolve
client.family = Socket::Family::INET if client.family == Socket::Family::UNSPEC
end

client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
client.before_request { |r| add_yt_headers(r) } if url.host.try &.ends_with?("youtube.com") || force_youtube_headers
client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds

Expand Down

0 comments on commit 42da254

Please sign in to comment.