This commit is contained in:
2021-04-28 19:29:07 +08:00
parent 9065f92831
commit 0425c47271
+52 -10
View File
@@ -10,6 +10,7 @@ use Snowflake\Abstracts\Component;
use Snowflake\Channel;
use Snowflake\Core\Json;
use Snowflake\Core\Xml;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Coroutine\Http2\Client as H2Client;
use Swoole\Http2\Request;
@@ -36,6 +37,36 @@ class Http2 extends Component
public function init()
{
$this->channel = Snowflake::getApp('channel');
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'releases']);
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'clean']);
}
/**
* @throws Exception
*/
public function releases()
{
foreach ($this->_clients as $name => $client) {
/** @var H2Client $client */
$client->close();
$this->channel->push($client, 'http2.' . $name);
}
$this->_clients = [];
}
/**
* 清空
*/
public function clean()
{
foreach ($this->_clients as $client) {
/** @var H2Client $client */
$client->close();
}
$this->_clients = [];
}
@@ -194,7 +225,6 @@ class Http2 extends Component
return null;
}
return $this->recv($client);
}
@@ -286,15 +316,7 @@ class Http2 extends Component
$pool = Snowflake::app()->getChannel();
/** @var H2Client $client */
$client = $pool->pop('http2.' . $domain, function () use ($domain, $isSsl, $timeout) {
$domain = rtrim($domain, '/');
if (str_contains($domain, ':')) {
[$domain, $port] = explode(':', $domain);
} else {
$port = $isSsl === true ? 443 : 80;
}
$client = new H2Client($domain, (int)$port, $isSsl);
$client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]);
return $client;
return $this->newRequest($domain, $isSsl, $timeout);
});
if ((!$client->connected || !$client->ping()) && !$client->connect()) {
throw new Exception($client->errMsg, $client->errCode);
@@ -303,4 +325,24 @@ class Http2 extends Component
}
/**
* @param $domain
* @param $isSsl
* @param $timeout
* @return H2Client
*/
public function newRequest($domain, $isSsl, $timeout): H2Client
{
$domain = rtrim($domain, '/');
if (str_contains($domain, ':')) {
[$domain, $port] = explode(':', $domain);
} else {
$port = $isSsl === true ? 443 : 80;
}
$client = new H2Client($domain, (int)$port, $isSsl);
$client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]);
return $client;
}
}