Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React (phpnw)

React (phpnw)

Igor Wiedler

October 06, 2012
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. var http = require('http'); var server = new http.Server(); server.on('request',

    function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(1337, '127.0.0.1'); Saturday, October 6, 12
  2. $server = stream_socket_server('tcp://127.0.0.1:1337'); while ($conn = stream_socket_accept($server, -1)) { fwrite($conn,

    "HTTP/1.1 200 OK\r\n"); fwrite($conn, "Content-Length: 3\r\n\r\n"); fwrite($conn, "Hi\n"); fclose($conn); } Saturday, October 6, 12
  3. Latency Comparison Numbers -------------------------- L1 cache reference 0.5 ns Branch

    mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Compress 1K bytes with Zippy 3,000 ns Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms Read 4K randomly from SSD* 150,000 ns 0.15 ms Read 1 MB sequentially from memory 250,000 ns 0.25 ms Round trip within same datacenter 500,000 ns 0.5 ms Read 1 MB sequentially from SSD* 1,000,000 ns 1 ms Disk seek 10,000,000 ns 10 ms Read 1 MB sequentially from disk 20,000,000 ns 20 ms Send packet CA->Netherlands->CA 150,000,000 ns 150 ms Saturday, October 6, 12
  4. Latency Comparison Numbers -------------------------- L1 cache reference 0.5 ns Branch

    mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Compress 1K bytes with Zippy 3,000 ns Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms Read 4K randomly from SSD* 150,000 ns 0.15 ms Read 1 MB sequentially from memory 250,000 ns 0.25 ms Round trip within same datacenter 500,000 ns 0.5 ms Read 1 MB sequentially from SSD* 1,000,000 ns 1 ms Disk seek 10,000,000 ns 10 ms Read 1 MB sequentially from disk 20,000,000 ns 20 ms Send packet CA->Netherlands->CA 150,000,000 ns 150 ms Saturday, October 6, 12
  5. $readable = $read ?: null; $writable = $write ?: null;

    $except = null; if (stream_select($readable, $writable, $except, 1)) { if ($readable) { foreach ($readable as $stream) { ... } } if ($writable) { foreach ($writable as $stream) { ... } } } Saturday, October 6, 12
  6. $readable = $read ?: null; $writable = $write ?: null;

    $except = null; if (stream_select($readable, $writable, $except, 1)) { if ($readable) { foreach ($readable as $stream) { ... } } if ($writable) { foreach ($writable as $stream) { ... } } } Event Loop Saturday, October 6, 12
  7. $server = stream_socket_server('tcp://127.0.0.1:1337'); $read = [$server]; $write = []; ...

    // deep in the event loop foreach ($readable as $stream) { if ($server === $stream) { $conn = stream_socket_accept($server, 0); $read[] = $conn; } else { $data = fread($stream, 1024); echo $data; } } Saturday, October 6, 12
  8. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new

    React\Http\Server($socket, $loop); $http->on('request', function ($req, $rep) { $rep->writeHead(); $rep->end("Hello World!\n"); }); $socket->listen(8080); $loop->run(); Saturday, October 6, 12
  9. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $socket->on('connection', function ($conn)

    { $conn->pipe($conn); }); $socket->listen(4000); $loop->run(); Saturday, October 6, 12
  10. $emitter = new Evenement\EventEmitter(); $emitter->on('data', function ($data) { echo $data;

    }); $emitter->emit('data', [$data]); Saturday, October 6, 12
  11. EventLoop $loop->addPeriodicTimer(5, function () { $memory = memory_get_usage() / 1024;

    $formatted = number_format($memory, 3).'K'; echo "Current memory usage: {$formatted}\n"; }); Saturday, October 6, 12
  12. Stream $source = new React\Stream\Stream(fopen('omg.txt', 'r'), $loop); $dest = new

    React\Stream\Stream(fopen('wtf.txt', 'w'), $loop); $source->pipe($dest); Saturday, October 6, 12
  13. Socket $socket = new React\Socket\Server($loop); $socket->on('connection', function ($conn) { $conn->write("Hello

    there!\n"); $conn->write("Welcome to this amazing server!\n"); $conn->write("Here's a tip: don't say anything.\n"); $conn->on('data', function ($data) use ($conn) { $conn->close(); }); }); $socket->listen(1337); Saturday, October 6, 12
  14. Http $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket); $http->on('request',

    function ($request, $response) { $response->writeHead(200, ['Content-Type' => 'text/plain']); $response->end("Hello World!\n"); }); $socket->listen(1337); Saturday, October 6, 12
  15. Http $http = new React\Http\Server($socket); $http->on('request', function ($request, $response) {

    $response->writeHead(200, ['Content-Type' => 'text/plain']); $request->pipe($response); }); Saturday, October 6, 12
  16. $waiting = null; $socket->on('connection', function ($conn) use (&$waiting) { if

    (null === $waiting || !$waiting->isReadable()) { $waiting = $conn; $conn->write("Please wait until a partner connects.\n"); return; } $message = "You are now talking to %s.\n"; $conn->write(sprintf($message, getConnectionId($waiting))); $waiting->write(sprintf($message, getConnectionId($conn))); $conn->pipe($waiting)->pipe($conn); $waiting = null; }); Saturday, October 6, 12
  17. $foo->get(function ($bar) { $bar->get(function ($baz) { $baz->get(function ($bazinga) { $bazinga->get(function

    () { throw new FuckItException(); }); }); }); }); Saturday, October 6, 12
  18. use React\Async\Util as Async; Async::waterfall([ function ($callback) use ($foo) {

    $foo->get($callback); }, function ($bar, $callback) { $bar->get($callback); }, function ($baz, $callback) { $baz->get($callback); }, function ($bazinga, $callback) { $bazinga->get($callback); }, function ($callback) { throw new FuckItException(); } ]); Saturday, October 6, 12
  19. use React\Curry\Util as Curry; $add = function ($a, $b) {

    return $a + $b; }; $addOne = Curry::bind($add, 1); echo sprintf("%s\n", $addOne(5)); // outputs 6 Saturday, October 6, 12
  20. public function query($domain, $callback) { Async::waterfall( [ Curry::bind([$this, 'resolveWhoisServer'], $domain),

    Curry::bind([$this, 'queryWhoisServer'], $domain), ], $callback ); } public function resolveWhoisServer($domain, $callback) public function queryWhoisServer($domain, $ip, $callback) Saturday, October 6, 12
  21. $client->connect(function ($client) { $client->monitor(function ($event) { echo implode(' ', [

    "[{$event->timestamp}]", $event->command, $event->arguments, ]); echo "\n"; }); }); Saturday, October 6, 12