From 5bd23ea79397485b2045d65e0138cf10184a663e Mon Sep 17 00:00:00 2001 From: diosmosis Date: Thu, 13 Aug 2020 07:35:15 -0700 Subject: [PATCH] Use exponential backoff for when GA fails too. --- Google/GoogleAnalyticsQueryService.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Google/GoogleAnalyticsQueryService.php b/Google/GoogleAnalyticsQueryService.php index e77efb218..cee909114 100644 --- a/Google/GoogleAnalyticsQueryService.php +++ b/Google/GoogleAnalyticsQueryService.php @@ -25,6 +25,7 @@ class GoogleAnalyticsQueryService const MAX_ATTEMPTS = 30; const MAX_BACKOFF_TIME = 60; const PING_MYSQL_EVERY = 25; + const DEFAULT_MIN_BACKOFF_TIME = 2; // start at 2s since GA seems to have trouble w/ the 10 requests per 100s limit w/ 1 private static $problematicMetrics = [ 'ga:users', @@ -54,7 +55,7 @@ class GoogleAnalyticsQueryService /** * @var int */ - private $currentBackoffTime = 1; + private $currentBackoffTime = self::DEFAULT_MIN_BACKOFF_TIME; private $pingMysqlEverySecs; @@ -146,7 +147,7 @@ private function gaBatchGet(Date $date, $metricNamesChunk, $options, $orderByMet $request = $this->googleQueryObjectFactory->make($this->viewId, $date, $metricNamesChunk, $options); - $this->currentBackoffTime = 1; + $this->currentBackoffTime = self::DEFAULT_MIN_BACKOFF_TIME; $attempts = 0; while ($attempts < self::MAX_ATTEMPTS) { @@ -159,7 +160,8 @@ private function gaBatchGet(Date $date, $metricNamesChunk, $options, $orderByMet if (empty($result)) { ++$attempts; - sleep(1); + + $this->backOff(); $this->logger->info("Google Analytics API returned null for some reason, trying again..."); @@ -180,13 +182,14 @@ private function gaBatchGet(Date $date, $metricNamesChunk, $options, $orderByMet ++$attempts; $this->logger->debug("Waiting {$this->currentBackoffTime}s before trying again..."); - $this->sleep($this->currentBackoffTime); - $this->currentBackoffTime = min(self::MAX_BACKOFF_TIME, $this->currentBackoffTime * 2); + $this->backOff(); } else if ($ex->getCode() >= 500) { ++$attempts; + $this->logger->info("Google Analytics API returned error: {$ex->getMessage()}. Waiting one minute before trying again..."); - $this->sleep(60); + + $this->backOff(); } else { throw $ex; } @@ -224,4 +227,10 @@ private function sleep($time) $this->issuePointlessMysqlQuery(); } } + + private function backOff() + { + $this->sleep($this->currentBackoffTime); + $this->currentBackoffTime = min(self::MAX_BACKOFF_TIME, $this->currentBackoffTime * 2); + } }