1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Randomize loop now offset

This commit is contained in:
Aaron Piotrowski 2018-11-26 10:33:03 -06:00 committed by Niklas Keller
parent d82c34d30b
commit 5889f4e0fd
3 changed files with 13 additions and 3 deletions

View File

@ -31,7 +31,7 @@ class EvDriver extends Driver
private $signals = [];
/** @var int Internal timestamp for now. */
private $now = 0;
private $now;
/** @var bool */
private $nowUpdateNeeded = false;
@ -43,6 +43,8 @@ class EvDriver extends Driver
{
$this->handle = new \EvLoop;
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC);
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
if (self::$activeSignals === null) {
self::$activeSignals = &$this->signals;

View File

@ -34,7 +34,7 @@ class EventDriver extends Driver
private $nowUpdateNeeded = false;
/** @var int Internal timestamp for now. */
private $now = 0;
private $now;
/** @var int Loop time offset from microtime() */
private $nowOffset;
@ -43,6 +43,8 @@ class EventDriver extends Driver
{
$this->handle = new \EventBase;
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC);
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
if (self::$activeSignals === null) {
self::$activeSignals = &$this->signals;

View File

@ -34,7 +34,7 @@ class NativeDriver extends Driver
private $nowUpdateNeeded = false;
/** @var int Internal timestamp for now. */
private $now = 0;
private $now;
/** @var int Loop time offset from microtime() */
private $nowOffset;
@ -47,6 +47,8 @@ class NativeDriver extends Driver
$this->timerQueue = new \SplPriorityQueue();
$this->signalHandling = \extension_loaded("pcntl");
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC);
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
}
/**
@ -126,6 +128,10 @@ class NativeDriver extends Driver
// Execute the timer.
$result = ($watcher->callback)($id, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}