Skip to content

Commit f7a29fb

Browse files
committed
Merge branch 'master' into loop-libev
* master: (87 commits) [Http] Add tests for Request::expectsContinue() [Http] Add a test for Response::writeContinue() [Http] Add support for HTTP/1.1 continue status codes Update CHANGELOG handle write errors in Stream Buffer Make Espresso\Stack work with invokables Replace calls to array_merge with array_values Remove useless chr calls in DNS parser CHANGELOG for v0.2.0 Fix some constraints to not use 0.2.* Update README and sub-composer.json files to point to 0.2.* Bump branch-alias to 0.2 Update composer.lock Update git-subsplit script Add dns component to subtree split script [dns] Add todos and references to README [dns] Add RFCs as documentation [dns] Add todos for more robust resolver [dns] Parser test case with two answers [dns] Parse all answers from response, not just first one ...
2 parents 031defa + c0963ce commit f7a29fb

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

LibEventLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ protected function createLibeventCallback()
5252
}
5353
}
5454
} catch (\Exception $ex) {
55-
// If one of the callbacks throws an exception we must remove the stream
55+
// If one of the callbacks throws an exception we must stop the loop
5656
// otherwise libevent will swallow the exception and go berserk.
57-
$loop->removeStream($stream);
57+
$loop->stop();
5858

5959
throw $ex;
6060
}

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# EventLoop Component
2+
3+
Event loop abstraction layer that libraries can use for evented I/O.
4+
5+
In order for async based libraries to be interoperable, they need to use the
6+
same event loop. This component provides a common `LoopInterface` that any
7+
library can target. This allows them to be used in the same loop, with one
8+
single `run` call that is controlled by the user.
9+
10+
In addition to the interface there are some implementations provided:
11+
12+
* `stream_select`: This is the only implementation which works out of the box
13+
with PHP. It does a simple `select` system call. It's not the most performant
14+
of loops, but still does the job quite well.
15+
16+
* `libevent`: This uses the `libevent` pecl extension. `libevent` itself
17+
supports a number of system-specific backends (epoll, kqueue).
18+
19+
All of the loops support these features:
20+
21+
* File descriptor polling
22+
* One-off timers
23+
* Periodic timers
24+
25+
## Usage
26+
27+
Here is an async HTTP server built with just the event loop.
28+
29+
$loop = React\EventLoop\Factory::create();
30+
31+
$server = stream_socket_server('tcp://127.0.0.1:8080');
32+
stream_set_blocking($server, 0);
33+
$loop->addReadStream($server, function ($server) use ($loop) {
34+
$conn = stream_socket_accept($server);
35+
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
36+
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
37+
$written = fwrite($conn, $data);
38+
if ($written === strlen($data)) {
39+
fclose($conn);
40+
$loop->removeStream($conn);
41+
} else {
42+
$data = substr($data, 0, $written);
43+
}
44+
});
45+
});
46+
47+
$loop->addPeriodicTimer(5, function () {
48+
$memory = memory_get_usage() / 1024;
49+
$formatted = number_format($memory, 3).'K';
50+
echo "Current memory usage: {$formatted}\n";
51+
});
52+
53+
$loop->run();
54+
55+
**Note:** The factory is just for convenience. It tries to pick the best
56+
available implementation. Libraries `SHOULD` allow the user to inject an
57+
instance of the loop. They `MAY` use the factory when the user did not supply
58+
a loop.

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
"keywords": ["event-loop"],
55
"license": "MIT",
66
"require": {
7-
"php": ">=5.3.2"
7+
"php": ">=5.3.3"
88
},
99
"suggest": {
1010
"ext-libevent": ">=0.0.5"
1111
},
1212
"autoload": {
1313
"psr-0": { "React\\EventLoop": "" }
1414
},
15-
"target-dir": "React/EventLoop"
15+
"target-dir": "React/EventLoop",
16+
"extra": {
17+
"branch-alias": {
18+
"dev-master": "0.2"
19+
}
20+
}
1621
}

0 commit comments

Comments
 (0)