From 9021dc84587332c2fac3e4b57ae4635c8f49abf0 Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Wed, 5 Apr 2017 11:38:22 +0300 Subject: [PATCH 1/7] Add StreamSelectLoop time conversion methods. --- src/StreamSelectLoop.php | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 92fc5787..754330a8 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -13,6 +13,8 @@ class StreamSelectLoop implements LoopInterface { const MICROSECONDS_PER_SECOND = 1000000; + const NANOSECONDS_PER_SECOND = 1000000000; + const NANOSECONDS_PER_MICROSECOND = 1000; private $futureTickQueue; private $timers; @@ -219,6 +221,61 @@ private function waitForStreamActivity($timeout) } } + /** + * Returns integer amount of seconds in $time or null. + * + * @param float|null $time – time in seconds + * + * @return int|null + */ + static private function getSeconds($time) + { + /* + * Workaround for PHP int overflow: + * (float)PHP_INT_MAX == PHP_INT_MAX => true + * (int)(float)PHP_INT_MAX == PHP_INT_MAX => false + * (int)(float)PHP_INT_MAX == PHP_INT_MIN => true + */ + if ($time == PHP_INT_MAX) { + return PHP_INT_MAX; + } + return $time === null ? null : intval(floor($time)); + } + + /** + * Returns integer amount of microseconds in $time or null. + * + * @param float|null $time – time in seconds + * + * @return int|null + */ + static private function getMicroseconds($time) + { + if ($time === null) { + return null; + } + $fractional = fmod($time, 1); + $microseconds = round($fractional * self::MICROSECONDS_PER_SECOND); + + return intval($microseconds); + } + + /** + * Returns integer amount of nanoseconds in $time or null. + * The precision is 1 microsecond. + * + * @param float|null $time – time in seconds + * + * @return int|null + */ + static private function getNanoseconds($time) + { + if ($time === null) { + return null; + } + return intval(self::getMicroseconds($time) * self::NANOSECONDS_PER_MICROSECOND); + } + /** * Emulate a stream_select() implementation that does not break when passed * empty stream arrays. From 8279d9bf76f6632efdd38bd49a89f10966379933 Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Thu, 6 Apr 2017 00:06:57 +0300 Subject: [PATCH 2/7] Change StreamSelectLoop to keep timeout in seconds, replace usleep with time_nanosleep. --- src/StreamSelectLoop.php | 75 +++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 754330a8..9f950dc6 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -154,20 +154,9 @@ public function run() // Future-tick queue has pending callbacks ... if (!$this->running || !$this->futureTickQueue->isEmpty()) { $timeout = 0; - // There is a pending timer, only block until it is due ... } elseif ($scheduledAt = $this->timers->getFirst()) { - $timeout = $scheduledAt - $this->timers->getTime(); - if ($timeout < 0) { - $timeout = 0; - } else { - /* - * round() needed to correct float error: - * https://github.com/reactphp/event-loop/issues/48 - */ - $timeout = round($timeout * self::MICROSECONDS_PER_SECOND); - } - + $timeout = max($scheduledAt - $this->timers->getTime(), 0); // The only possible event is stream activity, so wait forever ... } elseif ($this->readStreams || $this->writeStreams) { $timeout = null; @@ -191,6 +180,8 @@ public function stop() /** * Wait/check for stream activity, or until the next timer is due. + * + * @param float $timeout */ private function waitForStreamActivity($timeout) { @@ -228,8 +219,12 @@ private function waitForStreamActivity($timeout) * * @return int|null */ - static private function getSeconds($time) + private static function getSeconds($time) { + if ($time === null) { + return null; + } + /* * Workaround for PHP int overflow: * (float)PHP_INT_MAX == PHP_INT_MAX => true @@ -239,7 +234,8 @@ static private function getSeconds($time) if ($time == PHP_INT_MAX) { return PHP_INT_MAX; } - return $time === null ? null : intval(floor($time)); + + return intval(floor($time)); } /** @@ -249,7 +245,7 @@ static private function getSeconds($time) * * @return int|null */ - static private function getMicroseconds($time) + private static function getMicroseconds($time) { if ($time === null) { return null; @@ -268,11 +264,12 @@ static private function getMicroseconds($time) * * @return int|null */ - static private function getNanoseconds($time) + private static function getNanoseconds($time) { if ($time === null) { return null; } + return intval(self::getMicroseconds($time) * self::NANOSECONDS_PER_MICROSECOND); } @@ -282,22 +279,58 @@ static private function getNanoseconds($time) * * @param array &$read An array of read streams to select upon. * @param array &$write An array of write streams to select upon. - * @param integer|null $timeout Activity timeout in microseconds, or null to wait forever. + * @param float|null $timeout Activity timeout in seconds, or null to wait forever. * * @return integer|false The total number of streams that are ready for read/write. * Can return false if stream_select() is interrupted by a signal. */ protected function streamSelect(array &$read, array &$write, $timeout) { + $seconds = self::getSeconds($timeout); + $microseconds = self::getMicroseconds($timeout); + $nanoseconds = self::getNanoseconds($timeout); + if ($read || $write) { - $except = null; + $except = []; - // suppress warnings that occur, when stream_select is interrupted by a signal - return @stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout); + return $this->doSelectStream($read, $write, $except, $seconds, $microseconds); } - $timeout && usleep($timeout); + $this->sleep($seconds, $nanoseconds); return 0; } + + /** + * Proxy for built-in stream_select method. + * + * @param array $read + * @param array $write + * @param array $except + * @param int|null $seconds + * @param int|null $microseconds + * + * @return int + */ + protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds = null) + { + // suppress warnings that occur, when stream_select is interrupted by a signal + return @stream_select($read, $write, $except, $seconds, $microseconds); + } + + /** + * Sleeps for $seconds and $nanoseconds. + * + * @param int|null $seconds + * @param int|null $nanoseconds + */ + protected function sleep($seconds, $nanoseconds = 0) + { + if ($seconds === null || $nanoseconds === null) { + return; + } + if ($seconds > 0 || $nanoseconds > 0) { + time_nanosleep($seconds, $nanoseconds); + } + } } From 4400ede5ca298ea86c653045effb1a8bc5f6726c Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Wed, 5 Apr 2017 11:42:22 +0300 Subject: [PATCH 3/7] Fix StreamSelectLoopTest::testSmallTimerInterval to expect nanoseconds. --- tests/StreamSelectLoopTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/StreamSelectLoopTest.php b/tests/StreamSelectLoopTest.php index d2e3e078..5888e62e 100644 --- a/tests/StreamSelectLoopTest.php +++ b/tests/StreamSelectLoopTest.php @@ -156,15 +156,15 @@ protected function forkSendSignal($signal) public function testSmallTimerInterval() { /** @var StreamSelectLoop|\PHPUnit_Framework_MockObject_MockObject $loop */ - $loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['streamSelect']); + $loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['sleep']); $loop ->expects($this->at(0)) - ->method('streamSelect') - ->with([], [], 1); + ->method('sleep') + ->with(0, intval(Timer::MIN_INTERVAL * StreamSelectLoop::NANOSECONDS_PER_SECOND)); $loop ->expects($this->at(1)) - ->method('streamSelect') - ->with([], [], 0); + ->method('sleep') + ->with(0, 0); $callsCount = 0; $loop->addPeriodicTimer(Timer::MIN_INTERVAL, function() use (&$loop, &$callsCount) { From 08c709b260faefe374d435c6db64c12bb772d0ab Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Wed, 5 Apr 2017 11:43:54 +0300 Subject: [PATCH 4/7] Add StreamSelectLoop test for PHP_INT_MAX seconds timer interval. --- tests/StreamSelectLoopTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/StreamSelectLoopTest.php b/tests/StreamSelectLoopTest.php index 5888e62e..387bcd94 100644 --- a/tests/StreamSelectLoopTest.php +++ b/tests/StreamSelectLoopTest.php @@ -176,4 +176,23 @@ public function testSmallTimerInterval() $loop->run(); } + + /** + * https://github.com/reactphp/event-loop/issues/19 + * + * Tests that timer with PHP_INT_MAX seconds interval will work. + */ + public function testIntOverflowTimerInterval() + { + /** @var StreamSelectLoop|\PHPUnit_Framework_MockObject_MockObject $loop */ + $loop = $this->getMock('React\EventLoop\StreamSelectLoop', ['sleep']); + $loop->expects($this->once()) + ->method('sleep') + ->with(PHP_INT_MAX, 0) + ->willReturnCallback(function() use (&$loop) { + $loop->stop(); + }); + $loop->addTimer(PHP_INT_MAX, function(){}); + $loop->run(); + } } From 4347fa3d0e21bbcd9eace7b8a06b627d12e87104 Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Mon, 10 Apr 2017 21:59:01 +0300 Subject: [PATCH 5/7] Change StreamSelectLoop::streamSelect null-timeout handling. --- src/StreamSelectLoop.php | 50 +++++++++++++++------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 9f950dc6..36fe3b8e 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -213,18 +213,14 @@ private function waitForStreamActivity($timeout) } /** - * Returns integer amount of seconds in $time or null. + * Returns integer amount of seconds in $time. * - * @param float|null $time – time in seconds + * @param float $time – time in seconds * - * @return int|null + * @return int */ private static function getSeconds($time) { - if ($time === null) { - return null; - } - /* * Workaround for PHP int overflow: * (float)PHP_INT_MAX == PHP_INT_MAX => true @@ -239,17 +235,14 @@ private static function getSeconds($time) } /** - * Returns integer amount of microseconds in $time or null. + * Returns integer amount of microseconds in $time. * - * @param float|null $time – time in seconds + * @param float $time – time in seconds * - * @return int|null + * @return int */ private static function getMicroseconds($time) { - if ($time === null) { - return null; - } $fractional = fmod($time, 1); $microseconds = round($fractional * self::MICROSECONDS_PER_SECOND); @@ -257,19 +250,15 @@ private static function getMicroseconds($time) } /** - * Returns integer amount of nanoseconds in $time or null. + * Returns integer amount of nanoseconds in $time. * The precision is 1 microsecond. * - * @param float|null $time – time in seconds + * @param float $time – time in seconds * - * @return int|null + * @return int */ private static function getNanoseconds($time) { - if ($time === null) { - return null; - } - return intval(self::getMicroseconds($time) * self::NANOSECONDS_PER_MICROSECOND); } @@ -286,9 +275,9 @@ private static function getNanoseconds($time) */ protected function streamSelect(array &$read, array &$write, $timeout) { - $seconds = self::getSeconds($timeout); - $microseconds = self::getMicroseconds($timeout); - $nanoseconds = self::getNanoseconds($timeout); + $seconds = $timeout === null ? null : self::getSeconds($timeout); + $microseconds = $timeout === null ? 0 : self::getMicroseconds($timeout); + $nanoseconds = $timeout === null ? 0 : self::getNanoseconds($timeout); if ($read || $write) { $except = []; @@ -296,7 +285,9 @@ protected function streamSelect(array &$read, array &$write, $timeout) return $this->doSelectStream($read, $write, $except, $seconds, $microseconds); } - $this->sleep($seconds, $nanoseconds); + if ($timeout !== null) { + $this->sleep($seconds, $nanoseconds); + } return 0; } @@ -308,11 +299,11 @@ protected function streamSelect(array &$read, array &$write, $timeout) * @param array $write * @param array $except * @param int|null $seconds - * @param int|null $microseconds + * @param int $microseconds * * @return int */ - protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds = null) + protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds) { // suppress warnings that occur, when stream_select is interrupted by a signal return @stream_select($read, $write, $except, $seconds, $microseconds); @@ -321,14 +312,11 @@ protected function doSelectStream(array &$read, array &$write, array &$except, $ /** * Sleeps for $seconds and $nanoseconds. * - * @param int|null $seconds - * @param int|null $nanoseconds + * @param int $seconds + * @param int $nanoseconds */ protected function sleep($seconds, $nanoseconds = 0) { - if ($seconds === null || $nanoseconds === null) { - return; - } if ($seconds > 0 || $nanoseconds > 0) { time_nanosleep($seconds, $nanoseconds); } From 3c8a5d6e0a5bb87d14b75e4efb69dbbb41e5daab Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Tue, 11 Apr 2017 12:29:04 +0300 Subject: [PATCH 6/7] Add explanation about StreamSelectLoop::getSeconds int overflow workaround. --- src/StreamSelectLoop.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 36fe3b8e..06845dc6 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -222,10 +222,15 @@ private function waitForStreamActivity($timeout) private static function getSeconds($time) { /* - * Workaround for PHP int overflow: - * (float)PHP_INT_MAX == PHP_INT_MAX => true - * (int)(float)PHP_INT_MAX == PHP_INT_MAX => false - * (int)(float)PHP_INT_MAX == PHP_INT_MIN => true + * intval(floor($time)) will produce int overflow + * if $time is (float)PHP_INT_MAX: + * (float)PHP_INT_MAX == PHP_INT_MAX //true + * (int)(float)PHP_INT_MAX == PHP_INT_MAX //false + * (int)(float)PHP_INT_MAX == PHP_INT_MIN //true + * ----------------------------------------------- + * Loose comparision is intentional, cause $time + * is float and + * (float)PHP_INT_MAX !== PHP_INT_MAX */ if ($time == PHP_INT_MAX) { return PHP_INT_MAX; From bb81383494d0c829ecc8c0d33d2f642e67ee7e21 Mon Sep 17 00:00:00 2001 From: Ivan Kalita Date: Tue, 11 Apr 2017 12:42:51 +0300 Subject: [PATCH 7/7] Move StreamSelectLoop::streamSelect micro- and nanoseconds calculation into the "if"s. --- src/StreamSelectLoop.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 06845dc6..b2aba72b 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -281,16 +281,16 @@ private static function getNanoseconds($time) protected function streamSelect(array &$read, array &$write, $timeout) { $seconds = $timeout === null ? null : self::getSeconds($timeout); - $microseconds = $timeout === null ? 0 : self::getMicroseconds($timeout); - $nanoseconds = $timeout === null ? 0 : self::getNanoseconds($timeout); if ($read || $write) { $except = []; + $microseconds = $timeout === null ? 0 : self::getMicroseconds($timeout); return $this->doSelectStream($read, $write, $except, $seconds, $microseconds); } if ($timeout !== null) { + $nanoseconds = self::getNanoseconds($timeout); $this->sleep($seconds, $nanoseconds); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy