Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-buckets-gzip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-php': patch
---

Fall back to uncompressed batch uploads when local gzip compression fails.
15 changes: 9 additions & 6 deletions lib/Consumer/ForkCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ public function flushBatch($messages)
$cmd2 = "echo " . $payload . " | gzip > " . $tmpfname;
exec($cmd2, $output, $exit);

if (0 != $exit) {
if (0 == $exit) {
$cmd .= " -H 'Content-Encoding: gzip'";
$cmd .= " --data-binary '@" . $tmpfname . "'";
} else {
$this->handleError($exit, $output);
return self::FLUSH_BATCH_NON_RETRYABLE_FAILURE;
if (is_file($tmpfname)) {
unlink($tmpfname);
}
$tmpfname = "";
$cmd .= " -d " . $payload;
}

$cmd .= " -H 'Content-Encoding: gzip'";

$cmd .= " --data-binary '@" . $tmpfname . "'";
} else {
$cmd .= " -d " . $payload;
}
Expand Down
21 changes: 15 additions & 6 deletions lib/Consumer/LibCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,34 @@ public function flushBatch($messages)
return self::FLUSH_BATCH_NON_RETRYABLE_FAILURE;
}

$isCompressed = false;
if ($this->compress_request) {
$payload = gzencode($payload);
$compressedPayload = gzencode($payload);

if (false === $payload) {
return self::FLUSH_BATCH_NON_RETRYABLE_FAILURE;
if (false !== $compressedPayload) {
$payload = $compressedPayload;
$isCompressed = true;
} else {
$this->handleError(0, "Failed to gzip batch payload; sending uncompressed.");
}
}
Comment thread
marandaneto marked this conversation as resolved.

$shouldVerify = $this->options['verify_batch_events_request'] ?? true;
$requestOptions = [
'shouldVerify' => $shouldVerify,
];
if ($this->compress_request) {
$requestOptions['compressRequest'] = $isCompressed;
}

$response = $this->httpClient->sendRequest(
'/batch/',
$payload,
[
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231.
"User-Agent: {$this->userAgent()}",
],
[
'shouldVerify' => $shouldVerify,
]
$requestOptions
);

if (!$shouldVerify) {
Expand Down
14 changes: 8 additions & 6 deletions lib/Consumer/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,17 @@ private function createBody($host, $content)
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231.
$req .= "User-Agent: " . $this->userAgent() . "\r\n";

// Compress content if compress_request is true
// Compress content if compress_request is true. If local compression fails,
// keep sending the original uncompressed payload.
if ($this->compress_request) {
$content = gzencode($content);
$compressedContent = gzencode($content);

if (false === $content) {
return false;
if (false !== $compressedContent) {
$content = $compressedContent;
$req .= "Content-Encoding: gzip\r\n";
} else {
$this->handleError(0, "Failed to gzip batch payload; sending uncompressed.");
}

$req .= "Content-Encoding: gzip\r\n";
}

$req .= "Content-length: " . strlen($content) . "\r\n";
Expand Down
6 changes: 4 additions & 2 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function __construct(
* shouldRetry?: bool,
* shouldVerify?: bool,
* includeEtag?: bool,
* timeout?: int
* timeout?: int,
* compressRequest?: bool
* } $requestOptions
* @return HttpResponse
*/
Expand All @@ -98,6 +99,7 @@ public function sendRequest(string $path, ?string $payload, array $extraHeaders
$shouldRetry = $requestOptions['shouldRetry'] ?? true;
$shouldVerify = $requestOptions['shouldVerify'] ?? true;
$includeEtag = $requestOptions['includeEtag'] ?? false;
$compressRequest = $requestOptions['compressRequest'] ?? $this->compressRequests;

do {
// open connection
Expand All @@ -109,7 +111,7 @@ public function sendRequest(string $path, ?string $payload, array $extraHeaders

$headers = [];
$headers[] = 'Content-Type: application/json';
if ($this->compressRequests) {
if ($compressRequest) {
$headers[] = 'Content-Encoding: gzip';
}

Expand Down
Loading