Skip to content

Commit

Permalink
Merge pull request #106 from matomo-org/expo-backof
Browse files Browse the repository at this point in the history
Use exponential backoff for when GA fails too.
  • Loading branch information
diosmosis authored Aug 13, 2020
2 parents 82208d6 + 5bd23ea commit 087d5dd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Google/GoogleAnalyticsQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -54,7 +55,7 @@ class GoogleAnalyticsQueryService
/**
* @var int
*/
private $currentBackoffTime = 1;
private $currentBackoffTime = self::DEFAULT_MIN_BACKOFF_TIME;

private $pingMysqlEverySecs;

Expand Down Expand Up @@ -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) {
Expand All @@ -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...");

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 087d5dd

Please sign in to comment.