Skip to content

Commit 65fb739

Browse files
committed
feature #723 [Platform] Use int for retry after (VincentLanglet)
This PR was merged into the main branch. Discussion ---------- [Platform] Use int for retry after | Q | A | ------------- | --- | Bug fix? | not really | New feature? | no | Docs? | no <!-- required for new features --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT According to RFC 7231 should be an integer https://datatracker.ietf.org/doc/html/rfc7231#page-69 So let's avoid a useless float here. Commits ------- d968812 Use int for retry after
2 parents 65b96e3 + d968812 commit 65fb739

File tree

5 files changed

+14
-19
lines changed

5 files changed

+14
-19
lines changed

src/platform/src/Bridge/Anthropic/ResultConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function convert(RawHttpResult|RawResultInterface $result, array $options
4242

4343
if (429 === $response->getStatusCode()) {
4444
$retryAfter = $response->getHeaders(false)['retry-after'][0] ?? null;
45-
$retryAfterValue = $retryAfter ? (float) $retryAfter : null;
45+
$retryAfterValue = $retryAfter ? (int) $retryAfter : null;
4646
throw new RateLimitExceededException($retryAfterValue);
4747
}
4848

src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
5858

5959
if (429 === $response->getStatusCode()) {
6060
$headers = $response->getHeaders(false);
61-
$resetTime = null;
61+
$resetTime = $headers['x-ratelimit-reset-requests'][0]
62+
?? $headers['x-ratelimit-reset-tokens'][0]
63+
?? null;
6264

63-
if (isset($headers['x-ratelimit-reset-requests'][0])) {
64-
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-requests'][0]);
65-
} elseif (isset($headers['x-ratelimit-reset-tokens'][0])) {
66-
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-tokens'][0]);
67-
}
68-
69-
throw new RateLimitExceededException($resetTime);
65+
throw new RateLimitExceededException($resetTime ? self::parseResetTime($resetTime) : null);
7066
}
7167

7268
if ($options['stream'] ?? false) {
@@ -221,16 +217,15 @@ private function convertToolCall(array $toolCall): ToolCall
221217
* - "6m0s"
222218
* - "2m30s"
223219
*/
224-
private static function parseResetTime(string $resetTime): float
220+
private static function parseResetTime(string $resetTime): ?int
225221
{
226-
$seconds = 0;
227-
228222
if (preg_match('/^(?:(\d+)m)?(?:(\d+)s)?$/', $resetTime, $matches)) {
229223
$minutes = isset($matches[1]) ? (int) $matches[1] : 0;
230224
$secs = isset($matches[2]) ? (int) $matches[2] : 0;
231-
$seconds = ($minutes * 60) + $secs;
225+
226+
return ($minutes * 60) + $secs;
232227
}
233228

234-
return (float) $seconds;
229+
return null;
235230
}
236231
}

src/platform/src/Exception/RateLimitExceededException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
final class RateLimitExceededException extends RuntimeException
1818
{
1919
public function __construct(
20-
private readonly ?float $retryAfter = null,
20+
private readonly ?int $retryAfter = null,
2121
) {
2222
parent::__construct('Rate limit exceeded.');
2323
}
2424

25-
public function getRetryAfter(): ?float
25+
public function getRetryAfter(): ?int
2626
{
2727
return $this->retryAfter;
2828
}

src/platform/tests/Bridge/Anthropic/ResultConverterRateLimitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testRateLimitExceededThrowsException()
4040
try {
4141
$handler->convert(new RawHttpResult($httpResponse));
4242
} catch (RateLimitExceededException $e) {
43-
$this->assertSame(60.0, $e->getRetryAfter());
43+
$this->assertSame(60, $e->getRetryAfter());
4444
throw $e;
4545
}
4646
}

src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterRateLimitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testRateLimitExceededWithRequestsResetTime()
4242
try {
4343
$handler->convert(new RawHttpResult($httpResponse));
4444
} catch (RateLimitExceededException $e) {
45-
$this->assertSame(20.0, $e->getRetryAfter());
45+
$this->assertSame(20, $e->getRetryAfter());
4646
throw $e;
4747
}
4848
}
@@ -69,7 +69,7 @@ public function testRateLimitExceededWithTokensResetTime()
6969
try {
7070
$handler->convert(new RawHttpResult($httpResponse));
7171
} catch (RateLimitExceededException $e) {
72-
$this->assertSame(150.0, $e->getRetryAfter());
72+
$this->assertSame(150, $e->getRetryAfter());
7373
throw $e;
7474
}
7575
}

0 commit comments

Comments
 (0)