Skip to content

Commit

Permalink
feat:grpc:优化timeout,因为 send 后才返回 streamId,但是在 send 时就会切换协程
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Sep 17, 2021
1 parent 2e60b4a commit 62a7619
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,8 @@ public function __construct(string $host, int $port, bool $ssl = false, float $t
continue;
}
$streamId = $response->streamId;
if (isset($this->channels[$streamId]) && !is_string($this->channels[$streamId])) {
$this->channels[$streamId]->push($response);
} else {
$this->channel->push($response);
}
}
});
go(function () {
while (true) {
$response = $this->channel->pop();
if (!$response) {
return;
}
$streamId = $response->streamId;
if (isset($this->channels[$streamId])) {
// timeout
if (is_string($this->channels[$streamId])) {
unset($this->channels[$streamId]);
continue;
}
$this->channels[$streamId]->push($response);
} else {
// 因为 send 后才返回 streamId,但是在 send 时就会切换协程,所以这里会找不到
$this->channel->push($response);
}
}
});
Expand Down Expand Up @@ -166,12 +144,8 @@ public function request(string $method, string $path, array $headers = [], strin
$channel = new \Swoole\Coroutine\Channel(1);
$this->channels[$streamId] = $channel;
$response = $channel->pop($timeout);
if (!$response) {
$this->channels[$streamId] = 'timeout';
} else {
$this->channels[$streamId] = null;
unset($this->channels[$streamId]);
}
$this->channels[$streamId] = null;
unset($this->channels[$streamId]);
if (!$response) {
throw new RuntimeException(sprintf('Client stream %d request timeout', $streamId));
}
Expand Down

0 comments on commit 62a7619

Please sign in to comment.