-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
THRIFT-5757 Unit tests for php lib #2942
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ class TSSLSocket extends TSocket | |
/** | ||
* Remote port | ||
* | ||
* @var resource | ||
* @var null|resource | ||
*/ | ||
protected $context_ = null; | ||
|
||
|
@@ -57,6 +57,10 @@ public function __construct( | |
) { | ||
$this->host_ = $this->getSSLHost($host); | ||
$this->port_ = $port; | ||
// Initialize a stream context if not provided | ||
if ($context === null) { | ||
$context = stream_context_create(); | ||
} | ||
$this->context_ = $context; | ||
$this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log'; | ||
} | ||
|
@@ -87,7 +91,8 @@ public function open() | |
throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN); | ||
} | ||
|
||
if (empty($this->host_)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does not work because ssl:// always added to hostname |
||
$host = parse_url($this->host_, PHP_URL_HOST); | ||
if (empty($host)) { | ||
throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,8 +252,10 @@ public function open() | |
|
||
if (function_exists('socket_import_stream') && function_exists('socket_set_option')) { | ||
// 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); | ||
$socket = socket_import_stream($this->handle_); | ||
if ($socket !== false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previous function can return false, and in php 8.0 it trigger a type error, so i added a check. In future it will be great to remove all @ and work correctly with all warning and error generated by stream functions |
||
@socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
|
@@ -24,24 +25,6 @@ | |
|
||
use Thrift\Exception\TException; | ||
|
||
/** | ||
* 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('apcu_fetch')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move it inside class as private methods. Global redeclare is not very good. |
||
function apcu_fetch($key) | ||
{ | ||
return false; | ||
} | ||
|
||
function apcu_store($key, $var, $ttl = 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Sockets implementation of the TTransport interface that allows connection | ||
* to a pool of servers. | ||
|
@@ -91,6 +74,12 @@ class TSocketPool extends TSocket | |
*/ | ||
private $alwaysTryLast_ = true; | ||
|
||
/** | ||
* Use apcu cache | ||
* @var bool | ||
*/ | ||
private $useApcuCache; | ||
|
||
/** | ||
* Socket pool constructor | ||
* | ||
|
@@ -116,9 +105,13 @@ public function __construct( | |
} | ||
|
||
foreach ($hosts as $key => $host) { | ||
$this->servers_ [] = array('host' => $host, | ||
'port' => $ports[$key]); | ||
$this->servers_ [] = array( | ||
'host' => $host, | ||
'port' => $ports[$key] | ||
); | ||
} | ||
|
||
$this->useApcuCache = function_exists('apcu_fetch'); | ||
} | ||
|
||
/** | ||
|
@@ -206,7 +199,7 @@ public function open() | |
$failtimeKey = 'thrift_failtime:' . $host . ':' . $port . '~'; | ||
|
||
// Cache miss? Assume it's OK | ||
$lastFailtime = apcu_fetch($failtimeKey); | ||
$lastFailtime = $this->apcuFetch($failtimeKey); | ||
if ($lastFailtime === false) { | ||
$lastFailtime = 0; | ||
} | ||
|
@@ -251,7 +244,7 @@ public function open() | |
|
||
// Only clear the failure counts if required to do so | ||
if ($lastFailtime > 0) { | ||
apcu_store($failtimeKey, 0); | ||
$this->apcuStore($failtimeKey, 0); | ||
} | ||
|
||
// Successful connection, return now | ||
|
@@ -265,7 +258,7 @@ public function open() | |
$consecfailsKey = 'thrift_consecfails:' . $host . ':' . $port . '~'; | ||
|
||
// Ignore cache misses | ||
$consecfails = apcu_fetch($consecfailsKey); | ||
$consecfails = $this->apcuFetch($consecfailsKey); | ||
if ($consecfails === false) { | ||
$consecfails = 0; | ||
} | ||
|
@@ -284,12 +277,12 @@ public function open() | |
); | ||
} | ||
// Store the failure time | ||
apcu_store($failtimeKey, time()); | ||
$this->apcuStore($failtimeKey, time()); | ||
|
||
// Clear the count of consecutive failures | ||
apcu_store($consecfailsKey, 0); | ||
$this->apcuStore($consecfailsKey, 0); | ||
} else { | ||
apcu_store($consecfailsKey, $consecfails); | ||
$this->apcuStore($consecfailsKey, $consecfails); | ||
} | ||
} | ||
} | ||
|
@@ -307,4 +300,20 @@ public function open() | |
} | ||
throw new TException($error); | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
private function apcuFetch($key, &$success = null) | ||
{ | ||
return $this->useApcuCache ? apcu_fetch($key, $success) : false; | ||
} | ||
|
||
private function apcuStore($key, $var, $ttl = 0) | ||
{ | ||
return $this->useApcuCache ? apcu_store($key, $var, $ttl) : false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CURLOPT_BINARYTRANSFER This constant had no effect since PHP 5.1.2