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
{
public const MATOMO_DEFAULT_TIMEOUT = 10; // Time in seconds
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
$tracker = new MatomoTracker($siteId, $this->options->baseUrl);
// Token required to set the IP and location
$tracker->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
$tracker->setRequestMethodNonBulk('POST');
$tracker
// Token required to set the IP and location
->setTokenAuth($this->options->apiToken)
// Ensure params are not sent in the URL, for security reasons
->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;
}

View file

@ -39,6 +39,8 @@ class MatomoTrackerBuilderTest extends TestCase
self::assertEquals('api_token', $tracker->token_auth); // @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