php - Set a timeout for both connect and read in file_get_contents -


prerequisites

cluster server

i have server runs number of virtual "cluster nodes". basically, each 1 of them public, minimal http server, has 2 functions:

127.0.0.1:xxxxn/ping 127.0.0.1:xxxxn/api/arg/arg/arg 

both of functions return 200ok header followed html. ping used test if node busy or not. if node n busy, connection time out (because node busy handling request). however, if node responsive , available, ping return

ping=node.free 

calling server

i want write php program can loaded on webserver expose better interface api. following code older api version had 1 node per ip, code quite simple:

<?php     error_reporting(0);     ini_set('display_errors', 0);     echo file_get_contents("http://127.0.0.1:xxxx9/".$_request['script']."/".$_request['query']."/".$_request['sort']."/".$_request['num']); ?> 

this was, of course, called like

http://server.org/api.php?script=api&query=... (etc.) 

and return results node #9 (hence xxxx9). there 10 nodes populating 1 server, 10 servers :xxxx0 :xxxx9.

the goal

i want implement php (i.e. webserver-side-) load balancing. basically, when called, php program should loop through nodes until 1 node return ping=node.free in less specified amount of time.

since use file_get_contents, read on ways set timeout this. turns out, there quite few ways, example using stream context, or curl option (instead of file_...). problem think facing each of options (presented in these answers here , here) affecting either connection or read.

what want check is, if in given time (say 1000ms per node), node connects and returns message. then, , only then, code should proceed call api @ node using above syntax. of course, if there no node avail. atm., should loop until 1 free.

now, best , safest way set , check timeout? if possible, i'd avoid fsock, because disabled managed webservers.

solved, using stream context method:

<?php error_reporting(0); ini_set('display_errors', 0);  $streamcontext = stream_context_create(array('http'=>     array(         'method' => 'get',         'timeout' => 1, // timeout in seconds (varies +- 1 sec or so)     ) ));  $ifreenode = -1;  while (true) {     ($i = 0; $i <= 9; $i++) {          $bnodeavailable = @file_get_contents('http://127.0.0.1:xxxx'.$i.'/ping', false, $streamcontext);          if ($bnodeavailable === false) {             //echo "node ".$i." busy.<br>";         } else {             if ($bnodeavailable = "ping=node.free") {                 //echo "node ".$i." available.<br>";                 $ifreenode = $i;                 break;             } else {                 //echo "node ".$i." busy.<br>";             }         }     }     if ($ifreenode > -1) { break; } } echo file_get_contents("http://127.0.0.1:xxxx".$ifreenode."/".$_request['script']."/".$_request['query']."/".$_request['sort']."/".$_request['num']); ?> 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -