From dbf4e5ff5c85d56ccae62265ed25424b366ce269 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Thu, 23 Sep 2021 11:34:40 +0100 Subject: [PATCH] update to 0.15.0 --- src/ClassLoader/ThriftClassLoader.php | 28 +++++++++++++-------------- src/Transport/TCurlClient.php | 26 +++++++++++++++++++++++++ src/Transport/TSocket.php | 8 +++++--- src/Transport/TSocketPool.php | 24 +++++++++++------------ 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/ClassLoader/ThriftClassLoader.php b/src/ClassLoader/ThriftClassLoader.php index 4361bd8..e4b4a17 100644 --- a/src/ClassLoader/ThriftClassLoader.php +++ b/src/ClassLoader/ThriftClassLoader.php @@ -40,26 +40,26 @@ class ThriftClassLoader protected $definitions = array(); /** - * Do we use APC cache ? + * Do we use APCu cache ? * @var boolean */ - protected $apc = false; + protected $apcu = false; /** - * APC Cache prefix + * APCu Cache prefix * @var string */ - protected $apc_prefix; + protected $apcu_prefix; /** - * Set autoloader to use APC cache + * Set autoloader to use APCu cache * @param boolean $apc - * @param string $apc_prefix + * @param string $apcu_prefix */ - public function __construct($apc = false, $apc_prefix = null) + public function __construct($apc = false, $apcu_prefix = null) { - $this->apc = $apc; - $this->apc_prefix = $apc_prefix; + $this->apcu = $apc; + $this->apcu_prefix = $apcu_prefix; } /** @@ -101,7 +101,7 @@ public function register($prepend = false) */ public function loadClass($class) { - if ((true === $this->apc && ($file = $this->findFileInApc($class))) or + if ((true === $this->apcu && ($file = $this->findFileInApcu($class))) or ($file = $this->findFile($class)) ) { require_once $file; @@ -109,14 +109,14 @@ public function loadClass($class) } /** - * Loads the given class or interface in APC. + * Loads the given class or interface in APCu. * @param string $class The name of the class * @return string */ - protected function findFileInApc($class) + protected function findFileInApcu($class) { - if (false === $file = apc_fetch($this->apc_prefix . $class)) { - apc_store($this->apc_prefix . $class, $file = $this->findFile($class)); + if (false === $file = apcu_fetch($this->apcu_prefix . $class)) { + apcu_store($this->apcu_prefix . $class, $file = $this->findFile($class)); } return $file; diff --git a/src/Transport/TCurlClient.php b/src/Transport/TCurlClient.php index f781da9..2087433 100644 --- a/src/Transport/TCurlClient.php +++ b/src/Transport/TCurlClient.php @@ -83,6 +83,13 @@ class TCurlClient extends TTransport */ protected $timeout_; + /** + * Connection timeout + * + * @var float + */ + protected $connectionTimeout_; + /** * http headers * @@ -109,6 +116,7 @@ public function __construct($host, $port = 80, $uri = '', $scheme = 'http') $this->request_ = ''; $this->response_ = null; $this->timeout_ = null; + $this->connectionTimeout_ = null; $this->headers_ = array(); } @@ -122,6 +130,16 @@ public function setTimeoutSecs($timeout) $this->timeout_ = $timeout; } + /** + * Set connection timeout + * + * @param float $connectionTimeout + */ + public function setConnectionTimeoutSecs($connectionTimeout) + { + $this->connectionTimeout_ = $connectionTimeout; + } + /** * Whether this transport is open. * @@ -237,6 +255,14 @@ public function flush() curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_); } } + if ($this->connectionTimeout_ > 0) { + if ($this->connectionTimeout_ < 1.0) { + // Timestamps smaller than 1 second are ignored when CURLOPT_CONNECTTIMEOUT is used + curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT_MS, 1000 * $this->connectionTimeout_); + } else { + curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout_); + } + } curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_); $this->request_ = ''; diff --git a/src/Transport/TSocket.php b/src/Transport/TSocket.php index 5147efa..8fe60fd 100644 --- a/src/Transport/TSocket.php +++ b/src/Transport/TSocket.php @@ -251,8 +251,9 @@ public function open() } if (function_exists('socket_import_stream') && function_exists('socket_set_option')) { - $socket = socket_import_stream($this->handle_); - socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1); + // warnings silenced due to bug https://bugs.php.net/bug.php?id=70939 + $socket = @socket_import_stream($this->handle_); + @socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1); } } @@ -328,7 +329,8 @@ public function write($buf) if ($writable > 0) { // write buffer to stream $written = fwrite($this->handle_, $buf); - if ($written === -1 || $written === false) { + $closed_socket = $written === 0 && feof($this->handle_); + if ($written === -1 || $written === false || $closed_socket) { throw new TTransportException( 'TSocket: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes ' . $this->host_ . ':' . $this->port_ diff --git a/src/Transport/TSocketPool.php b/src/Transport/TSocketPool.php index cb9e8dd..307885f 100644 --- a/src/Transport/TSocketPool.php +++ b/src/Transport/TSocketPool.php @@ -25,18 +25,18 @@ use Thrift\Exception\TException; /** - * This library makes use of APC cache to make hosts as down in a web - * environment. If you are running from the CLI or on a system without APC + * This library makes use of APCu cache to make hosts as down in a web + * environment. If you are running from the CLI or on a system without APCu * installed, then these null functions will step in and act like cache * misses. */ -if (!function_exists('apc_fetch')) { - function apc_fetch($key) +if (!function_exists('apcu_fetch')) { + function apcu_fetch($key) { return false; } - function apc_store($key, $var, $ttl = 0) + function apcu_store($key, $var, $ttl = 0) { return false; } @@ -202,11 +202,11 @@ public function open() // This extracts the $host and $port variables extract($this->servers_[$i]); - // Check APC cache for a record of this server being down + // Check APCu cache for a record of this server being down $failtimeKey = 'thrift_failtime:' . $host . ':' . $port . '~'; // Cache miss? Assume it's OK - $lastFailtime = apc_fetch($failtimeKey); + $lastFailtime = apcu_fetch($failtimeKey); if ($lastFailtime === false) { $lastFailtime = 0; } @@ -251,7 +251,7 @@ public function open() // Only clear the failure counts if required to do so if ($lastFailtime > 0) { - apc_store($failtimeKey, 0); + apcu_store($failtimeKey, 0); } // Successful connection, return now @@ -265,7 +265,7 @@ public function open() $consecfailsKey = 'thrift_consecfails:' . $host . ':' . $port . '~'; // Ignore cache misses - $consecfails = apc_fetch($consecfailsKey); + $consecfails = apcu_fetch($consecfailsKey); if ($consecfails === false) { $consecfails = 0; } @@ -284,12 +284,12 @@ public function open() ); } // Store the failure time - apc_store($failtimeKey, time()); + apcu_store($failtimeKey, time()); // Clear the count of consecutive failures - apc_store($consecfailsKey, 0); + apcu_store($consecfailsKey, 0); } else { - apc_store($consecfailsKey, $consecfails); + apcu_store($consecfailsKey, $consecfails); } } }