Add 10 second timeout to matomo requests

This commit is contained in:
Alejandro Celaya 2023-11-21 08:34:37 +01:00
parent c03eea789c
commit 316b88cea6
2 changed files with 14 additions and 6 deletions

View file

@ -9,6 +9,8 @@ use Shlinkio\Shlink\Core\Exception\RuntimeException;
class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
{ {
public const MATOMO_DEFAULT_TIMEOUT = 10; // Time in seconds
public function __construct(private readonly MatomoOptions $options) public function __construct(private readonly MatomoOptions $options)
{ {
} }
@ -27,12 +29,16 @@ class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
// Create a new MatomoTracker on every request, because it infers request info during construction // Create a new MatomoTracker on every request, because it infers request info during construction
$tracker = new MatomoTracker($siteId, $this->options->baseUrl); $tracker = new MatomoTracker($siteId, $this->options->baseUrl);
$tracker
// Token required to set the IP and location // Token required to set the IP and location
$tracker->setTokenAuth($this->options->apiToken); ->setTokenAuth($this->options->apiToken)
// We don't want to bulk send, as every request to Shlink will create a new tracker
$tracker->disableBulkTracking();
// Ensure params are not sent in the URL, for security reasons // Ensure params are not sent in the URL, for security reasons
$tracker->setRequestMethodNonBulk('POST'); ->setRequestMethodNonBulk('POST')
// Set a reasonable timeout
->setRequestTimeout(self::MATOMO_DEFAULT_TIMEOUT)
->setRequestConnectTimeout(self::MATOMO_DEFAULT_TIMEOUT)
// We don't want to bulk send, as every request to Shlink will create a new tracker
->disableBulkTracking();
return $tracker; return $tracker;
} }

View file

@ -39,6 +39,8 @@ class MatomoTrackerBuilderTest extends TestCase
self::assertEquals('api_token', $tracker->token_auth); // @phpstan-ignore-line self::assertEquals('api_token', $tracker->token_auth); // @phpstan-ignore-line
self::assertEquals(5, $tracker->idSite); // @phpstan-ignore-line self::assertEquals(5, $tracker->idSite); // @phpstan-ignore-line
self::assertEquals(MatomoTrackerBuilder::MATOMO_DEFAULT_TIMEOUT, $tracker->getRequestTimeout());
self::assertEquals(MatomoTrackerBuilder::MATOMO_DEFAULT_TIMEOUT, $tracker->getRequestConnectTimeout());
} }
private function builder(?MatomoOptions $options = null): MatomoTrackerBuilder private function builder(?MatomoOptions $options = null): MatomoTrackerBuilder