Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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+'
Expand All @@ -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 }}

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
<br>
Expand Down
26 changes: 17 additions & 9 deletions src/EventLoop/Driver/IoPollDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/EventLoop/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
16 changes: 4 additions & 12 deletions test/Driver/IoPollDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Io\Poll\Context;
use PHPUnit\Framework\ExpectationFailedException;
use Revolt\EventLoop\Driver;
use Time\Duration;

class IoPollDriverTest extends DriverTest
{
Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
}
Loading