From 22b76bf20bf48ca6673235f80aed63a51769154c Mon Sep 17 00:00:00 2001 From: syeopite Date: Thu, 10 Apr 2025 01:02:59 -0700 Subject: [PATCH] Disable auto reconnect for companion pool clients --- src/invidious.cr | 2 +- src/invidious/connection/client.cr | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 0ae785df..4c2af431 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -115,7 +115,7 @@ COMPANION_POOL = Invidious::ConnectionPool::Pool.new( reinitialize_proxy: false ) do companion = CONFIG.invidious_companion.sample - next make_client(companion.private_url, use_http_proxy: false) + next make_client(companion.private_url, use_http_proxy: false, allow_auto_reconnect: false) end # CLI diff --git a/src/invidious/connection/client.cr b/src/invidious/connection/client.cr index 4a0cbade..1a49b09b 100644 --- a/src/invidious/connection/client.cr +++ b/src/invidious/connection/client.cr @@ -12,6 +12,15 @@ module Invidious end class HTTPClient < HTTP::Client + def initialize(uri : URI, tls : TLSContext = nil, allow_auto_reconnect : Bool = true) + tls = HTTP::Client.tls_flag(uri, tls) + host = HTTP::Client.validate_host(uri) + + super(host, uri.port, tls) + + @reconnect = allow_auto_reconnect + end + def initialize(uri : URI, tls : TLSContext = nil, force_resolve : Socket::Family = Socket::Family::UNSPEC) tls = HTTP::Client.tls_flag(uri, tls) @@ -72,14 +81,21 @@ def add_yt_headers(request) end end -def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_headers : Bool = true, use_http_proxy : Bool = true) +def make_client( + url : URI, + region = nil, + force_resolve : Bool = false, + force_youtube_headers : Bool = true, + use_http_proxy : Bool = true, + allow_auto_reconnect : Bool = true, +) if CONFIG.http_proxy && use_http_proxy client = Invidious::HTTPClient.new(url) client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy && use_http_proxy elsif force_resolve client = Invidious::HTTPClient.new(url, force_resolve: CONFIG.force_resolve) else - client = Invidious::HTTPClient.new(url) + client = Invidious::HTTPClient.new(url, allow_auto_reconnect: allow_auto_reconnect) end client.before_request { |r| add_yt_headers(r) } if url.host.try &.ends_with?("youtube.com") || force_youtube_headers