From 63a15aaa15e7d900b4e8d4dbfa8b560e58a5ed1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sl=C3=A1vek=20Banko?= Date: Mon, 28 Oct 2019 01:05:10 +0100 Subject: [PATCH] Adjustments for detecting mirrors status + Curl-multi is used for asynchronous testing of mirrors. + Timeout for curl requests increased to 10 seconds. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Slávek Banko --- mirrorstatus.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/mirrorstatus.php b/mirrorstatus.php index b248140..12f2030 100644 --- a/mirrorstatus.php +++ b/mirrorstatus.php @@ -27,6 +27,8 @@ $redis = new Redis(); $redis->connect('127.0.0.1', 6379, 2); + $ch = array(); + $cmh = curl_multi_init(); foreach( $mirrors as $mirrorName => $mirrorInfo ) { $savedResult = $redis->isConnected() ? $redis->get('mirrorStatus-'.$mirrorName) : false; @@ -36,14 +38,29 @@ } else { - $cs = curl_init(); - curl_setopt( $cs, CURLOPT_URL, $mirrorInfo['url'].'-synctime' ); - curl_setopt( $cs, CURLOPT_TIMEOUT, 3 ); - curl_setopt( $cs, CURLOPT_HEADER, true ); - curl_setopt( $cs, CURLOPT_FOLLOWLOCATION, true ); - curl_setopt( $cs, CURLOPT_RETURNTRANSFER, true ); - $ret = curl_exec( $cs ); - curl_close( $cs ); + $ch[$mirrorName] = curl_init(); + curl_setopt( $ch[$mirrorName], CURLOPT_URL, $mirrorInfo['url'].'-synctime' ); + curl_setopt( $ch[$mirrorName], CURLOPT_TIMEOUT, 10 ); + curl_setopt( $ch[$mirrorName], CURLOPT_HEADER, true ); + curl_setopt( $ch[$mirrorName], CURLOPT_FOLLOWLOCATION, true ); + curl_setopt( $ch[$mirrorName], CURLOPT_RETURNTRANSFER, true ); + curl_multi_add_handle( $cmh, $ch[$mirrorName] ); + } + } + + if(!empty($ch)) + { + $running = null; + do + { + curl_multi_exec($cmh, $running); + } + while($running); + + foreach( $ch as $mirrorName => $cs ) + { + $ret = curl_multi_getcontent( $cs ); + curl_multi_remove_handle( $cmh, $cs ); if(!empty( $ret )) { if(!preg_match('#(^|\n)HTTP/1.1 200 OK#', $ret)) @@ -67,6 +84,8 @@ } } } + curl_multi_close($cmh); + $redis->close(); foreach( $mirrors as $mirrorName => $mirrorInfo ) { @@ -107,7 +126,6 @@ } } } - $redis->close(); if(!isset($_GET['mr'])) {