fix: gateway timeout on calling fallback instances (merge pull request #16 from davidovski/fix_fallback)

Fix Gateway Timeout on fallback
This commit is contained in:
Ahwx 2023-08-20 12:21:38 +02:00 committed by GitHub
commit 82ac92a98b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View File

@ -26,7 +26,10 @@
switch ($type)
{
case 0:
require "engines/google/text.php";
$engine=$config->preferred_engines['text'];
if (is_null($engine))
$engine = "google";
require "engines/$engine/text.php";
$results = get_text_results($query, $page);
break;
case 1:

View File

@ -72,6 +72,13 @@
curl_multi_exec($mh, $running);
} while ($running);
if (curl_getinfo($google_ch)['http_code'] != '200')
{
require "engines/librex/text.php";
return get_librex_results($query, $page);
}
if ($special_search != 0)
{

View File

@ -169,6 +169,9 @@
function print_text_results($results)
{
if (empty($results))
return;
$special = $results[0];
if (array_key_exists("did_you_mean", $special))

View File

@ -4,6 +4,9 @@
{
global $config;
if (isset($_REQUEST["nfb"]) && $_REQUEST["nfb"] == "1")
return array();
if (!$config->instance_fallback)
return array();
@ -24,16 +27,12 @@
do {
$tries++;
// after "too many" requests, give up
if ($tries > 5)
return array();
$instance = array_pop($instances);
if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
continue;
$url = $instance . "api.php?q=$query_encoded&p=$page&t=0";
$url = $instance . "api.php?q=$query_encoded&p=$page&t=0&nfb=1";
$librex_ch = curl_init($url);
curl_setopt_array($librex_ch, $config->curl_settings);
@ -44,7 +43,10 @@
$code = curl_getinfo($librex_ch)["http_code"];
$results = json_decode($response, true);
} while ( $results == null || count($results) <= 1);
} while ( !empty($instances) && ($results == null || count($results) <= 1));
if (empty($instances))
return array();
return array_values($results);
}