Skip to content

Commit

Permalink
Revert #38 (keep-alive feature)
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed May 7, 2020
1 parent fe517b2 commit 3809ce2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
18 changes: 8 additions & 10 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function isWaiting(): bool
protected function initBasicAuth()
{
$userInfo = $this->getUri()->getUserInfo();
if($userInfo) {
if ($userInfo) {
$userInfo = explode(':', $userInfo);
$username = $userInfo[0];
$password = $userInfo[1] ?? null;
Expand All @@ -179,7 +179,7 @@ public function getConnectionTarget(): array
}
$port = $this->uri->getRealPort();
$ssl = $this->getSSL();
$ssl = ($ssl === self::SSL_AUTO) ? ('https' === $this->uri->getScheme()) : (bool)$ssl;
$ssl = ($ssl === self::SSL_AUTO) ? ('https' === $this->uri->getScheme()) : (bool) $ssl;

return ['host' => $host, 'port' => $port, 'ssl' => $ssl];
}
Expand All @@ -190,6 +190,7 @@ public function shouldRecycleClient($client)

return (!$client || ($client->host !== $connectionInfo['host'] || $client->port !== $connectionInfo['port']));
}

/** @return null|bool */
public function getPool()
{
Expand Down Expand Up @@ -516,7 +517,7 @@ public function withExpectCharset(string $source = 'auto', string $target = 'utf
{
$this->charset_source = $source;
$this->charset_target = $target;
$this->charset_use_mb = (bool)$use_mb;
$this->charset_use_mb = (bool) $use_mb;

return $this;
}
Expand Down Expand Up @@ -564,7 +565,6 @@ public function exec()
return $ret;
}


if ($this->client && ($this->shouldRecycleClient($this->client))) {
// target maybe changed
$this->tryToRevertClientToPool();
Expand Down Expand Up @@ -623,7 +623,7 @@ public function exec()
}
}
/** 设置请求主体 */
$body = (string)($this->getBody() ?? '');
$body = (string) ($this->getBody() ?? '');
if ($body !== '') {
$this->client->setData($body);
}
Expand All @@ -644,7 +644,7 @@ public function exec()
'keep_alive' => $this->getKeepAlive(),
];

if($this->ssl) {
if ($this->ssl) {
$settings['ssl_host_name'] = $this->uri->getHost();
}

Expand Down Expand Up @@ -728,7 +728,7 @@ public function recv()

/** Solve redirect */
if (($this->client->headers['location'] ?? false) && $this->_redirect_times < $this->redirect) {
$current_uri = (string)$this->uri;
$current_uri = (string) $this->uri;
//record headers before redirect
$this->_redirect_headers[$current_uri] = PHP_DEBUG ?
array_merge([], $this->client->headers) :
Expand Down Expand Up @@ -808,9 +808,7 @@ public function recv()
}
$this->client->body = '';

if($this->use_pool){
$this->tryToRevertClientToPool();
}
$this->tryToRevertClientToPool();

return $response;
}
Expand Down
11 changes: 0 additions & 11 deletions src/Saber.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ private function __construct(array $options = [])
);
}

/** @var \Swoole\Coroutine\Http\Client a temp client when not use pool */
protected $lastTempClient;
/**
* @param array $options
* @return Request|Response
Expand All @@ -158,15 +156,6 @@ public function request(array $options)
$request = clone $this->raw;
$this->setOptions($options, $request);

if (!$request->getPool()) {
$lastTempClient =& $this->lastTempClient;
if ($request->shouldRecycleClient($lastTempClient)) {
$lastTempClient = $request->client = \Swlib\Saber\ClientPool::getInstance()->createEx($request->getConnectionTarget(), true);
//This Temp Client will Recyle by https://github.com/swlib/saber/blob/1188d0a67d18430d5c1a11f8dcdc135852fc1e31/src/Request.php#L502-L506
} else {
$request->client = $lastTempClient;
}
}
/** Psr style */
if ($options['psr'] ?? false) {
return $request;
Expand Down
10 changes: 7 additions & 3 deletions tests/SaberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,13 @@ function ($pool) {

public function testKeepAliveAmongSameHostAndPortWithOutUsePool()
{
// FIXME
if (true) {
return;
}

global $server_list;
list($ip, $port) = array_values($server_list['httpd']);
[$ip, $port] = array_values($server_list['httpd']);
$saber = Saber::create(
[
'base_uri' => "http://$ip:$port",
Expand All @@ -389,8 +394,7 @@ public function testKeepAliveAmongSameHostAndPortWithOutUsePool()
$ReqWithSaberPSR2 = $saber->request(['psr' => 1])->withMethod('GET')->withUri(
new Uri("http://$ip:$port/anything?dump_info=ReqWithSaberPSR2")
)->exec()->recv()->getParsedJsonArray();
$ReqAfterAnotherPort = $saber->get('http://httpbin.org/anything?dump_info=$ReqWithSaber2')->getParsedJsonArray(
);
// $ReqAfterAnotherPort = $saber->get('http://httpbin.org/anything?dump_info=$ReqWithSaber2')->getParsedJsonArray();
$ReqAfterAnotherPort = $saber->get('/anything?dump_info=$ReqWithSaber2')->getParsedJsonArray();

$this->assertTrue($ReqWithSaber['server']['remote_port'] === $ReqWithSaber2['server']['remote_port']);
Expand Down

0 comments on commit 3809ce2

Please sign in to comment.