Skip to content

Commit

Permalink
fix: correct rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed Aug 5, 2024
1 parent 5663b1a commit 6f3c780
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/Controller/DomainRefreshController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
Expand Down Expand Up @@ -65,10 +64,10 @@ public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain

if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) {
$limiter = $this->rdapRequestsLimiter->create($userId);
try {
$limiter->consume()->ensureAccepted();
} catch (RateLimitExceededException $e) {
throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage());
$limit = $limiter->consume();

if (!$limit->isAccepted()) {
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -57,11 +56,10 @@ public function register(Request $request, UserPasswordHasherInterface $userPass

if (false === $this->kernel->isDebug()) {
$limiter = $this->userRegisterLimiter->create($request->getClientIp());
$limit = $limiter->consume();

try {
$limiter->consume()->ensureAccepted();
} catch (RateLimitExceededException $e) {
throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage());
if (!$limit->isAccepted()) {
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
}
}

Expand Down

0 comments on commit 6f3c780

Please sign in to comment.