Skip to content

Commit a048153

Browse files
feat: Implement retry
1 parent f8630c6 commit a048153

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "syntaxphoenix/bgpview-php",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"type": "framework",
55
"autoload": {
66
"psr-4": {

src/BGPView.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SyntaxPhoenix\Api\BGPView;
44

5+
use Exception;
56
use GuzzleHttp\Client;
67
use Nette\Caching\Cache;
78
use Nette\Caching\Storage;
@@ -19,8 +20,10 @@ class BGPView
1920
private string $cacheTime;
2021
private float $lastRequestTimestamp = 0;
2122
private float $requestTimeout;
23+
private int $failures;
24+
private int $maxFailures;
2225

23-
public function __construct(string $baseUrl, float $requestTimeout = 0.4, bool $caching = false, ?string $cachingUrl = 'web/cache/bgpview', ?string $cacheTime = '10 minutes') {
26+
public function __construct(string $baseUrl, float $requestTimeout = 0.4, bool $caching = false, ?string $cachingUrl = 'web/cache/bgpview', ?string $cacheTime = '10 minutes', int $maxFailures = 3) {
2427
if ($caching) {
2528
$this->createPath($cachingUrl);
2629
$this->storage = new FileStorage($cachingUrl);
@@ -30,6 +33,7 @@ public function __construct(string $baseUrl, float $requestTimeout = 0.4, bool $
3033
$this->caching = $caching;
3134
$this->cacheTime = $cacheTime;
3235
$this->requestTimeout = $requestTimeout;
36+
$this->maxFailures = $maxFailures;
3337
$this->client = new Client([
3438
'base_uri' => $baseUrl
3539
]);
@@ -84,11 +88,20 @@ private function getDataByApi(string $urlPart): array
8488
}
8589
}
8690
$time = microtime(true);
87-
if (($this->lastRequestTimestamp + $this->requestTimeout) > $time) {
88-
$restTime = $this->requestTimeout - ($time - $this->lastRequestTimestamp);
91+
if ($this->lastRequestTimestamp > $time) {
92+
$restTime = $this->lastRequestTimestamp - $time;
8993
usleep($restTime * 1000 * 1000);
9094
}
91-
$response = $this->client->request('GET', $urlPart);
95+
try {
96+
$response = $this->client->request('GET', $urlPart);
97+
} catch (Exception $exception) {
98+
$this->failures++;
99+
if ($this->failures > $this->maxFailures) {
100+
throw new RequestFailedException();
101+
}
102+
$this->lastRequestTimestamp = microtime(true) + ($this->requestTimeout * ($this->failures + 1));
103+
return $this->getDataByApi($urlPart);
104+
}
92105
if ($response->getStatusCode() != 200) {
93106
throw new RequestFailedException();
94107
}
@@ -98,7 +111,8 @@ private function getDataByApi(string $urlPart): array
98111
Cache::EXPIRE => $this->cacheTime ?? '10 minutes',
99112
]);
100113
}
101-
$this->lastRequestTimestamp = microtime(true);
114+
$this->lastRequestTimestamp = microtime(true); + $this->requestTimeout;
115+
$this->failures = 0;
102116

103117
return $finalResponse;
104118
}
@@ -111,4 +125,4 @@ private function createPath($path) {
111125
$return = $this->createPath($prev_path);
112126
return ($return && is_writable($prev_path)) ? mkdir($path) : false;
113127
}
114-
}
128+
}

0 commit comments

Comments
 (0)