Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __construct(
private INotificationManager $notificationManager,
private QuotaRuleService $quotaRuleService,
IClientService $clientService,
private bool $isCLI,
) {
$this->client = $clientService->newClient();
}
Expand Down Expand Up @@ -1017,12 +1018,14 @@ public function updateExpImgProcessingTime(int $runtime): void {
* @param string|null $contentType
* @param bool $logErrors if set to false error logs will be suppressed
* @param string|null $serviceType
* @param int $retryCount number of retries that have been attempted so far
* @return array decoded request result or error
* @throws Exception
*/
public function request(
?string $userId, string $endPoint, array $params = [], string $method = 'GET',
?string $contentType = null, bool $logErrors = true, ?string $serviceType = null,
int $retryCount = 0,
): array {
try {
if ($serviceType === Application::SERVICE_TYPE_IMAGE && $this->openAiSettingsService->imageOverrideEnabled()) {
Expand Down Expand Up @@ -1145,6 +1148,33 @@ public function request(
}
return ['body' => $body];
} catch (ClientException|ServerException $e) {
if ($e->getResponse()->getStatusCode() === Http::STATUS_TOO_MANY_REQUESTS) {
if ($retryCount < 3 && $this->isCLI) {
if (empty($e->getResponse()->getHeader('Retry-After'))) {
$sleep = random_int(10, 120);
} else {
$retryAfter = $e->getResponse()->getHeader('Retry-After')[0];
if ((string)(int)$retryAfter !== $retryAfter) {
// if it's not an integer, it might be a date
$retryAfterTime = strtotime($retryAfter);
if ($retryAfterTime !== false) {
$sleep = max(0, $retryAfterTime - time());
} else {
// fallback to random sleep if the header is not parsable
$sleep = random_int(10, 120);
}
} else {
$sleep = (int)$retryAfter;
}
$sleep += random_int(5, 30); // add some jitter to avoid thundering herd problem
}
$this->logger->warning("Rate limit exceeded, retrying in $sleep seconds", ['retry_count' => $retryCount]);
sleep($sleep);
return $this->request($userId, $endPoint, $params, $method, $contentType, $logErrors, $serviceType, $retryCount + 1);
} else {
$this->logger->warning('Rate limit exceeded, maximum retries reached', ['retry_count' => $retryCount]);
}
}
$responseBody = $e->getResponse()->getBody();
$parsedResponseBody = json_decode($responseBody, true);
if ($logErrors) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function setUp(): void {
$this->createMock(\OCP\Notification\IManager::class),
\OCP\Server::get(QuotaRuleService::class),
$clientService,
true,
);

$this->openAiSettingsService->setUserApiKey(self::TEST_USER1, 'This is a PHPUnit test API key');
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Quota/QuotaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected function setUp(): void {
$this->notificationManager,
\OCP\Server::get(QuotaRuleService::class),
\OCP\Server::get(IClientService::class),
true
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit/Service/ServiceOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function setUp(): void {
$this->createMock(\OCP\Notification\IManager::class),
\OCP\Server::get(QuotaRuleService::class),
$clientService,
true
);
}

Expand Down
Loading