Skip to content

Commit 3bf7c35

Browse files
authored
Merge pull request #16 from bytes-commerce/BC-XX-add-timeout-for-requests
BC-XX Add timeout to handle unavailable clients better.
2 parents e9fb341 + 7a48bc6 commit 3bf7c35

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/Resource/Client.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function __construct(
2828
public function __destruct()
2929
{
3030
if ($this->sid !== null) {
31-
//$this->client->request(Request::METHOD_GET, sprintf('%s/webapi/Auth.cgi?api=SYNO.API.Auth&method=logout&version=1&_sid=%s', $this->targetUrl, $this->sid));
3231
$this->resetTokens();
3332
}
3433
}
@@ -43,6 +42,8 @@ public function request(AbstractActionItem $actionItem, array $parameters = []):
4342
'Referer' => $this->referer,
4443
'Cookie' => sprintf('id=%s', $this->sid),
4544
],
45+
'timeout' => 10,
46+
'max_duration' => 10,
4647
]);
4748

4849
if ($actionItem->isMultipartForm()) {
@@ -179,10 +180,10 @@ private function checkParameters(AbstractActionItem $actionItem, array $paramete
179180
$availableKeys = $actionItem->getAvailableKeys();
180181
foreach (array_keys($parameters) as $key) {
181182
if (!in_array($key, $availableKeys, true) && (array_key_exists('optional', $availableKeys) && !in_array(
182-
$key,
183-
$availableKeys['optional'],
184-
true,
185-
))) {
183+
$key,
184+
$availableKeys['optional'],
185+
true,
186+
))) {
186187
throw new \InvalidArgumentException(
187188
sprintf(
188189
'Invalid parameter "%s" for action item "%s", required keys are %s',

src/Resource/EndpointProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace BytesCommerce\SynologyApi\Resource;
66

7+
use BytesCommerce\SynologyApi\Exceptions\NoConnectionException;
78
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
89
use Symfony\Contracts\Cache\ItemInterface;
910
use Webmozart\Assert\Assert;
@@ -41,10 +42,17 @@ public function getEndpoints(string $targetUrl): array
4142

4243
curl_setopt($ch, \CURLOPT_REFERER, $referer);
4344
curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
45+
curl_setopt($ch, \CURLOPT_TIMEOUT, 10);
46+
curl_setopt($ch, \CURLOPT_CONNECTTIMEOUT, 3);
47+
4448
$response = curl_exec($ch);
4549
Assert::same(0, curl_errno($ch));
4650
curl_close($ch);
47-
Assert::string($response);
51+
try {
52+
Assert::string($response);
53+
} catch (Throwable $e) {
54+
throw new NoConnectionException();
55+
}
4856

4957
$result = json_decode($response, true);
5058
if ($result !== null) {

0 commit comments

Comments
 (0)