Skip to content

Commit 453f770

Browse files
committed
limit all pagination to 10000 results
Paged results are expensive to compute on Elasticsearch. It will refuse to give results if paged forward far enough. Limit our pagination and give empty results if paged too deep.
1 parent fbe6f1a commit 453f770

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

lib/MetaCPAN/Query/Favorite.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package MetaCPAN::Query::Favorite;
33
use MetaCPAN::Moose;
44

55
use MetaCPAN::ESConfig qw( es_doc_path );
6-
use MetaCPAN::Util qw(hit_total);
6+
use MetaCPAN::Util qw( MAX_RESULT_WINDOW hit_total );
77

88
with 'MetaCPAN::Query::Role::Common';
99

@@ -148,6 +148,14 @@ sub recent {
148148
$page //= 1;
149149
$size //= 100;
150150

151+
if ( $page * $size >= MAX_RESULT_WINDOW ) {
152+
return +{
153+
favorites => [],
154+
took => 0,
155+
total => 0,
156+
};
157+
}
158+
151159
my $favs = $self->es->search(
152160
es_doc_path('favorite'),
153161
body => {

lib/MetaCPAN/Query/Release.pm

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use MetaCPAN::Moose;
44

55
use MetaCPAN::ESConfig qw( es_doc_path );
66
use MetaCPAN::Util
7-
qw( hit_total single_valued_arrayref_to_scalar true false );
7+
qw( MAX_RESULT_WINDOW hit_total single_valued_arrayref_to_scalar true false );
88

99
with 'MetaCPAN::Query::Role::Common';
1010

@@ -399,6 +399,14 @@ sub by_author {
399399
$size //= 1000;
400400
$page //= 1;
401401

402+
if ( $page * $size >= MAX_RESULT_WINDOW ) {
403+
return {
404+
releases => [],
405+
took => 0,
406+
total => 0,
407+
};
408+
}
409+
402410
my $body = {
403411
query => {
404412
bool => {
@@ -491,6 +499,14 @@ sub all_by_author {
491499
$size //= 100;
492500
$page //= 1;
493501

502+
if ( $page * $size >= MAX_RESULT_WINDOW ) {
503+
return {
504+
releases => [],
505+
took => 0,
506+
total => 0,
507+
};
508+
}
509+
494510
my $body = {
495511
query => { term => { author => uc($author) } },
496512
sort => [ { date => 'desc' } ],
@@ -713,6 +729,14 @@ sub _get_depended_releases {
713729
$page //= 1;
714730
$page_size //= 50;
715731

732+
if ( $page * $page_size >= MAX_RESULT_WINDOW ) {
733+
return +{
734+
data => [],
735+
took => 0,
736+
total => 0,
737+
};
738+
}
739+
716740
$sort = _fix_sort_value($sort);
717741

718742
my $dependency_filter = {
@@ -773,17 +797,15 @@ sub recent {
773797
$page_size //= 10000;
774798
$type //= '';
775799

776-
my $query;
777-
my $from = ( $page - 1 ) * $page_size;
778-
779-
if ( $from + $page_size > 10000 ) {
780-
return {
800+
if ( $page * $page_size >= MAX_RESULT_WINDOW ) {
801+
return +{
781802
releases => [],
782-
total => 0,
783803
took => 0,
804+
total => 0,
784805
};
785806
}
786807

808+
my $query;
787809
if ( $type eq 'n' ) {
788810
$query = {
789811
constant_score => {

lib/MetaCPAN/Query/Search.pm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use List::Util qw( min uniq );
88
use Log::Contextual qw( :log :dlog );
99
use MetaCPAN::ESConfig qw( es_doc_path );
1010
use MetaCPAN::Types::TypeTiny qw( Object Str );
11-
use MetaCPAN::Util qw( hit_total true false );
11+
use MetaCPAN::Util qw( MAX_RESULT_WINDOW hit_total true false );
1212
use MooseX::StrictConstructor;
1313

1414
with 'MetaCPAN::Query::Role::Common';
@@ -60,6 +60,15 @@ sub search_web {
6060
$page_size //= 20;
6161
$page //= 1;
6262

63+
if ( $page * $page_size >= MAX_RESULT_WINDOW ) {
64+
return {
65+
results => [],
66+
total => 0,
67+
tool => 0,
68+
colapsed => $collapsed ? true : false,
69+
};
70+
}
71+
6372
$search_term =~ s{([+=><!&|\(\)\{\}[\]\^"~*?\\/])}{\\$1}g;
6473

6574
# munge the search_term

lib/MetaCPAN/Util.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ use Sub::Exporter -setup => {
3737
true
3838
false
3939
is_bool
40+
MAX_RESULT_WINDOW
4041
) ]
4142
};
4243

44+
use constant MAX_RESULT_WINDOW => 10000;
45+
4346
*true = \&Cpanel::JSON::XS::true;
4447
*false = \&Cpanel::JSON::XS::false;
4548
*is_bool = \&Cpanel::JSON::XS::is_bool;

0 commit comments

Comments
 (0)