Skip to content

Instantly share code, notes, and snippets.

@yllus
Created September 21, 2015 15:35
Show Gist options
  • Select an option

  • Save yllus/da8907f5fddf4b72b8a9 to your computer and use it in GitHub Desktop.

Select an option

Save yllus/da8907f5fddf4b72b8a9 to your computer and use it in GitHub Desktop.
/**
* Check the responsiveness of a tcp/ip daemon
* @return (bool) true when $host:$post responds within $float_timeout seconds, else (bool) false
*/
function check_tcp_responsiveness( $host, $port, $float_timeout ) {
if ( function_exists( 'apc_store' ) ) {
$use_apc = true;
$apc_key = "{$host}{$port}";
$apc_ttl = 10;
} else {
$use_apc = false;
}
if ( $use_apc ) {
$cached_value = apc_fetch( $apc_key );
switch ( $cached_value ) {
case 'up':
$this->tcp_responsive = 'true';
return true;
case 'down':
$this->tcp_responsive = 'false';
return false;
}
}
$socket = @ fsockopen( $host, $port, $errno, $errstr, $float_timeout );
if ( $socket === false ) {
if ( $use_apc )
apc_store( $apc_key, 'down', $apc_ttl );
return "[ > $float_timeout ] ($errno) '$errstr'";
}
fclose( $socket );
if ( $use_apc )
apc_store( $apc_key, 'up', $apc_ttl );
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment