Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Transporter/GuzzleHttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 4 additions & 9 deletions src/Transporter/StreamSocketTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class StreamSocketTransporter extends AbstractTransporter
*/
protected $client;

/**
* @var int
*/
protected $timeout;

/**
* @var bool
*/
Expand Down Expand Up @@ -69,19 +64,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)) {
Expand All @@ -92,7 +87,7 @@ public function receive()
}

if (! $buf) {
$timeout *= 2;
$timeoutMs *= 2;

throw new RecvFailedException('No data was received');
}
Expand Down