From 961baf95b9decff3e6cdeb845c9008db0e4f5b8d Mon Sep 17 00:00:00 2001 From: Joris Berthelot Date: Thu, 19 May 2022 20:39:39 +0200 Subject: [PATCH] Allow to configure the HttpClient maximumBackoffDuration --- lib/Client.php | 2 +- lib/HttpClient.php | 6 +++--- lib/PostHog.php | 2 +- lib/QueueConsumer.php | 4 ++++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index dc6c486..c04ab1d 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -53,7 +53,7 @@ public function __construct(string $apiKey, array $options = [], ?HttpClient $ht $this->httpClient = $httpClient !== null ? $httpClient : new HttpClient( $options['host'] ?? "app.posthog.com", $options['ssl'] ?? true, - 10000, + (int) ($options['maximum_backoff_duration'] ?? 10000), false, $options["debug"] ?? false ); diff --git a/lib/HttpClient.php b/lib/HttpClient.php index c4ee81e..43099b8 100644 --- a/lib/HttpClient.php +++ b/lib/HttpClient.php @@ -68,9 +68,9 @@ public function sendRequest(string $path, ?string $payload, array $extraHeaders { $protocol = $this->useSsl ? "https://" : "http://"; - $backoff = 100; // Set initial waiting time to 100ms + $backoff = 100; // Set initial waiting time to 100ms - while ($backoff < $this->maximumBackoffDuration) { + do { // open connection $ch = curl_init(); @@ -115,7 +115,7 @@ public function sendRequest(string $path, ?string $payload, array $extraHeaders } else { break; // no error } - } + } while ($backoff < $this->maximumBackoffDuration); return $httpResponse; } diff --git a/lib/PostHog.php b/lib/PostHog.php index 455defa..30b3156 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -6,7 +6,7 @@ class PostHog { - public const VERSION = '2.1.0'; + public const VERSION = '2.1.1'; public const ENV_API_KEY = "POSTHOG_API_KEY"; public const ENV_HOST = "POSTHOG_HOST"; diff --git a/lib/QueueConsumer.php b/lib/QueueConsumer.php index 120bfb6..354e12c 100644 --- a/lib/QueueConsumer.php +++ b/lib/QueueConsumer.php @@ -30,6 +30,10 @@ public function __construct($apiKey, $options = array()) $this->batch_size = $options["batch_size"]; } + if (isset($options["maximum_backoff_duration"])) { + $this->maximum_backoff_duration = (int) $options["maximum_backoff_duration"]; + } + if (isset($options["host"])) { $this->host = $options["host"];