改名
This commit is contained in:
@@ -10,6 +10,7 @@ use Snowflake\Abstracts\Component;
|
|||||||
use Snowflake\Channel;
|
use Snowflake\Channel;
|
||||||
use Snowflake\Core\Json;
|
use Snowflake\Core\Json;
|
||||||
use Snowflake\Core\Xml;
|
use Snowflake\Core\Xml;
|
||||||
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Swoole\Coroutine\Http2\Client as H2Client;
|
use Swoole\Coroutine\Http2\Client as H2Client;
|
||||||
use Swoole\Http2\Request;
|
use Swoole\Http2\Request;
|
||||||
@@ -36,6 +37,36 @@ class Http2 extends Component
|
|||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$this->channel = Snowflake::getApp('channel');
|
$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 null;
|
||||||
}
|
}
|
||||||
return $this->recv($client);
|
return $this->recv($client);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -286,6 +316,23 @@ class Http2 extends Component
|
|||||||
$pool = Snowflake::app()->getChannel();
|
$pool = Snowflake::app()->getChannel();
|
||||||
/** @var H2Client $client */
|
/** @var H2Client $client */
|
||||||
$client = $pool->pop('http2.' . $domain, function () use ($domain, $isSsl, $timeout) {
|
$client = $pool->pop('http2.' . $domain, function () use ($domain, $isSsl, $timeout) {
|
||||||
|
return $this->newRequest($domain, $isSsl, $timeout);
|
||||||
|
});
|
||||||
|
if ((!$client->connected || !$client->ping()) && !$client->connect()) {
|
||||||
|
throw new Exception($client->errMsg, $client->errCode);
|
||||||
|
}
|
||||||
|
return $this->_clients[$domain] = $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $domain
|
||||||
|
* @param $isSsl
|
||||||
|
* @param $timeout
|
||||||
|
* @return H2Client
|
||||||
|
*/
|
||||||
|
public function newRequest($domain, $isSsl, $timeout): H2Client
|
||||||
|
{
|
||||||
$domain = rtrim($domain, '/');
|
$domain = rtrim($domain, '/');
|
||||||
if (str_contains($domain, ':')) {
|
if (str_contains($domain, ':')) {
|
||||||
[$domain, $port] = explode(':', $domain);
|
[$domain, $port] = explode(':', $domain);
|
||||||
@@ -295,11 +342,6 @@ class Http2 extends Component
|
|||||||
$client = new H2Client($domain, (int)$port, $isSsl);
|
$client = new H2Client($domain, (int)$port, $isSsl);
|
||||||
$client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]);
|
$client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]);
|
||||||
return $client;
|
return $client;
|
||||||
});
|
|
||||||
if ((!$client->connected || !$client->ping()) && !$client->connect()) {
|
|
||||||
throw new Exception($client->errMsg, $client->errCode);
|
|
||||||
}
|
|
||||||
return $this->_clients[$domain] = $client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user