Skip to content

Commit 357111d

Browse files
authored
feat: default timeout (#3)
* feat: default timeout * fix: longer timeout
1 parent 42a33a9 commit 357111d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/Dishy.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use const Grpc\STATUS_OK;
66
use const Grpc\STATUS_PERMISSION_DENIED;
77

8+
use DateTime;
89
use Google\Protobuf\Internal\Message;
910
use Grpc\ChannelCredentials;
1011
use SpaceX\API\Device\DeviceClient;
@@ -29,10 +30,12 @@ class Dishy
2930

3031
public function __construct(
3132
public string $host = '192.168.100.1:9200',
33+
public int $timeout = 2,
3234
) {
3335
// Create a generic stub
3436
$this->client = new DeviceClient($this->host, [
3537
'credentials' => ChannelCredentials::createInsecure(),
38+
'timeout' => $this->timeout,
3639
]);
3740
}
3841

@@ -55,15 +58,23 @@ public static function getRequestKey(Message $request): string
5558

5659
/**
5760
* @throws GrpcCallFailedException
61+
* @throws PermissionDeniedException
5862
*/
59-
public function handle(Message $request): Response
63+
public function handle(Message $request, ?int $timeout = null): Response
6064
{
65+
$options = [];
66+
67+
if (! is_null($timeout)) {
68+
$options['timeout'] = (new DateTime)->modify("+{$timeout} seconds")->format('U.u');
69+
}
70+
6171
/** @var Response $response */
6272
/** @var object{code: int, details: string, metadata:array} $status */
6373
[$response, $status] = $this->client->Handle(
6474
argument: new Request([
6575
self::getRequestKey($request) => new $request,
66-
])
76+
]),
77+
options: $options
6778
)->wait();
6879

6980
if ($status->code === STATUS_PERMISSION_DENIED) {
@@ -137,7 +148,10 @@ public function getLocation(): array|bool
137148
public function getStatsHistory(): array
138149
{
139150
return self::responseToArray(
140-
$this->handle(new GetHistoryRequest)->getDishGetHistory()
151+
$this->handle(
152+
request: new GetHistoryRequest,
153+
// timeout: max($this->timeout, 10)
154+
)->getDishGetHistory()
141155
);
142156
}
143157

0 commit comments

Comments
 (0)