From 3d9f055cdc67b0a7adac4ac27aa7549c96ac9915 Mon Sep 17 00:00:00 2001 From: kanoi Date: Wed, 15 Oct 2014 18:06:42 +1100 Subject: [PATCH] php - more descriptive socket errors and enable the timeout --- pool/socket.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/pool/socket.php b/pool/socket.php index 6bc574c8..b823348e 100644 --- a/pool/socket.php +++ b/pool/socket.php @@ -12,9 +12,10 @@ function _getsock($fun, $port, $unix=true) $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false || $socket === null) { - $sockerr = socket_strerror(socket_last_error()); + $sle = socket_last_error(); + $sockerr = socket_strerror($sle); $msg = "$fun() _getsock() create($port) failed"; - error_log("CKPERR: $msg '$sockerr'"); + error_log("CKPERR: $msg ($sle) '$sockerr'"); return false; } @@ -43,21 +44,24 @@ function _getsock($fun, $port, $unix=true) } if ($res === false) { - $sockerr = socket_strerror(socket_last_error()); + $sle = socket_last_error(); + $sockerr = socket_strerror($sle); if ($unix === true) $msg = "$fun() _getsock() connect($port) failed 3x"; else $msg = "$fun() _getsock() connect($port) failed 3x (+2+5s sleep)"; - error_log("CKPERR: $msg '$sockerr'"); + error_log("CKPERR: $msg ($sle) '$sockerr'"); socket_close($socket); return false; } } } # Avoid getting locked up for long - $tmo = array('sec' => 1, 'usec' => 0); + $tmo = array('sec' => 2, 'usec' => 0); socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $tmo); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $tmo); + # Enable timeout + socket_set_block($socket); return $socket; } # @@ -71,7 +75,8 @@ function readsockline($fun, $socket) $siz = socket_read($socket, 4, PHP_BINARY_READ); if ($siz === false) { - $sockerr = socket_strerror(socket_last_error()); + $sle = socket_last_error(); + $sockerr = socket_strerror($sle); $msg = "$fun() readsockline() failed"; error_log("CKPERR: $msg '$sockerr'"); return false; @@ -91,9 +96,10 @@ function readsockline($fun, $socket) $line = socket_read($socket, $left, PHP_BINARY_READ); if ($line === false) { - $sockerr = socket_strerror(socket_last_error()); - $msg = "$fun() readsockline() failed"; - error_log("CKPERR: $msg '$sockerr'"); + $sle = socket_last_error(); + $sockerr = socket_strerror($sle); + $msg = "$fun() readsockline() $left failed (len=$len)"; + error_log("CKPERR: $msg ($sle) '$sockerr'"); return false; } $red = strlen($line);