改名
This commit is contained in:
+12
-32
@@ -11,7 +11,6 @@ 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;
|
||||
use Swoole\Http2\Response;
|
||||
@@ -28,16 +27,11 @@ class Http2 extends Component
|
||||
private array $_clients = [];
|
||||
|
||||
|
||||
private Channel $channel;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->channel = Snowflake::getApp('channel');
|
||||
|
||||
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'releases']);
|
||||
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'clean']);
|
||||
}
|
||||
@@ -48,11 +42,6 @@ class Http2 extends Component
|
||||
*/
|
||||
public function releases()
|
||||
{
|
||||
foreach ($this->_clients as $name => $client) {
|
||||
/** @var H2Client $client */
|
||||
$client->close();
|
||||
$this->channel->push($client, 'http2.' . $name);
|
||||
}
|
||||
$this->_clients = [];
|
||||
}
|
||||
|
||||
@@ -111,7 +100,7 @@ class Http2 extends Component
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($domain, $path, $params = [], $timeout = -1): Result
|
||||
public function get($domain, $path, array $params = [], int $timeout = -1): Result
|
||||
{
|
||||
$request = $this->dispatch($domain, $path, 'GET', $params, $timeout);
|
||||
|
||||
@@ -127,7 +116,7 @@ class Http2 extends Component
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function post($domain, $path, $params = [], $timeout = -1): Result
|
||||
public function post($domain, $path, array $params = [], int $timeout = -1): Result
|
||||
{
|
||||
$request = $this->dispatch($domain, $path, 'POST', $params, $timeout);
|
||||
|
||||
@@ -143,7 +132,7 @@ class Http2 extends Component
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function upload($domain, $path, $params = [], $timeout = -1): Result
|
||||
public function upload($domain, $path, array $params = [], int $timeout = -1): Result
|
||||
{
|
||||
$request = $this->dispatch($domain, $path, 'POST', $params, $timeout, true);
|
||||
|
||||
@@ -159,7 +148,7 @@ class Http2 extends Component
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($domain, $path, $params = [], $timeout = -1): Result
|
||||
public function delete($domain, $path, array $params = [], int $timeout = -1): Result
|
||||
{
|
||||
$request = $this->dispatch($domain, $path, 'DELETE', $params, $timeout);
|
||||
|
||||
@@ -177,7 +166,7 @@ class Http2 extends Component
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatch($domain, $path, $method, $params = [], $timeout = -1, $isUpload = false): mixed
|
||||
private function dispatch($domain, $path, $method, array $params = [], int $timeout = -1, bool $isUpload = false): mixed
|
||||
{
|
||||
[$domain, $isSsl] = $this->clear($domain);
|
||||
|
||||
@@ -185,7 +174,6 @@ class Http2 extends Component
|
||||
$request->headers = array_merge($request->headers, [
|
||||
'Host' => $domain
|
||||
]);
|
||||
defer(fn() => $this->channel->push($request, 'request.' . $method . $path));
|
||||
return $this->doRequest($request, $domain, $isSsl, $timeout);
|
||||
}
|
||||
|
||||
@@ -215,7 +203,6 @@ class Http2 extends Component
|
||||
private function doRequest(Request $request, $domain, $ssl, $timeout): mixed
|
||||
{
|
||||
$client = $this->getClient($domain, $ssl, $timeout);
|
||||
defer(fn() => $this->channel->push($client, 'http2.' . $domain));
|
||||
$client->send($request);
|
||||
if (Context::getContext('http2isRecv') === false) {
|
||||
return null;
|
||||
@@ -275,18 +262,15 @@ class Http2 extends Component
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getRequest($path, $method, $params, $isUpload = false): Request
|
||||
public function getRequest($path, $method, $params, bool $isUpload = false): Request
|
||||
{
|
||||
if (!str_starts_with($path, '/')) {
|
||||
$path = '/' . $path;
|
||||
}
|
||||
$channel = Snowflake::app()->getChannel();
|
||||
$request = $channel->pop('request.' . $method . $path, function () use ($path, $method) {
|
||||
$request = new Request();
|
||||
$request->method = $method;
|
||||
$request->path = $path;
|
||||
return $request;
|
||||
});
|
||||
|
||||
$request = new Request();
|
||||
$request->method = $method;
|
||||
$request->path = $path;
|
||||
if ($method === 'GET') {
|
||||
$request->path .= '?' . http_build_query($params);
|
||||
} else {
|
||||
@@ -304,16 +288,12 @@ class Http2 extends Component
|
||||
* @return H2Client
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getClient($domain, $isSsl = false, $timeout = -1): H2Client
|
||||
private function getClient($domain, bool $isSsl = false, int $timeout = -1): H2Client
|
||||
{
|
||||
if (isset($this->_clients[$domain])) {
|
||||
return $this->_clients[$domain];
|
||||
}
|
||||
$pool = Snowflake::app()->getChannel();
|
||||
/** @var H2Client $client */
|
||||
$client = $pool->pop('http2.' . $domain, function () use ($domain, $isSsl, $timeout) {
|
||||
return $this->newRequest($domain, $isSsl, $timeout);
|
||||
});
|
||||
$client = $this->newRequest($domain, $isSsl, $timeout);
|
||||
if ((!$client->connected || !$client->ping()) && !$client->connect()) {
|
||||
throw new Exception($client->errMsg, $client->errCode);
|
||||
}
|
||||
|
||||
+30
-22
@@ -19,25 +19,27 @@ class Context extends BaseContext
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $coroutineId
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setContext($id, $context): mixed
|
||||
public static function setContext($id, $context, $coroutineId = null): mixed
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return static::$_contents[$id] = $context;
|
||||
}
|
||||
return Coroutine::getContext()[$id] = $context;
|
||||
return Coroutine::getContext($coroutineId)[$id] = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param int $value
|
||||
* @param null $coroutineId
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function increment($id, int $value = 1): bool|int
|
||||
public static function increment($id, int $value = 1, $coroutineId = null): bool|int
|
||||
{
|
||||
if (!isset(Coroutine::getContext()[$id])) {
|
||||
return Coroutine::getContext()[$id] += $value;
|
||||
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
|
||||
return Coroutine::getContext($coroutineId)[$id] += $value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -45,15 +47,16 @@ class Context extends BaseContext
|
||||
/**
|
||||
* @param $id
|
||||
* @param int $value
|
||||
* @param null $coroutineId
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function decrement($id, int $value = 1): bool|int
|
||||
public static function decrement($id, int $value = 1, $coroutineId = null): bool|int
|
||||
{
|
||||
if (!static::hasContext($id)) {
|
||||
return false;
|
||||
}
|
||||
if (isset(Coroutine::getContext()[$id])) {
|
||||
return Coroutine::getContext()[$id] -= $value;
|
||||
if (isset(Coroutine::getContext($coroutineId)[$id])) {
|
||||
return Coroutine::getContext($coroutineId)[$id] -= $value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -61,25 +64,27 @@ class Context extends BaseContext
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $default
|
||||
* @param null $coroutineId
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getContext($id, $default = null): mixed
|
||||
public static function getContext($id, $default = null, $coroutineId = null): mixed
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return static::loadByStatic($id, $default);
|
||||
}
|
||||
return static::loadByContext($id, $default);
|
||||
return static::loadByContext($id, $default, $coroutineId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $default
|
||||
* @param null $coroutineId
|
||||
* @return mixed
|
||||
*/
|
||||
private static function loadByContext($id, $default = null): mixed
|
||||
private static function loadByContext($id, $default = null, $coroutineId = null): mixed
|
||||
{
|
||||
$data = Coroutine::getContext()[$id] ?? null;
|
||||
$data = Coroutine::getContext($coroutineId)[$id] ?? null;
|
||||
if ($data === null) {
|
||||
return $default;
|
||||
}
|
||||
@@ -103,12 +108,13 @@ class Context extends BaseContext
|
||||
|
||||
|
||||
/**
|
||||
* @param null $coroutineId
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getAllContext(): mixed
|
||||
public static function getAllContext($coroutineId = null): mixed
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return Coroutine::getContext() ?? [];
|
||||
return Coroutine::getContext($coroutineId) ?? [];
|
||||
} else {
|
||||
return static::$_contents ?? [];
|
||||
}
|
||||
@@ -116,16 +122,17 @@ class Context extends BaseContext
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param null $coroutineId
|
||||
*/
|
||||
public static function remove(string $id)
|
||||
public static function remove(string $id, $coroutineId = null)
|
||||
{
|
||||
if (!static::hasContext($id)) {
|
||||
if (!static::hasContext($id, $coroutineId)) {
|
||||
return;
|
||||
}
|
||||
if (Coroutine::getCid() === -1) {
|
||||
unset(static::$_contents[$id]);
|
||||
} else {
|
||||
unset(Coroutine::getContext()[$id]);
|
||||
unset(Coroutine::getContext($coroutineId)[$id]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,12 +141,12 @@ class Context extends BaseContext
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasContext($id, $key = null): bool
|
||||
public static function hasContext($id, $key = null, $coroutineId = null): bool
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return static::searchByStatic($id, $key);
|
||||
}
|
||||
return static::searchByCoroutine($id, $key);
|
||||
return static::searchByCoroutine($id, $key, $coroutineId);
|
||||
}
|
||||
|
||||
|
||||
@@ -163,15 +170,16 @@ class Context extends BaseContext
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @param null $coroutineId
|
||||
* @return bool
|
||||
*/
|
||||
private static function searchByCoroutine($id, $key = null): bool
|
||||
private static function searchByCoroutine($id, $key = null, $coroutineId = null): bool
|
||||
{
|
||||
if (!isset(Coroutine::getContext()[$id])) {
|
||||
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
|
||||
return false;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return isset((Coroutine::getContext()[$id] ?? [])[$key]);
|
||||
return isset((Coroutine::getContext($coroutineId)[$id] ?? [])[$key]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user