From 58394533a75a616e36755c514ffe686f537b593e Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Fri, 26 Apr 2013 10:49:17 -0400 Subject: [PATCH] "Queue Compression" -- let one do the work for all identical, concurrent searches Signed-off-by: Mike Rylander Signed-off-by: Jason Etheridge --- .../perlmods/lib/OpenILS/Application/Search/Biblio.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm index 6d149cabbb..3975edae13 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm @@ -1293,6 +1293,23 @@ sub staged_search { my $facet_key = $key.'_facets'; my $cache_data = $cache->get_cache($key) || {}; + # First, we want to make sure that someone else isn't currently trying to perform exactly + # this same search. The point is to allow just one instance of a search to fill the needs + # of all concurrent, identical searches. This will avoid spammy searches killing the + # database without requiring admins to start locking some IP addresses out entirely. + # + # There's still a tiny race condition where 2 might run, but without sigificantly more code + # and complexity, this is close to the best we can do. + + if ($cache_data->{running}) { # someone is already doing the search... + while ( sleep(1) ) { # sleep for a second ... maybe they'll finish + $cache_data = $cache->get_cache($key) || {}; + last if (!$cache_data->{running}); + } + } else { # we're the first ... let's give it a try + $cache->put_cache($key, { running => $$ }, $cache_timeout / 3); + } + # keep retrieving results until we find enough to # fulfill the user-specified limit and offset my $all_results = []; -- 2.11.0