summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsyeopite <syeopite@syeopite.dev>2021-05-15 20:15:09 -0700
committersyeopite <syeopite@syeopite.dev>2021-06-19 04:16:18 -0700
commit950c8f7104ca0973bc439e8e7707440869d0bb94 (patch)
tree0525769050046159eb2b616629321282fa6a21cb /src
parentb63bebb519c448df6576c7bd124f2ce7681cc203 (diff)
downloadinvidious-950c8f7104ca0973bc439e8e7707440869d0bb94.tar.gz
invidious-950c8f7104ca0973bc439e8e7707440869d0bb94.tar.bz2
invidious-950c8f7104ca0973bc439e8e7707440869d0bb94.zip
Enhance fetch_random_instance func
Handle cross-inst. redirect w/ broken health stats Add check for instance version in cross-redirect
Diffstat (limited to 'src')
-rw-r--r--src/invidious/helpers/utils.cr28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr
index cb94b44a..7c573aba 100644
--- a/src/invidious/helpers/utils.cr
+++ b/src/invidious/helpers/utils.cr
@@ -417,9 +417,31 @@ def fetch_random_instance
filtered_instance_list = [] of String
instance_list.as_a.each do |data|
if data[1]["type"] == "https"
- if data[1]["monitor"]
- health = data[1]["monitor"].as_h["dailyRatios"][0].as_h["ratio"]
- filtered_instance_list << data[0].as_s if health.to_s.to_f > 90
+ # Makes sure the instance isn't too outdated.
+ remote_version = data[1]["stats"]["software"]["version"]
+ remote_commit_date = remote_version.as_s.match(/\d{4}\.\d{2}\.\d{2}/)
+ next if !remote_commit_date
+ remote_commit_date = Time.parse(remote_commit_date[0], "%Y.%m.%d", Time::Location::UTC)
+ local_commit_date = Time.parse(CURRENT_VERSION, "%Y.%m.%d", Time::Location::UTC)
+
+ if (remote_commit_date - local_commit_date).abs.days <= 30
+ # as_nil? doesn't exist. Thus we'll have to handle the error rasied if
+ # as_nil fails.
+ begin
+ broken_health_monitoring = data[1]["monitor"].as_nil
+ broken_health_monitoring = true if broken_health_monitoring.nil?
+ rescue TypeCastError
+ broken_health_monitoring = false
+ end
+
+ if !broken_health_monitoring
+ health = data[1]["monitor"].as_h["dailyRatios"][0].as_h["ratio"]
+ filtered_instance_list << data[0].as_s if health.to_s.to_f > 90
+ else
+ # We can't check the health if the monitoring is broken. Thus we'll just add it to the list
+ # and move on
+ filtered_instance_list << data[0].as_s
+ end
end
end
end