From b57165df67aa65a30cbf92652ba164d368384d64 Mon Sep 17 00:00:00 2001 From: Yurun Date: Sun, 19 Jan 2020 13:31:16 +0800 Subject: [PATCH] =?UTF-8?q?http2=20client=20=E6=94=AF=E6=8C=81=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/YurunHttp/Http2/IHttp2Client.php | 15 +++++++++++ src/YurunHttp/Http2/SwooleClient.php | 40 ++++++++++++++++++++++++++++ tests/unit/Http2/SwooleHttp2Test.php | 3 +++ 3 files changed, 58 insertions(+) diff --git a/src/YurunHttp/Http2/IHttp2Client.php b/src/YurunHttp/Http2/IHttp2Client.php index 364c609..011f98d 100644 --- a/src/YurunHttp/Http2/IHttp2Client.php +++ b/src/YurunHttp/Http2/IHttp2Client.php @@ -105,4 +105,19 @@ public function isSSL(); */ public function getRecvingCount(); + /** + * 设置超时时间,单位:秒 + * + * @param float|null $timeout + * @return void + */ + public function setTimeout($timeout); + + /** + * 获取超时时间,单位:秒 + * + * @return float|null + */ + public function getTimeout(); + } diff --git a/src/YurunHttp/Http2/SwooleClient.php b/src/YurunHttp/Http2/SwooleClient.php index 34b9006..11cc856 100644 --- a/src/YurunHttp/Http2/SwooleClient.php +++ b/src/YurunHttp/Http2/SwooleClient.php @@ -64,6 +64,13 @@ class SwooleClient implements IHttp2Client */ private $requestMap = []; + /** + * 超时时间,单位:秒 + * + * @var float + */ + private $timeout; + /** * @param string $host * @param int $port @@ -96,6 +103,12 @@ public function connect() if($client) { $this->http2Client = $client; + if($this->timeout) + { + $this->http2Client->set([ + 'timeout' => $this->timeout, + ]); + } $this->startRecvCo(); return true; } @@ -330,4 +343,31 @@ public function setServerPushQueueLength($serverPushQueueLength) return $this; } + /** + * 设置超时时间,单位:秒 + * + * @param float|null $timeout + * @return void + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + if($this->http2Client) + { + $this->http2Client->set([ + 'timeout' => $timeout, + ]); + } + } + + /** + * 获取超时时间,单位:秒 + * + * @return float|null + */ + public function getTimeout() + { + return $this->timeout; + } + } diff --git a/tests/unit/Http2/SwooleHttp2Test.php b/tests/unit/Http2/SwooleHttp2Test.php index c3c5798..cd7c02c 100644 --- a/tests/unit/Http2/SwooleHttp2Test.php +++ b/tests/unit/Http2/SwooleHttp2Test.php @@ -74,6 +74,7 @@ public function testMuiltCo() $this->call(function(){ $uri = new Uri($this->http2Host); $client = new SwooleClient($uri->getHost(), Uri::getServerPort($uri), 'https' === $uri->getScheme()); + $client->setTimeout(3); go(function() use($client){ $result = $client->recv(); $this->assertFalse($result->success); @@ -133,6 +134,8 @@ public function testPipeline1() $this->assertTrue($client->connect()); + $client->setTimeout(3); + $http = new HttpRequest; $http->protocolVersion = '2.0'; $http->timeout = 3000;