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

React (confoo)

React (confoo)

Igor Wiedler

March 01, 2013
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. View Slide

  2. Y U NO PHP?

    View Slide

  3. @igorwesome

    View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. 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');

    View Slide

  8. View Slide

  9. $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);
    }

    View Slide

  10. $s=stream_socket_server('tcp://[::1]:81');
    while($c=stream_socket_accept($s,-1))
    fwrite($c,"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHi");

    View Slide

  11. Non-blocking I/O

    View Slide

  12. C10K

    View Slide

  13. Calculations are fast, I/O is slow.

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

  16. View Slide

  17. stream_select

    View Slide

  18. $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) {
    ...
    }
    }
    }

    View Slide

  19. $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

    View Slide

  20. View Slide

  21. View Slide

  22. Event-driven, non-blocking I/O with PHP.

    View Slide

  23. Demo

    View Slide

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

    View Slide

  25. $loop = React\EventLoop\Factory::create();
    $socket = new React\Socket\Server($loop);
    $socket->on('connection', function ($conn) {
    $conn->pipe($conn);
    });
    $socket->listen(4000);
    $loop->run();

    View Slide

  26. View Slide

  27. # composer.json
    {
    "require": {
    "react/http": "0.2.*"
    }
    }

    View Slide

  28. $ composer install

    View Slide

  29. Philosophy

    View Slide

  30. Architecture

    View Slide

  31. Événement
    EventEmitter for PHP

    View Slide

  32. $emitter = new Evenement\EventEmitter();
    $emitter->on('data', function ($data) {
    echo $data;
    });
    $emitter->emit('data', [$data]);

    View Slide

  33. EventLoop
    Stream
    Socket
    Http

    View Slide

  34. EventLoop
    $loop = React\EventLoop\Factory::create();
    $server = stream_socket_server('tcp://127.0.0.1:8080');
    stream_set_blocking($server, 0);
    $loop->addReadStream($server, function ($server) use ($loop) {
    $conn = stream_socket_accept($server);
    ...
    });

    View Slide

  35. EventLoop
    $loop->addPeriodicTimer(5, function () {
    $memory = memory_get_usage() / 1024;
    $formatted = number_format($memory, 3).'K';
    echo "Current memory usage: {$formatted}\n";
    });

    View Slide

  36. EventLoop
    $loop->run();

    View Slide

  37. EventLoop
    backends
    stream_select
    libevent
    libev
    upcoming
    libuv

    View Slide

  38. ReadableStream
    isReadable()
    pause()
    resume()
    close()
    Events
    data, end, error, close
    Stream

    View Slide

  39. ReadableStream
    isReadable()
    pause()
    resume()
    close()
    Events
    data, end, error, close
    Stream

    View Slide

  40. WritableStream
    isWritable()
    write($data)
    end($data)
    close()
    Events
    drain, error, close, pipe
    Stream

    View Slide

  41. WritableStream
    isWritable()
    write($data)
    end($data)
    close()
    Events
    drain, error, close, pipe
    Stream

    View Slide

  42. Stream
    $source = new React\Stream\Stream(fopen('omg.txt', 'r'), $loop);
    $dest = new React\Stream\Stream(fopen('wtf.txt', 'w'), $loop);
    $source->pipe($dest);

    View Slide

  43. Stream
    $a->pipe($b)->pipe($c);
    a | b | c

    View Slide

  44. 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);

    View Slide

  45. 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);

    View Slide

  46. EventLoop
    Stream
    Socket
    Http

    View Slide

  47. Http
    HttpClient DNS

    View Slide

  48. HttpClient Http DNS Stomp Whois

    View Slide

  49. HttpClient Http DNS Stomp Whois
    WebSocket DNode
    SOCKS IRC

    View Slide

  50. View Slide

  51. $foo->get(function ($bar) {
    $bar->get(function ($baz) {
    $baz->get(function ($bazinga) {
    $bazinga->get(function () {
    throw new FuckItException();
    });
    });
    });
    });

    View Slide

  52. async
    API
    (escaping callback hell)

    View Slide

  53. React/Async

    View Slide

  54. 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();
    }
    ]);

    View Slide

  55. React/Promise

    View Slide

  56. $dns
    ->resolve('igor.io')
    ->then(function ($ip) {
    echo "Host: $ip\n";
    });

    View Slide

  57. $promise
    ->then('doStuff')
    ->then('doMoreStuff')
    ->then('explosion')
    ->then(null, function ($e) {
    echo "[Error] {$e->getMessage()}\n";
    });

    View Slide

  58. Deferred
    Promise Resolver
    Consumer Producer
    then() resolve()
    reject()

    View Slide

  59. Streams

    View Slide

  60. Readable Writable

    View Slide

  61. Readable Writable
    Through

    View Slide

  62. Readable Writable
    Through
    Composite

    View Slide

  63. Buffered Sink

    View Slide

  64. Bi-directional

    View Slide

  65. conn logger parser
    $conn->pipe($inputLogger)->pipe($parser);

    View Slide

  66. parser client
    $parser->on('message', function ($message) use ($client) {
    $client->say('pong');
    });

    View Slide

  67. client conn
    logger
    $client->pipe($outputLogger)->pipe($conn);

    View Slide

  68. conn parser client conn

    View Slide

  69. Houston we have a
    blocking call

    View Slide

  70. Inter-process
    communication

    View Slide

  71. React/ZMQ

    View Slide

  72. $context = new React\ZMQ\Context($loop);
    $push = $context->getSocket(ZMQ::SOCKET_PUSH);
    $push->connect('tcp://127.0.0.1:5555');
    $push->send('hello');

    View Slide

  73. React/Stomp Predis/Async DNode-PHP

    View Slide

  74. RealtimeWeb™

    View Slide

  75. Ratchet

    View Slide

  76. gifsockets!!!11

    View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. Questions?
    • joind.in/7969
    • reactphp.org
    • @reactphp
    • @igorwesome

    View Slide

  81. Questions?
    • joind.in/7969
    • reactphp.org
    • @reactphp
    • @igorwesome

    View Slide

  82. Http Server
    Worker
    Client
    Worker
    Worker
    Client
    Client
    HTTP

    View Slide