Skip to content

Commit 3adbaf9

Browse files
authored
Merge pull request #129 from clue-labs/edge-triggered
Documentation for edge-triggered event listeners and stream buffers
2 parents eec2037 + 536aef8 commit 3adbaf9

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ To reiterate: Using this event loop on PHP 7 is not recommended.
207207
Accordingly, the [`Factory`](#factory) will not try to use this event loop on
208208
PHP 7.
209209

210+
This event loop is known to trigger a readable listener only if
211+
the stream *becomes* readable (edge-triggered) and may not trigger if the
212+
stream has already been readable from the beginning.
213+
This also implies that a stream may not be recognized as readable when data
214+
is still left in PHP's internal stream buffers.
215+
As such, it's recommended to use `stream_set_read_buffer($stream, 0);`
216+
to disable PHP's internal read buffer in this case.
217+
See also [`addReadStream()`](#addreadstream) for more details.
218+
210219
#### ExtLibevLoop
211220

212221
An `ext-libev` based event loop.
@@ -482,6 +491,14 @@ read event listener for this stream.
482491
The execution order of listeners when multiple streams become ready at
483492
the same time is not guaranteed.
484493

494+
Some event loop implementations are known to only trigger the listener if
495+
the stream *becomes* readable (edge-triggered) and may not trigger if the
496+
stream has already been readable from the beginning.
497+
This also implies that a stream may not be recognized as readable when data
498+
is still left in PHP's internal stream buffers.
499+
As such, it's recommended to use `stream_set_read_buffer($stream, 0);`
500+
to disable PHP's internal read buffer in this case.
501+
485502
#### addWriteStream()
486503

487504
> Advanced! Note that this low-level API is considered advanced usage.

src/ExtLibeventLoop.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
* Accordingly, the [`Factory`](#factory) will not try to use this event loop on
2323
* PHP 7.
2424
*
25+
* This event loop is known to trigger a readable listener only if
26+
* the stream *becomes* readable (edge-triggered) and may not trigger if the
27+
* stream has already been readable from the beginning.
28+
* This also implies that a stream may not be recognized as readable when data
29+
* is still left in PHP's internal stream buffers.
30+
* As such, it's recommended to use `stream_set_read_buffer($stream, 0);`
31+
* to disable PHP's internal read buffer in this case.
32+
* See also [`addReadStream()`](#addreadstream) for more details.
33+
*
2534
* @link https://pecl.php.net/package/libevent
2635
*/
2736
class ExtLibeventLoop implements LoopInterface

src/LoopInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public function addReadStream($stream, callable $listener);
9494
* The execution order of listeners when multiple streams become ready at
9595
* the same time is not guaranteed.
9696
*
97+
* Some event loop implementations are known to only trigger the listener if
98+
* the stream *becomes* readable (edge-triggered) and may not trigger if the
99+
* stream has already been readable from the beginning.
100+
* This also implies that a stream may not be recognized as readable when data
101+
* is still left in PHP's internal stream buffers.
102+
* As such, it's recommended to use `stream_set_read_buffer($stream, 0);`
103+
* to disable PHP's internal read buffer in this case.
104+
*
97105
* @param resource $stream The PHP stream resource to check.
98106
* @param callable $listener Invoked when the stream is ready.
99107
* @see self::removeWriteStream()

0 commit comments

Comments
 (0)