From 6256ed8604ff749e2cf6bc1ee236784e4fd1fe8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Tue, 28 Oct 2025 13:08:16 +0100 Subject: [PATCH] refactor: remove rate limiter and use retry strategy --- config/packages/messenger.yaml | 6 +--- config/packages/rate_limiter.yaml | 5 --- .../ProcessWatchlistHandler.php | 3 +- src/MessageHandler/UpdateDomainHandler.php | 33 +++++++++---------- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index 7ba03ff..15ccfa9 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -14,8 +14,7 @@ framework: retry_strategy: delay: 1000 multiplier: 2 - max_retries: 10 - max_delay: 10000 + max_delay: 86400000 failed: 'doctrine://default?queue_name=failed' # sync: 'sync://' @@ -37,6 +36,3 @@ framework: App\Message\UpdateRdapServers: async App\Message\ValidateConnectorCredentials: async App\Message\UpdateDomain: rdap_async - - # Route your messages to the transports - # 'App\Message\YourMessage': async diff --git a/config/packages/rate_limiter.yaml b/config/packages/rate_limiter.yaml index b93e8c5..6c0af89 100644 --- a/config/packages/rate_limiter.yaml +++ b/config/packages/rate_limiter.yaml @@ -24,8 +24,3 @@ framework: policy: sliding_window limit: 10 interval: '1 hour' - - rdap_requests: - policy: sliding_window - limit: 2 - interval: '1 second' diff --git a/src/MessageHandler/ProcessWatchlistHandler.php b/src/MessageHandler/ProcessWatchlistHandler.php index 3a14848..0af5c26 100644 --- a/src/MessageHandler/ProcessWatchlistHandler.php +++ b/src/MessageHandler/ProcessWatchlistHandler.php @@ -79,8 +79,7 @@ final readonly class ProcessWatchlistHandler */ /** @var Domain $domain */ - foreach ($watchlist->getDomains()->filter(fn ($domain) => $this->RDAPService->isToBeUpdated($domain, false, null !== $watchlist->getConnector())) as $domain - ) { + foreach ($watchlist->getDomains()->filter(fn ($domain) => $this->RDAPService->isToBeUpdated($domain, false, null !== $watchlist->getConnector())) as $domain) { $this->bus->dispatch(new UpdateDomain($domain->getLdhName(), $watchlist->getToken())); } } diff --git a/src/MessageHandler/UpdateDomainHandler.php b/src/MessageHandler/UpdateDomainHandler.php index 720a4be..2aef5eb 100644 --- a/src/MessageHandler/UpdateDomainHandler.php +++ b/src/MessageHandler/UpdateDomainHandler.php @@ -3,6 +3,7 @@ namespace App\MessageHandler; use App\Entity\RdapServer; +use App\Entity\Watchlist; use App\Exception\DomainNotFoundException; use App\Exception\MalformedDomainException; use App\Exception\TldNotSupportedException; @@ -22,11 +23,9 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Exception\ExceptionInterface; -use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Mime\Address; use Symfony\Component\Notifier\Recipient\Recipient; -use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -46,7 +45,7 @@ final readonly class UpdateDomainHandler private MessageBusInterface $bus, private WatchlistRepository $watchlistRepository, private DomainRepository $domainRepository, - private RateLimiterFactory $rdapRequestsLimiter, private LoggerInterface $logger, + private LoggerInterface $logger, ) { $this->sender = new Address($mailerSenderEmail, $mailerSenderName); } @@ -63,6 +62,7 @@ final readonly class UpdateDomainHandler * @throws ServerExceptionInterface * @throws MalformedDomainException * @throws ExceptionInterface + * @throws \Exception */ public function __invoke(UpdateDomain $message): void { @@ -76,22 +76,17 @@ final readonly class UpdateDomainHandler return; } - $limiter = $this->rdapRequestsLimiter->create($rdapServer->getUrl()); - $limit = $limiter->consume(); - - if (!$limit->isAccepted()) { - $retryAfter = $limit->getRetryAfter()->getTimestamp() - time(); - - $this->logger->warning('Security rate limit reached for this RDAP server', [ - 'url' => $rdapServer->getUrl(), - 'retryAfter' => $retryAfter, - ]); - - throw new RecoverableMessageHandlingException('Rate limit reached', 0, null, $retryAfter); - } $watchlist = $this->watchlistRepository->findOneBy(['token' => $message->watchlistToken]); + if (!$this->RDAPService->isToBeUpdated($domain, false, null !== $watchlist->getConnector())) { + $this->logger->debug('The domain name is already present in the database and does not need to be updated at this time', [ + 'ldhName' => $domain->getLdhName(), + ]); + + return; + } + $updatedAt = $domain->getUpdatedAt(); $deleted = $domain->getDeleted(); @@ -101,7 +96,6 @@ final readonly class UpdateDomainHandler * We send messages that correspond to the sending of notifications that will not be processed here. */ $this->RDAPService->registerDomain($domain->getLdhName()); - $this->bus->dispatch(new DetectDomainChange($watchlist->getToken(), $domain->getLdhName(), $updatedAt)); } catch (DomainNotFoundException) { $newDomain = $this->domainRepository->findOneBy(['ldhName' => $domain->getLdhName()]); @@ -123,5 +117,10 @@ final readonly class UpdateDomainHandler * In this case, the domain name can no longer be updated. Unfortunately, there is nothing more that can be done. */ } + + /** @var Watchlist $wl */ + foreach ($domain->getWatchlists()->getIterator() as $wl) { + $this->bus->dispatch(new DetectDomainChange($wl->getToken(), $domain->getLdhName(), $updatedAt)); + } } }