Skip to content

Commit

Permalink
update to 0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Sep 23, 2021
1 parent e3dbcfb commit dbf4e5f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
28 changes: 14 additions & 14 deletions src/ClassLoader/ThriftClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -101,22 +101,22 @@ 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;
}
}

/**
* 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;
Expand Down
26 changes: 26 additions & 0 deletions src/Transport/TCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class TCurlClient extends TTransport
*/
protected $timeout_;

/**
* Connection timeout
*
* @var float
*/
protected $connectionTimeout_;

/**
* http headers
*
Expand All @@ -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();
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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_ = '';

Expand Down
8 changes: 5 additions & 3 deletions src/Transport/TSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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_
Expand Down
24 changes: 12 additions & 12 deletions src/Transport/TSocketPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit dbf4e5f

Please sign in to comment.