From 949924c799e4e3ae6bcff20c499878d4d43817c1 Mon Sep 17 00:00:00 2001 From: vpapaloukas Date: Fri, 24 Mar 2023 15:17:25 +0200 Subject: [PATCH 1/2] add support for CURLOPT_CONNECTTIMEOUT --- MatomoTracker.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/MatomoTracker.php b/MatomoTracker.php index 62d21dd..6336f20 100644 --- a/MatomoTracker.php +++ b/MatomoTracker.php @@ -161,6 +161,7 @@ public function __construct($idSite, $apiUrl = '') // Allow debug while blocking the request $this->requestTimeout = 600; + $this->requestConnectTimeout = 300; $this->doBulkRequests = false; $this->storedTrackingActions = []; @@ -1697,6 +1698,32 @@ public function setRequestTimeout($timeout) return $this; } + /** + * Returns the maximum number of seconds the tracker will spend trying to connect to Matomo. + * Defaults to 0 seconds (unlimited). + */ + public function getRequestConnectTimeout() + { + return $this->requestConnectTimeout; + } + + /** + * Sets the maximum number of seconds that the tracker will spend tryint to connect to Matomo. + * + * @param int $timeout + * @return $this + * @throws Exception + */ + public function setRequestConnectTimeout($timeout) + { + if (!is_int($timeout) || $timeout < 0) { + throw new Exception("Invalid value supplied for request connect timeout: $timeout"); + } + + $this->requestConnectTimeout = $timeout; + return $this; + } + /** * Sets the request method to POST, which is recommended when using setTokenAuth() * to prevent the token from being recorded in server logs. Avoid using redirects @@ -1752,6 +1779,7 @@ protected function prepareCurlOptions($url, $method, $data, $forcePostUrlEncoded CURLOPT_USERAGENT => $this->userAgent, CURLOPT_HEADER => true, CURLOPT_TIMEOUT => $this->requestTimeout, + CURLOPT_CONNECTTIMEOUT => $this->requestConnectTimeout, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Accept-Language: ' . $this->acceptLanguage, From 390a90d6fa6b83691121c8e41aeb2e77d6030c01 Mon Sep 17 00:00:00 2001 From: vpapaloukas Date: Fri, 24 Mar 2023 16:03:13 +0200 Subject: [PATCH 2/2] fix getRequestConnectTimeout docblock --- MatomoTracker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MatomoTracker.php b/MatomoTracker.php index 6336f20..1a06676 100644 --- a/MatomoTracker.php +++ b/MatomoTracker.php @@ -1700,7 +1700,7 @@ public function setRequestTimeout($timeout) /** * Returns the maximum number of seconds the tracker will spend trying to connect to Matomo. - * Defaults to 0 seconds (unlimited). + * Defaults to 300 seconds. */ public function getRequestConnectTimeout() {