diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d3a000..014b44e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,7 +44,7 @@ jobs: io-poll-polyfill: 'yes' psalm: 'skip' - # kqueue and WSAPoll back IoPollDriver there, instead of epoll + # kqueue backs IoPollDriver on macOS instead of epoll; on Windows, the IoPollDriver tests are skipped (WSAPoll) - operating-system: 'macos-latest' php-version: 8.6 composer-flags: '--ignore-platform-req=php+' @@ -59,6 +59,17 @@ jobs: extensions: ':xdebug' psalm: 'skip' + # IoPollDriver is not supported on Windows with the polyfill yet, as it never reports failed + # non-blocking connects there (Winsock only signals them in select()'s except set). Re-enable + # once the polyfill passes the except set to stream_select(). + # - operating-system: 'windows-latest' + # php-version: 8.4 + # composer-flags: '--ignore-platform-req=php+' + # job-description: 'on Windows with the Io\Poll polyfill' + # io-poll-polyfill: 'yes' + # extensions: ':xdebug' + # psalm: 'skip' + name: PHP ${{ matrix.php-version }} ${{ matrix.job-description }} runs-on: ${{ matrix.operating-system }} diff --git a/README.md b/README.md index 23489e0..b5a0fe5 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ composer require revolt/event-loop ``` Applications with many concurrent file descriptors require one of the [extensions](https://revolt.run/extensions). +On PHP 8.6 and later, the built-in `Io\Poll` API (epoll, kqueue, …) is used automatically if no extension is available, except on Windows. +On older versions (again except on Windows), `IoPollDriver` also works with [`symfony/polyfill-io-poll`](https://github.com/symfony/polyfill-io-poll), but is not selected automatically, as the polyfill is backed by `stream_select()`. +Set `REVOLT_DRIVER=Revolt\EventLoop\Driver\IoPollDriver` to use it anyway. →  [View documentation](https://revolt.run/)
diff --git a/src/EventLoop/Driver/IoPollDriver.php b/src/EventLoop/Driver/IoPollDriver.php index 6317471..9f209d5 100644 --- a/src/EventLoop/Driver/IoPollDriver.php +++ b/src/EventLoop/Driver/IoPollDriver.php @@ -35,20 +35,28 @@ final class IoPollDriver extends AbstractDriver ]; /** - * Whether this driver is worth choosing over StreamSelectDriver. + * Whether this driver can run in the current environment. * - * The Io\Poll API ships with every PHP 8.6 build. The driver also runs on symfony/polyfill-io-poll - * below that, but that polyfill is backed by stream_select() itself, so it is slower than using - * stream_select() directly and is not picked automatically. Set REVOLT_DRIVER to this class to - * use it there anyway. + * The Io\Poll API ships with every PHP 8.6 build. Below that, the driver runs on + * symfony/polyfill-io-poll (with symfony/polyfill-time). That polyfill is backed by stream_select() + * itself, so it is slower than StreamSelectDriver, and DriverFactory only picks this driver + * automatically on PHP 8.6+. Set REVOLT_DRIVER to this class to use it with the polyfill anyway. * - * On Windows, the only backend is WSAPoll, which accepts pipes such as STDOUT, but never reports - * them, so callbacks on them never run. stream_select() handles them, so the driver is not - * supported there for now. + * The driver is not supported on Windows for now: + * - With PHP 8.6+, the only native backend is WSAPoll, which accepts pipes such as STDOUT, but + * never reports them, so callbacks on them never run. stream_select() handles them. + * - The polyfill never reports a failed non-blocking connect(), because Winsock signals that only + * in select()'s except set, which the polyfill does not pass to stream_select(), so writable + * callbacks on such sockets never run. StreamSelectDriver handles this. */ public static function isSupported(): bool { - return \PHP_VERSION_ID >= 80600 && \PHP_OS_FAMILY !== 'Windows'; + if (\PHP_OS_FAMILY === 'Windows') { + return false; + } + + return \PHP_VERSION_ID >= 80600 + || (\class_exists(Context::class) && \class_exists(Duration::class)); } private readonly Context $context; diff --git a/src/EventLoop/DriverFactory.php b/src/EventLoop/DriverFactory.php index efd8bd3..75ff4d0 100644 --- a/src/EventLoop/DriverFactory.php +++ b/src/EventLoop/DriverFactory.php @@ -40,7 +40,9 @@ public function create(): Driver return new EventDriver(); } - if (IoPollDriver::isSupported()) { + // Below PHP 8.6, IoPollDriver runs on symfony/polyfill-io-poll, which is backed by + // stream_select() and therefore slower than StreamSelectDriver. + if (\PHP_VERSION_ID >= 80600 && IoPollDriver::isSupported()) { return new IoPollDriver(); } diff --git a/test/Driver/IoPollDriverTest.php b/test/Driver/IoPollDriverTest.php index 2b5c371..4d9799a 100644 --- a/test/Driver/IoPollDriverTest.php +++ b/test/Driver/IoPollDriverTest.php @@ -8,7 +8,6 @@ use Io\Poll\Context; use PHPUnit\Framework\ExpectationFailedException; use Revolt\EventLoop\Driver; -use Time\Duration; class IoPollDriverTest extends DriverTest { @@ -21,14 +20,8 @@ public function getFactory(): callable public function setUp(): void { - // Not isSupported(): that is deliberately false below 8.6, while the driver still has to be - // exercised there through the polyfill. - if (!\class_exists(Context::class) || !\class_exists(Duration::class)) { - self::markTestSkipped("Skip, the Io\\Poll API requires PHP 8.6 or symfony/polyfill-io-poll"); - } - - if (self::isWindows()) { - self::markTestSkipped("Skip on Windows, WSAPoll never reports pipes such as STDOUT, so tests hang"); + if (!IoPollDriver::isSupported()) { + self::markTestSkipped("IoPollDriver is not supported in this environment"); } parent::setUp(); @@ -199,9 +192,8 @@ public function testMoreFileDescriptorsThanFdSetSize(): void self::assertTrue($invoked); } - public function testSupportedOnlyWhenNativeAndNotOnWindows(): void + public function testSupportedIfTestsAreNotSkipped(): void { - // the polyfill runs the driver, but it is backed by stream_select() and slower than using it directly - self::assertSame(\PHP_VERSION_ID >= 80600 && !self::isWindows(), IoPollDriver::isSupported()); + self::assertTrue(IoPollDriver::isSupported()); } }