This commit is contained in:
2020-11-01 05:10:38 +08:00
parent a24e431052
commit bf1cc479b9
+9 -17
View File
@@ -5,6 +5,7 @@ namespace HttpServer\Client;
use Exception; use Exception;
use HttpServer\Http\Context;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Core\Help; use Snowflake\Core\Help;
use Snowflake\Core\JSON; use Snowflake\Core\JSON;
@@ -19,14 +20,6 @@ use \Swoole\Coroutine\Http2\Client as H2Client;
class Http2 extends Component class Http2 extends Component
{ {
/** @var H2Client[] */
private array $_clients = [];
/** @var Request[] */
private array $_requests = [];
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -59,7 +52,6 @@ class Http2 extends Component
} }
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -76,7 +68,6 @@ class Http2 extends Component
} }
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -103,12 +94,10 @@ class Http2 extends Component
*/ */
public function getRequest($domain, $path, $method, $params) public function getRequest($domain, $path, $method, $params)
{ {
if (isset($this->_requests[$domain . $path])) { if (Context::hasContext($domain . $path)) {
$req = $this->_requests[$domain . $path]; $req = Context::getContext($domain . $path);
} else { } else {
$req = new Request(); $req = new Request();
$this->_requests[$domain . $path] = $req;
}
$req->method = $method; $req->method = $method;
$req->path = $path; $req->path = $path;
$req->headers = [ $req->headers = [
@@ -120,6 +109,8 @@ class Http2 extends Component
if (!is_string($params)) { if (!is_string($params)) {
$params = JSON::encode($params); $params = JSON::encode($params);
} }
Context::setContext($domain . $path, $req);
}
$req->data = $params; $req->data = $params;
return $req; return $req;
} }
@@ -134,8 +125,8 @@ class Http2 extends Component
*/ */
private function getClient($domain, $path, $timeout = -1) private function getClient($domain, $path, $timeout = -1)
{ {
if (isset($this->_clients[$path])) { if (Context::hasContext($domain)) {
return $this->_clients[$path]; return Context::getContext($domain);
} }
$client = new H2Client($domain, 443, true); $client = new H2Client($domain, 443, true);
$client->set([ $client->set([
@@ -145,7 +136,8 @@ class Http2 extends Component
if (!$client->connect()) { if (!$client->connect()) {
throw new Exception('Connected fail.'); throw new Exception('Connected fail.');
} }
return $this->_clients[$domain . $path] = $client;
return Context::setContext($domain, $client);
} }