Sfoglia il codice sorgente

Limits HelloAsso token refresh API calls

Limits the number of HelloAsso API calls during token refresh
to prevent exceeding rate limits.
Instead of hourly rate limiting with timed sleeps, limits by cronjob execution.
Also, replaces UI prints with logger info.
Olivier Massot 2 mesi fa
parent
commit
5c62644422
1 ha cambiato i file con 19 aggiunte e 28 eliminazioni
  1. 19 28
      src/Service/Cron/Job/RefreshHelloassoTokens.php

+ 19 - 28
src/Service/Cron/Job/RefreshHelloassoTokens.php

@@ -34,9 +34,9 @@ class RefreshHelloassoTokens extends BaseCronJob
     private const REFRESH_OLDER_THAN = 24;
 
     /**
-     * Limite à poser au nombre d'appels API par heure.
+     * Limite à poser au nombre d'appels API par exécution du cronjob.
      */
-    private const HOURLY_LIMIT = 50;
+    private const CALLS_LIMIT = 10;
 
     #[Pure]
     public function __construct(
@@ -80,49 +80,40 @@ class RefreshHelloassoTokens extends BaseCronJob
         $amount = count($helloAssoEntities);
 
         if ($amount === 0) {
-            $this->ui->print('No tokens to refresh');
+            $this->logger->info('No tokens to refresh');
             return;
         }
 
-        $sleepingTime = $this->sleepingTime($amount);
+        $this->logger->info($amount . ' tokens to refresh');
 
-        $this->ui->print($amount . ' tokens to refresh');
+        $i = 0;
 
         foreach ($helloAssoEntities as $helloAssoEntity) {
-            $this->ui->print(
+            $this->logger->info(
                 ' * Refresh token for organization '.$helloAssoEntity->getOrganization()->getId().' : '.$helloAssoEntity->getRefreshTokenCreatedAt()->format('Y-m-d H:i:s')
             );
 
             $this->helloAssoService->refreshTokens($helloAssoEntity);
 
-            sleep($sleepingTime);
+            $i++;
+
+            if ($i >= self::CALLS_LIMIT) {
+                if ($amount > self::CALLS_LIMIT) {
+                    $this->logger->warning('API calls limit reached, aborting');
+                }
+                return;
+            }
+
+            sleep(1);
         }
+
+        $this->logger->info('Tokens refreshed');
     }
 
-    public function getHelloassoAccounts(): array
+    protected function getHelloassoAccounts(): array
     {
         $helloassoRepository = $this->entityManager->getRepository(HelloAsso::class);
 
         return $helloassoRepository->findOldRefreshTokens(self::REFRESH_OLDER_THAN);
     }
-
-    protected function sleepingTime($amount): int
-    {
-        if ($amount >= 50) {
-            // Une attente de 80s entre chaque appel donne un nombre d'appels max de 45 par heure (max 50 par heure).
-            return 80;
-        }
-
-        if ($amount >= 20) {
-            // Une attente de 35s entre chaque appel donne un nombre d'appels max de 17 par 10 minutes (max 20 par 10 minutes).
-            return 35;
-        }
-
-        if ($amount >= 10) {
-            // Une attente de 2s entre chaque appel donne un nombre d'appels max de 5 par 10s (max 10 par 10s).
-            return 2;
-        }
-
-        return 1;
-    }
 }