fix: fix wrong invidious companion logic on backends
All checks were successful
Invidious CI / build (push) Successful in 4m51s

do not change to another companion if request fails
This commit is contained in:
Fijxu 2025-04-01 22:03:52 -03:00
parent 5f1944925b
commit adc5c73853
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
3 changed files with 14 additions and 27 deletions

View file

@ -113,9 +113,11 @@ YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size) GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
COMPANION_POOL = CompanionConnectionPool.new( COMPANION_POOL = [] of CompanionConnectionPool
capacity: CONFIG.pool_size
) CONFIG.invidious_companion.each do |companion|
COMPANION_POOL << CompanionConnectionPool.new(companion, capacity: CONFIG.pool_size)
end
# CLI # CLI
Kemal.config.extra_options do |parser| Kemal.config.extra_options do |parser|

View file

@ -49,7 +49,7 @@ end
struct CompanionConnectionPool struct CompanionConnectionPool
property pool : DB::Pool(HTTP::Client) property pool : DB::Pool(HTTP::Client)
def initialize(capacity = 5, timeout = 5.0) def initialize(companion, capacity = 5, timeout = 5.0)
options = DB::Pool::Options.new( options = DB::Pool::Options.new(
initial_pool_size: 0, initial_pool_size: 0,
max_pool_size: capacity, max_pool_size: capacity,
@ -58,33 +58,13 @@ struct CompanionConnectionPool
) )
@pool = DB::Pool(HTTP::Client).new(options) do @pool = DB::Pool(HTTP::Client).new(options) 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)
end end
end end
def client(env : HTTP::Server::Context | Nil, &) def client(&)
conn = pool.checkout conn = pool.checkout
response = yield conn
begin
response = yield conn
rescue ex
conn.close
if env.nil?
companion = CONFIG.invidious_companion.sample
else
current_companion = env.get("current_companion").as(Int32)
companion = CONFIG.invidious_companion[current_companion]
end
conn = make_client(companion.private_url, use_http_proxy: false)
response = yield conn
ensure
pool.release(conn)
end
response response
end end
end end

View file

@ -688,7 +688,12 @@ module YoutubeAPI
# Send the POST request # Send the POST request
begin begin
response = COMPANION_POOL.client(env, &.post(endpoint, headers: headers, body: data.to_json)) if env.nil?
current_companion = rand(CONFIG.invidious_companion.size)
else
current_companion = env.get("current_companion").as(Int32)
end
response = COMPANION_POOL[current_companion].client &.post(endpoint, headers: headers, body: data.to_json)
body = response.body body = response.body
if (response.status_code != 200) if (response.status_code != 200)
raise Exception.new( raise Exception.new(