Skip to content

Commit

Permalink
Short-circuit correctly in search_expanded
Browse files Browse the repository at this point in the history
The total will always be the number of documents matched, not the number
of documents returned for the given page.  Instead, do what
search_collapsed does and check if we have any distributions.

Additionally, guard search_descriptions against an empty id list, which
produces invalid queries.

I found these while tracking down the cause of thousands of 500s on
/file/_search, ultimately being triggered by a web crawler.
  • Loading branch information
tsibley committed Nov 18, 2016
1 parent a05ffb7 commit 5e259db
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/MetaCPAN/Web/Model/API/Module.pm
Expand Up @@ -80,15 +80,15 @@ sub search_expanded {
}
)->recv;

my @distributions = uniq
map { $_->{fields}->{distribution} } @{ $data->{hits}->{hits} };

# Everything after this will fail (slowly and silently) without results.
if ( !$data->{hits}->{total} ) {
unless (@distributions) {
$cv->send( {} );
return $cv;
}

my @distributions = uniq
map { $_->{fields}->{distribution} } @{ $data->{hits}->{hits} };

my @ids = map { $_->{fields}->{id} } @{ $data->{hits}->{hits} };
my $descriptions = $self->search_descriptions(@ids);
my $ratings = $self->model('Rating')->get(@distributions);
Expand Down Expand Up @@ -167,6 +167,12 @@ sub search_collapsed {
sub search_descriptions {
my ( $self, @ids ) = @_;
my $cv = $self->cv;

unless (@ids) {
$cv->send( {} );
return $cv;
}

$self->request(
'/file/_search',
{
Expand Down

0 comments on commit 5e259db

Please sign in to comment.