Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): accept a float start time on 32-bit PHP

Yudhi Armyndharisc962b62509b0
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index a2af599d2..70a9ad9a4 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -300,8 +300,10 @@ class HttpExecutor
* (before the headers on Guzzle 8, in the middle of the body on Guzzle 7): no errno, no timeout text.
* Such a failure is a timeout when it took at least the whole timeout. cURL failures carry their own
* "cURL error" text and are left alone, and a timeout of 0 (no bound) never matches.
+ *
+ * The start is hrtime(true), an int on 64-bit PHP and a float on 32-bit PHP.
*/
- private function failedAfterTimeout(\Throwable $e, int $startedNs): bool
+ private function failedAfterTimeout(\Throwable $e, int|float $startedNs): bool
{
return $this->timeout > 0
&& (hrtime(true) - $startedNs) / 1e9 >= $this->timeout
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index 795e8809a..93df47ed2 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -718,6 +718,27 @@ class ClientTest extends TestCase
}
}
+ /**
+ * The start is hrtime(true): an int on 64-bit PHP and a float on 32-bit PHP. The closure calls the check
+ * from this strict file, as the executor itself does.
+ */
+ public function testTheElapsedTimeCheckTakesAnIntOrAFloatStart(): void
+ {
+ $executor = new \OpenWA\Http\HttpExecutor('http://localhost:2785', 'k', 1.0);
+ $failure = new \GuzzleHttp\Exception\ConnectException('Connection refused', new \GuzzleHttp\Psr7\Request('GET', '/api/sessions'));
+ $failedAfterTimeout = \Closure::bind(
+ fn (int|float $startedNs): bool => $this->failedAfterTimeout($failure, $startedNs),
+ $executor,
+ \OpenWA\Http\HttpExecutor::class
+ );
+
+ $now = hrtime(true);
+ foreach (['int' => $now, 'float' => (float) $now] as $type => $started) {
+ $this->assertFalse($failedAfterTimeout($started), "{$type} start, no time elapsed");
+ $this->assertTrue($failedAfterTimeout($started - 2_000_000_000), "{$type} start, 2 s elapsed");
+ }
+ }
+
private static function clientWhoseHandlerFails(\Throwable $failure, float $timeout, float $afterSeconds): \OpenWA\Client
{
$handler = static function () use ($failure, $afterSeconds) {