From 90a09b98b4f25810e1e32d961be56bf332c7e538 Mon Sep 17 00:00:00 2001 From: Andrii Nikitin Date: Fri, 6 Jun 2025 17:35:46 +0200 Subject: [PATCH] Reduce limit of mirrors in folder mirrorlist --- lib/MirrorCache/Config.pm | 4 +++- lib/MirrorCache/Schema/ResultSet/Server.pm | 2 +- lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm | 8 ++++++-- t/environ/03-geo.sh | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/MirrorCache/Config.pm b/lib/MirrorCache/Config.pm index d339ca5f..b2f0fb23 100644 --- a/lib/MirrorCache/Config.pm +++ b/lib/MirrorCache/Config.pm @@ -59,6 +59,8 @@ has custom_footer_message => $ENV{MIRRORCACHE_CUSTOM_FOOTER_MESSAGE}; has browser_agent_mask => $ENV{MIRRORCACHE_BROWSER_AGENT_MASK} // '(?i)(firefox|msie|chrom|safari|seamonkey|opera|opr|trident).*'; has country_image_dir => $ENV{MIRRORCACHE_COUNTRY_IMAGE_DIR} // '/srv/www/htdocs/webalizer/flags/'; +has limit_mirrorlist_folder => int($ENV{MIRRORCACHE_LIMIT_MIRRORLIST_FOLDER} // 8); + has geoip => undef; sub init($self, $cfgfile) { @@ -67,7 +69,7 @@ sub init($self, $cfgfile) { my $cfg; $cfg = Config::IniFiles->new(-file => $cfgfile, -fallback => 'default') if $cfgfile; if ($cfg) { - for my $k (qw/root root_nfs redirect redirect_huge huge_file_size small_file_size city_mmdb ip2location top_folders workers regions mirror_provider browser_agent_mask custom_footer_message country_image_dir vpn_prefix/) { + for my $k (qw/root root_nfs redirect redirect_huge huge_file_size small_file_size city_mmdb ip2location top_folders workers regions mirror_provider browser_agent_mask custom_footer_message country_image_dir vpn_prefix limit_mirrorlist_folder/) { if (my $v = $cfg->val('default', $k)) { $self->$k($v); } diff --git a/lib/MirrorCache/Schema/ResultSet/Server.pm b/lib/MirrorCache/Schema/ResultSet/Server.pm index b1e5813c..5e669304 100644 --- a/lib/MirrorCache/Schema/ResultSet/Server.pm +++ b/lib/MirrorCache/Schema/ResultSet/Server.pm @@ -109,7 +109,7 @@ sub mirrors_query { my $folder_cond = "fd.folder_id in (coalesce((select id from folder where path = concat(?::text,'/repodata')),?),?) and (fdf.file_id is NULL and fl.folder_id in (coalesce((select id from folder where path = concat(?::text,'/repodata')),?),?))"; my $where_recent = "where s.mtime > 0"; # license.tar* and info.xml* might be kept with the same name through updates, so timestamp on them is unreliable in mirrorlist for folders - my $file_dt = ", max(case when fdf.file_id is null and fl.name ~ '[0-9]' and fl.name not like '%license.tar.%' and fl.name not like '%info.xml.%' and fl.name not like '%.asc' and fl.name not like '%.txt' and fl.name not like '%/' and fl.name not like 'yast2%' and fl.name not like '%.pf2' and fl.name not like '%patterns.xml.zst' and fl.name not like '%susedata%xml%' and fl.name not like '%appdata-icons%' then fl.mtime else null end) as mtime"; + my $file_dt = ", max(case when fdf.file_id is null and fl.name ~ '[0-9]' and fl.name not like '%license.tar.%' and fl.name not like '%info.xml.%' and fl.name not like '%.asc' and fl.name not like '%.txt' and fl.name not like '%/' and fl.name not like 'yast2%' and fl.name not like '%.pf2' and fl.name not like '%patterns.xml.zst' and fl.name not like '%data%xml%' and fl.name not like '%appdata-icons%' then fl.mtime else null end) as mtime"; my $group_by = "group by s.id, s.hostname, s.hostname_vpn, s.urldir, s.region, s.country, s.lat, s.lng, s.score, fd.folder_id"; if ($file_id) { diff --git a/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm b/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm index e973649b..5752313d 100644 --- a/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm +++ b/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm @@ -44,9 +44,13 @@ sub register { return $c->render(status => 404, text => "Folder not found"); return 1; } + my $limit = 8; + eval { + $limit = $app->mcconfig->limit_mirrorlist_folder // $limit; + }; my (@mirrors_country, @mirrors_region, @mirrors_rest); my $project_id = $c->mcproject->get_id($path); - my $cnt = _collect_mirrors($dm, \@mirrors_country, \@mirrors_region, \@mirrors_rest, undef, undef, $folder_id, $project_id, undef, undef, 32); + my $cnt = _collect_mirrors($dm, \@mirrors_country, \@mirrors_region, \@mirrors_rest, undef, undef, $folder_id, $project_id, undef, undef, $limit); return $c->render(status => 204, text => 'No mirrors found') unless $cnt; my @mirrors; @@ -818,7 +822,7 @@ sub _collect_mirrors { } } - if ($found_count < $limit) { + if ($found_count < $limit && $file_id) { $m = $rs->mirrors_query( $country, $region, $realfolder_id, $folder_id, $file_id, $realproject_id, $project_id, $scheme, $ipv, $lat, $lng, $avoid_countries, $limit, 1, diff --git a/t/environ/03-geo.sh b/t/environ/03-geo.sh index a2ff2404..7f5f2c8b 100755 --- a/t/environ/03-geo.sh +++ b/t/environ/03-geo.sh @@ -76,6 +76,6 @@ $mc/curl -H "Accept: */*, application/metalink+xml" --interface 127.0.0.2 -s /do $mc/curl -H "Accept: */*, application/metalink4+xml" --interface 127.0.0.2 -s /download/folder1/file1.1.dat?COUNTRY=fr | grep -B20 127.0.0.3 echo check metalink for folder -$mc/curl -i /download/folder1/?mirrorlist | grep -F '"url":"http:\/\/127.0.0.2:1304\/folder1\/"' | grep -F '"url":"http:\/\/127.0.0.3:1314\/folder1\/"' +$mc/curl -i /download/folder1/?mirrorlist | grep -F '"url":"http:\/\/127.0.0.2:1304\/folder1\/"' # | grep -F '"url":"http:\/\/127.0.0.3:1314\/folder1\/"'