Optimized, fixed a flaw

This commit is contained in:
pezcurrel 2023-12-27 16:42:31 +01:00
parent 0ed8165a53
commit 540c9d4440

View file

@ -250,34 +250,21 @@ function crawl(&$list,$id) {
lecho(1,'working on «'.$inst.'»: round '.$id.', '.$i.'/'.$c.'; TET: '.ght($now-$tini,null,0).'; ETR of this round: '.ght($rtela/$i*$c-$rtela,null,0).'; using '.ghs(memory_get_usage(true)).' mem. (peak: '.ghs(memory_get_peak_usage(true)).'); '.count($insts).' instances responded; '.count($nlist).' instances in next round list; '.$newc.' new instance(s) found.'.N);
waituntilonline();
updexarr();
lecho(0,'trying to load «'.$inst.s peers...'.N);
lecho(1,'trying to load «'.$inst.s peers...'.N);
$peers=gurl('https://'.$inst.'/api/v1/instance/peers',$opts['timeout'],$opts['curltimeout']);
$cinsts[]=$inst;// don't need to check if in_array
$responded=0;
if ($peers['cont']===false) {
lecho(2,'could not load «'.$inst.s peers: '.$peers['emsg'].'.'.N);
} else {
$peers=@json_decode($peers['cont'],true);
if (!is_array($peers)) {
lecho(2,'loading «'.$inst.s peers, got not good JSON.'.N);
lecho(2,'loading «'.$inst.s peers, got bad JSON.'.N);
} else {
$cp=count($peers);
lecho(1,'successfully loaded «'.$inst.s peers ('.$cp.') :-)'.N);
$responded=1;
if (!in_array($inst,$insts)) {
lecho(1,'instance «'.$inst.'» responded :-)'.N);
$insts[]=$inst;
$res=myq($link,'SELECT ID FROM Instances WHERE URI=\''.myesc($link,$inst).'\'');
if (mysqli_num_rows($res)==0) {
lecho(1,'instance «'.$inst.'» is new :-)'.N);
myq($link,'INSERT INTO Instances SET URI=\''.myesc($link,$inst).'\', InsertTS='.time());
$newc++;
}
}
$cp--;
$pi=0;
$pi=1;
foreach ($peers as $key=>$peer) {
if ($key!=$pi) {
if ($key!=$pi-1) {
lecho(2,'«'.$inst.s peers: entity '.$pi.'/'.$cp.'s key is not sequential; not checking further.'.N);
break;
} elseif (!is_string($peer)) {
@ -302,6 +289,28 @@ function crawl(&$list,$id) {
}
}
}
$responded=false;
lecho(1,'trying to load «'.$inst.s instance info...'.N);
$instinfo=gurl('https://'.$inst.'/api/v1/instance/peers',$opts['timeout'],$opts['curltimeout']);
if ($instinfo['cont']===false) {
lecho(2,'could not load «'.$inst.s instance info: '.$instinfo['emsg'].'.'.N);
} else {
$instinfo=@json_decode($instinfo['cont'],true);
if (is_array($instinfo))
$responded=true;
else
lecho(2,'loading «'.$inst.s instance info, got bad JSON.'.N);
}
if ($responded && !in_array($inst,$insts)) {
lecho(1,'instance «'.$inst.'» responded :-)'.N);
$insts[]=$inst;
$res=myq($link,'SELECT ID FROM Instances WHERE URI=\''.myesc($link,$inst).'\'');
if (mysqli_num_rows($res)==0) {
lecho(1,'instance «'.$inst.'» is new :-)'.N);
myq($link,'INSERT INTO Instances SET URI=\''.myesc($link,$inst).'\', InsertTS='.$now);
$newc++;
}
}
$res=myq($link,'SELECT * FROM Peers WHERE Hostname=\''.myesc($link,$inst).'\'');
$nrows=mysqli_num_rows($res);
if ($nrows>0) {