feat: do all the backend balancing on the invidious side

This will make invidious easier to maintain and escalate without the need of an overcomplicated reverse proxy configuration and multiple invidious instances with each one with a different configuration (in this case, invidious companion)
This commit is contained in:
Fijxu
2025-03-30 20:08:15 -03:00
parent ddf6802d76
commit d47aa3dd6a
13 changed files with 341 additions and 265 deletions

View File

@@ -298,7 +298,7 @@ struct Video
predicate_bool upcoming, isUpcoming
end
def get_video(id, refresh = true, region = nil, force_refresh = false)
def get_video(id, refresh = true, region = nil, force_refresh = false, env : HTTP::Server::Context | Nil = nil)
if (video = Invidious::Database::Videos.select(id)) && !region
# If record was last updated over 10 minutes ago, or video has since premiered,
# refresh (expire param in response lasts for 6 hours)
@@ -308,7 +308,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
force_refresh ||
video.schema_version != Video::SCHEMA_VERSION # cache control
begin
video = fetch_video(id, region)
video = fetch_video(id, region, env)
Invidious::Database::Videos.insert(video)
rescue ex
Invidious::Database::Videos.delete(id)
@@ -316,7 +316,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
end
end
else
video = fetch_video(id, region)
video = fetch_video(id, region, env)
Invidious::Database::Videos.insert(video) if !region
end
@@ -324,11 +324,11 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
rescue DB::Error
# Avoid common `DB::PoolRetryAttemptsExceeded` error and friends
# Note: All DB errors inherit from `DB::Error`
return fetch_video(id, region)
return fetch_video(id, region, env)
end
def fetch_video(id, region)
info = extract_video_info(video_id: id)
def fetch_video(id, region, env)
info = extract_video_info(video_id: id, env: env)
if reason = info["reason"]?
if reason == "Video unavailable"