From dcce04b08b95cceeb7727a6334b465efe4bfc258 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:03:13 +0800 Subject: [PATCH 1/6] Optimized timeout of transporter Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Transporter/GuzzleHttpTransporter.php | 5 +++-- src/Transporter/StreamSocketTransporter.php | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 509f541..bac13ad 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -35,8 +35,8 @@ class GuzzleHttpTransporter extends AbstractTransporter public function __construct(string $host = '', int $port = 9501, array $config = []) { - $this->host = $host; - $this->port = $port; + parent::__construct($host, $port); + $this->config = array_merge_recursive($config, [ 'headers' => [ 'Content-Type' => 'application/json', @@ -45,6 +45,7 @@ public function __construct(string $host = '', int $port = 9501, array $config = 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, + 'timeout' => (int) ($config['timeout'] ?? $this->timeout), ]); } diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index 396962f..92c1177 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -69,19 +69,19 @@ public function recv() public function receive() { $buf = ''; - $timeout = 1000; + $timeoutMs = $this->timeout > 0 ? $this->timeout * 1000 : 1000; stream_set_blocking($this->client, false); // The maximum number of retries is 12, and 1000 microseconds is the minimum waiting time. // The waiting time is doubled each time until the server writes data to the buffer. // Usually, the data can be obtained within 1 microsecond. - $result = retry(12, function () use (&$buf, &$timeout) { + $result = retry(12, function () use (&$buf, &$timeoutMs) { $read = [$this->client]; $write = null; $except = null; - while (stream_select($read, $write, $except, 0, $timeout)) { + while (stream_select($read, $write, $except, 0, $timeoutMs)) { foreach ($read as $r) { $res = fread($r, 8192); if (feof($r)) { @@ -92,7 +92,7 @@ public function receive() } if (! $buf) { - $timeout *= 2; + $timeoutMs *= 2; throw new RecvFailedException('No data was received'); } From b33786814a9390fe7e99136cab5c88cee459e1b8 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:08:41 +0800 Subject: [PATCH 2/6] chore: Remove unused timeout property in StreamSocketTransporter --- src/Transporter/StreamSocketTransporter.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index 92c1177..b9a0f51 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -24,11 +24,6 @@ class StreamSocketTransporter extends AbstractTransporter */ protected $client; - /** - * @var int - */ - protected $timeout; - /** * @var bool */ From e7f414dc00f9f9ea0dc540c736798970b01c3e46 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:10:10 +0800 Subject: [PATCH 3/6] feat: Update GuzzleHttpTransporter timeout handling The code changes in `GuzzleHttpTransporter.php` update the timeout handling in the constructor. The `timeout` value is now retrieved from the `$config` array and assigned to the transporter's `timeout` property. This change ensures that the timeout value is correctly set when creating a new instance of `GuzzleHttpTransporter`. Based on recent user commits and repository commits, it seems that there have been optimizations and fixes related to transporters and helper functions. However, these changes are not directly related to the current code changes in `GuzzleHttpTransporter.php`. Please note that the suggested commit message follows the format "feat: ". Feel free to modify it according to your repository's commit message conventions. --- src/Transporter/GuzzleHttpTransporter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index bac13ad..e1822f2 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -35,7 +35,7 @@ class GuzzleHttpTransporter extends AbstractTransporter public function __construct(string $host = '', int $port = 9501, array $config = []) { - parent::__construct($host, $port); + parent::__construct($host, $port, (int) ($config['timeout'] ?? 1)); $this->config = array_merge_recursive($config, [ 'headers' => [ @@ -45,7 +45,7 @@ public function __construct(string $host = '', int $port = 9501, array $config = 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, - 'timeout' => (int) ($config['timeout'] ?? $this->timeout), + 'timeout' => $this->timeout, ]); } From 5c936d1dd00b2cbd28108a7cb7924c50be29123f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:12:47 +0800 Subject: [PATCH 4/6] feat: Update GuzzleHttpTransporter timeout handling --- src/Transporter/GuzzleHttpTransporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index e1822f2..9043aeb 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -45,7 +45,7 @@ public function __construct(string $host = '', int $port = 9501, array $config = 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, - 'timeout' => $this->timeout, + // 'timeout' => $this->timeout, ]); } From 5bb77b322f2868d9b46976c5fe272d6eab3fbbe0 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:16:58 +0800 Subject: [PATCH 5/6] feat: Update GuzzleHttpTransporter timeout handling --- src/Transporter/GuzzleHttpTransporter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 9043aeb..f91276e 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -35,7 +35,7 @@ class GuzzleHttpTransporter extends AbstractTransporter public function __construct(string $host = '', int $port = 9501, array $config = []) { - parent::__construct($host, $port, (int) ($config['timeout'] ?? 1)); + parent::__construct($host, $port); $this->config = array_merge_recursive($config, [ 'headers' => [ @@ -45,7 +45,7 @@ public function __construct(string $host = '', int $port = 9501, array $config = 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, - // 'timeout' => $this->timeout, + 'timeout' => (float) ($config['timeout'] ?? $this->timeout), ]); } From 54a99b62f63e347539f2a6d84e7ad1a6a11c249e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:18:44 +0800 Subject: [PATCH 6/6] feat: Update GuzzleHttpTransporter timeout handling --- src/Transporter/GuzzleHttpTransporter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index f91276e..57af769 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -45,7 +45,6 @@ public function __construct(string $host = '', int $port = 9501, array $config = 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, - 'timeout' => (float) ($config['timeout'] ?? $this->timeout), ]); }