Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use He4rt\IntegrationDiscord\Transport\DiscordConnector;
use He4rt\IntegrationDiscord\Transport\Requests\Invites\DeleteInvite;
use He4rt\IntegrationDiscord\Transport\Requests\Invites\ListGuildInvites;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Sleep;
use JsonException;
use Random\RandomException;
use RuntimeException;
use Saloon\Exceptions\Request\FatalRequestException;
use Saloon\Exceptions\Request\RequestException;
use Saloon\Http\Response;
use Throwable;

final readonly class PurgeUnusedInvitesAction
Expand Down Expand Up @@ -111,14 +113,38 @@ private function deleteInvite(string $code): void
return;
}

if ($response->status() === 429 && $attempt < self::MAX_RETRIES) {
$retryAfter = (float) ($response->json('retry_after') ?? 1.0);
$this->jitteredSleep($retryAfter);
if ($response->status() === 429) {
$retryAfter = $this->parseRetryAfter($response);

continue;
if ($retryAfter > 60.0) {
throw new RuntimeException(sprintf('Cloudflare IP ban: Retry-After %ds', (int) $retryAfter));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

if ($attempt < self::MAX_RETRIES) {
$this->jitteredSleep($retryAfter);

continue;
}
}

throw new RuntimeException(sprintf('HTTP %d: %s', $response->status(), $response->body()));
}
}

private function parseRetryAfter(Response $response): float
{
$retryAfter = Arr::get(json_decode($response->body(), true), 'retry_after');

if ($retryAfter !== null) {
return (float) $retryAfter;
}

$header = $response->header('Retry-After');

if ($header !== '' && is_numeric($header)) {
return (float) $header;
}

return 1.0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,53 @@ function makeInvite(string $code, int $maxAge, int $uses, string $channel = 'gen
Sleep::assertSleptTimes(1);
});

it('retries on cloudflare 429 with non-json body and short retry-after header', function (): void {
Sleep::fake();

$invites = [
makeInvite('aaa', maxAge: 0, uses: 0),
];

$mockClient = new MockClient([
MockResponse::make($invites),
MockResponse::make('error code: 1015', 429, ['Retry-After' => '5']),
MockResponse::make([], 204),
]);

$connector = new DiscordConnector('test-token');
$connector->withMockClient($mockClient);

$action = new PurgeUnusedInvitesAction($connector);
$result = $action->execute('guild-123', dryRun: false);

expect($result->deleted)->toBe(1)
->and($result->failed)->toBe(0);
});

it('aborts immediately on cloudflare ip ban', function (): void {
Sleep::fake();

$invites = [
makeInvite('aaa', maxAge: 0, uses: 0),
];

$mockClient = new MockClient([
MockResponse::make($invites),
MockResponse::make('error code: 1015', 429, ['Retry-After' => '80974']),
]);

$connector = new DiscordConnector('test-token');
$connector->withMockClient($mockClient);

$action = new PurgeUnusedInvitesAction($connector);
$result = $action->execute('guild-123', dryRun: false);

expect($result->deleted)->toBe(0)
->and($result->failed)->toBe(1);

Sleep::assertSleptTimes(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
});

it('fails after exhausting retries on persistent 429', function (): void {
Sleep::fake();

Expand Down