Skip to content

Commit 190c9f8

Browse files
Address Comments
1 parent a4f1e98 commit 190c9f8

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/Client.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Client
3232
private string $userAgent = '';
3333
private int $maxRetries = 0;
3434
private int $retryDelay = 1000; // milliseconds
35+
private array $retryStatusCodes = [500, 503];
3536

3637
/**
3738
* @param string $key
@@ -131,6 +132,18 @@ public function setRetryDelay(int $retryDelay): self
131132
return $this;
132133
}
133134

135+
/**
136+
* Set the retry status codes.
137+
*
138+
* @param array<int> $retryStatusCodes
139+
* @return self
140+
*/
141+
public function setRetryStatusCodes(array $retryStatusCodes): self
142+
{
143+
$this->retryStatusCodes = $retryStatusCodes;
144+
return $this;
145+
}
146+
134147
/**
135148
* Flatten request body array to PHP multiple format
136149
*
@@ -161,14 +174,14 @@ private static function flatten(array $data, string $prefix = ''): array
161174
* @return mixed
162175
* @throws \Exception
163176
*/
164-
private function retry(callable $callback): mixed
177+
private function withRetries(callable $callback): mixed
165178
{
166179
$attempts = 1;
167180

168181
while (true) {
169182
$res = $callback();
170183

171-
if ($res->getStatusCode() !== 500 && $res->getStatusCode() !== 503 || $attempts >= $this->maxRetries) {
184+
if (!in_array($res->getStatusCode(), $this->retryStatusCodes) || $attempts >= $this->maxRetries) {
172185
return $res;
173186
}
174187

@@ -265,7 +278,7 @@ public function fetch(
265278
};
266279

267280
if ($this->maxRetries > 0) {
268-
$response = $this->retry($sendRequest);
281+
$response = $this->withRetries($sendRequest);
269282
} else {
270283
$response = $sendRequest();
271284
}
@@ -342,4 +355,14 @@ public function getRetryDelay(): int
342355
{
343356
return $this->retryDelay;
344357
}
358+
359+
/**
360+
* Get the retry status codes.
361+
*
362+
* @return array<int>
363+
*/
364+
public function getRetryStatusCodes(): array
365+
{
366+
return $this->retryStatusCodes;
367+
}
345368
}

tests/ClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,22 @@ public function testRetryWithDelay(): void
400400
$this->assertEquals(200, $res->getStatusCode());
401401
unlink(__DIR__ . '/state.json');
402402
}
403+
404+
/**
405+
* Test custom retry status codes
406+
* @return void
407+
*/
408+
public function testCustomRetryStatusCodes(): void
409+
{
410+
$client = new Client();
411+
$client->setMaxRetries(3);
412+
$client->setRetryDelay(3000);
413+
$client->setRetryStatusCodes([401]);
414+
$now = microtime(true);
415+
416+
$res = $client->fetch('localhost:8000/mock-retry-401');
417+
$this->assertEquals(200, $res->getStatusCode());
418+
$this->assertGreaterThan($now + 3.0, microtime(true));
419+
unlink(__DIR__ . '/state.json');
420+
}
403421
}

tests/router.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ function setState(array $newState): void
6767
throw new \Exception('Mock retry error');
6868
}
6969

70+
$body = json_encode([
71+
'success' => true,
72+
'attempts' => $state['attempts']
73+
]);
74+
} elseif ($curPageName == 'mock-retry-401') {
75+
$state = getState();
76+
$state['attempts'] = isset($state['attempts']) ? $state['attempts'] + 1 : 1;
77+
setState($state);
78+
79+
if ($state['attempts'] <= 2) {
80+
http_response_code(401);
81+
throw new \Exception('Mock retry error');
82+
}
83+
7084
$body = json_encode([
7185
'success' => true,
7286
'attempts' => $state['attempts']

0 commit comments

Comments
 (0)