Skip to content

Commit 668f10f

Browse files
minor #50367 [HttpClient] Use hrtime() to compute timeouts (nicolas-grekas)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpClient] Use hrtime() to compute timeouts | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Makes the computation of timeouts resistant to clock drifts. Commits ------- bf6d57db82 [HttpClient] Use hrtime() to compute timeouts
2 parents 9d983af + 9a3a39d commit 668f10f

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

EventSourceHttpClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function request(string $method, string $url, array $options = []): Respo
8585
return;
8686
}
8787
} catch (TransportExceptionInterface) {
88-
$state->lastError = $lastError ?? microtime(true);
88+
$state->lastError = $lastError ?? hrtime(true) / 1E9;
8989

90-
if (null === $state->buffer || ($isTimeout && microtime(true) - $state->lastError < $state->reconnectionTime)) {
90+
if (null === $state->buffer || ($isTimeout && hrtime(true) / 1E9 - $state->lastError < $state->reconnectionTime)) {
9191
yield $chunk;
9292
} else {
9393
$options['headers']['Last-Event-ID'] = $state->lastEventId;
9494
$state->buffer = '';
95-
$state->lastError = microtime(true);
95+
$state->lastError = hrtime(true) / 1E9;
9696
$context->getResponse()->cancel();
9797
$context->replaceRequest($method, $url, $options);
9898
if ($isTimeout) {

Internal/HttplugWaitLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function wait(?ResponseInterface $pendingResponse, float $maxDuration = n
5757
if (0.0 === $remainingDuration = $maxDuration) {
5858
$idleTimeout = 0.0;
5959
} elseif (null !== $maxDuration) {
60-
$startTime = microtime(true);
60+
$startTime = hrtime(true) / 1E9;
6161
$idleTimeout = max(0.0, min($maxDuration / 5, $idleTimeout ?? $maxDuration));
6262
}
6363

@@ -100,7 +100,7 @@ public function wait(?ResponseInterface $pendingResponse, float $maxDuration = n
100100
}
101101

102102
check_duration:
103-
if (null !== $maxDuration && $idleTimeout && $idleTimeout > $remainingDuration = max(0.0, $maxDuration - microtime(true) + $startTime)) {
103+
if (null !== $maxDuration && $idleTimeout && $idleTimeout > $remainingDuration = max(0.0, $maxDuration - hrtime(true) / 1E9 + $startTime)) {
104104
$idleTimeout = $remainingDuration / 5;
105105
break;
106106
}

Response/AmpResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ private static function perform(ClientState $multi, array &$responses = null): v
201201
*/
202202
private static function select(ClientState $multi, float $timeout): int
203203
{
204-
$timeout += microtime(true);
204+
$timeout += hrtime(true) / 1E9;
205205
self::$delay = Loop::defer(static function () use ($timeout) {
206-
if (0 < $timeout -= microtime(true)) {
206+
if (0 < $timeout -= hrtime(true) / 1E9) {
207207
self::$delay = Loop::delay(ceil(1000 * $timeout), Loop::stop(...));
208208
} else {
209209
Loop::stop();

Response/CurlResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
103103
}
104104

105105
$lastExpiry = end($multi->pauseExpiries);
106-
$multi->pauseExpiries[(int) $ch] = $duration += microtime(true);
106+
$multi->pauseExpiries[(int) $ch] = $duration += hrtime(true) / 1E9;
107107
if (false !== $lastExpiry && $lastExpiry > $duration) {
108108
asort($multi->pauseExpiries);
109109
}
@@ -326,7 +326,7 @@ private static function perform(ClientState $multi, array &$responses = null): v
326326
private static function select(ClientState $multi, float $timeout): int
327327
{
328328
if ($multi->pauseExpiries) {
329-
$now = microtime(true);
329+
$now = hrtime(true) / 1E9;
330330

331331
foreach ($multi->pauseExpiries as $id => $pauseExpiry) {
332332
if ($now < $pauseExpiry) {
@@ -344,7 +344,7 @@ private static function select(ClientState $multi, float $timeout): int
344344
return $selected;
345345
}
346346

347-
if ($multi->pauseExpiries && 0 < $timeout -= microtime(true) - $now) {
347+
if ($multi->pauseExpiries && 0 < $timeout -= hrtime(true) / 1E9 - $now) {
348348
usleep((int) (1E6 * $timeout));
349349
}
350350

Response/NativeResponse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
7575

7676
$pauseExpiry = &$this->pauseExpiry;
7777
$info['pause_handler'] = static function (float $duration) use (&$pauseExpiry) {
78-
$pauseExpiry = 0 < $duration ? microtime(true) + $duration : 0;
78+
$pauseExpiry = 0 < $duration ? hrtime(true) / 1E9 + $duration : 0;
7979
};
8080

8181
$this->canary = new Canary(static function () use ($multi, $id) {
@@ -232,7 +232,7 @@ private static function perform(ClientState $multi, array &$responses = null): v
232232
{
233233
foreach ($multi->openHandles as $i => [$pauseExpiry, $h, $buffer, $onProgress]) {
234234
if ($pauseExpiry) {
235-
if (microtime(true) < $pauseExpiry) {
235+
if (hrtime(true) / 1E9 < $pauseExpiry) {
236236
continue;
237237
}
238238

@@ -321,7 +321,7 @@ private static function perform(ClientState $multi, array &$responses = null): v
321321
continue;
322322
}
323323

324-
if ($response->pauseExpiry && microtime(true) < $response->pauseExpiry) {
324+
if ($response->pauseExpiry && hrtime(true) / 1E9 < $response->pauseExpiry) {
325325
// Create empty open handles to tell we still have pending requests
326326
$multi->openHandles[$i] = [\INF, null, null, null];
327327
} elseif ($maxHosts && $maxHosts > ($multi->hosts[parse_url($response->url, \PHP_URL_HOST)] ?? 0)) {
@@ -351,7 +351,7 @@ private static function select(ClientState $multi, float $timeout): int
351351
continue;
352352
}
353353

354-
if ($pauseExpiry && ($now ??= microtime(true)) < $pauseExpiry) {
354+
if ($pauseExpiry && ($now ??= hrtime(true) / 1E9) < $pauseExpiry) {
355355
$timeout = min($timeout, $pauseExpiry - $now);
356356
continue;
357357
}

Response/TransportResponseTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function stream(iterable $responses, float $timeout = null): \Gene
147147
self::schedule($response, $runningResponses);
148148
}
149149

150-
$lastActivity = microtime(true);
150+
$lastActivity = hrtime(true) / 1E9;
151151
$elapsedTimeout = 0;
152152

153153
if ($fromLastTimeout = 0.0 === $timeout && '-0' === (string) $timeout) {
@@ -172,7 +172,7 @@ public static function stream(iterable $responses, float $timeout = null): \Gene
172172
$chunk = false;
173173

174174
if ($fromLastTimeout && null !== $multi->lastTimeout) {
175-
$elapsedTimeout = microtime(true) - $multi->lastTimeout;
175+
$elapsedTimeout = hrtime(true) / 1E9 - $multi->lastTimeout;
176176
}
177177

178178
if (isset($multi->handlesActivity[$j])) {
@@ -291,15 +291,15 @@ public static function stream(iterable $responses, float $timeout = null): \Gene
291291
}
292292

293293
if ($hasActivity) {
294-
$lastActivity = microtime(true);
294+
$lastActivity = hrtime(true) / 1E9;
295295
continue;
296296
}
297297

298298
if (-1 === self::select($multi, min($timeoutMin, $timeoutMax - $elapsedTimeout))) {
299299
usleep(min(500, 1E6 * $timeoutMin));
300300
}
301301

302-
$elapsedTimeout = microtime(true) - $lastActivity;
302+
$elapsedTimeout = hrtime(true) / 1E9 - $lastActivity;
303303
}
304304
}
305305
}

0 commit comments

Comments
 (0)