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

React.PHP

 React.PHP

Karel Čížek

June 29, 2013
Tweet

More Decks by Karel Čížek

Other Decks in Programming

Transcript

  1. 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 = 3 µs Send 2K bytes over 1 Gbps network ....... 20,000 ns = 20 µs SSD random read ........................ 150,000 ns = 150 µs Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs 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
  2. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new

    React\Http\Server($socket, $loop); $http->on('request', function ($request, $response) { $response->writeHead(200, ['Content-Type' => 'text/plain']); $response->end("Hello World\n"); }); $socket->listen(1337); $loop->run();
  3. callbax: call(function ($a) { call(function ($b) use ($a) { call(function

    ($c) use ($a, $b) { throw new Exception('Where is your god now?'); }); }); });
  4. callbax: $http->on('connect', function ($req, $resp) { $req->on('data', function ($postData) {

    parse_str($postData, $post); $users->getById($post['id'], function ($err, $user) { renderUser($user, $resp); }); } });
  5. // map (Functor) fetchPost($id)->then(function ($post) { return toJson($post, PRETTY); });

    // flatMap (Monad) fetchPost($id)->then(function ($post) { return fetchPost($post->parentId); }); // robot unicorn attack loadFromCache()->then(null, 'loadFromDatabase')
  6. $deferred = new React\Promise\Deferred(); $promise = $deferred->promise(); $resolver = $deferred->resolver();

    $resolver->resolve('I\'m Boxxy you see') $resolver->reject(new \Exception('U dun goofed'));
  7. $data = []; $callback = function($err, $val) use (&$data) {

    $data[] = $val; // pořadí? if (count($data) == 3) { realCallback($err, $data); } // chyby? chybějí! } fetch('ak', $callback); fetch('ruby', $callback); fetch('peo', $callback);
  8. function timeout($time, $promise, $loop) { $defer = new React\Promise\Deferred; $r

    = $defer->resolver(); $sig = $loop->addTimer($time, function() use($r) { $r->reject('timeout'); }); $promise->then( function ($x) use($r, $sig, $loop) { $loop->cancelTimer($sig); $r->resolve($x); }, function ($e) use($r) { $r->reject($e); } ); return $defer->promise(); }
  9. // fetch all followers $profile = fetchProfile('kaja47'); $ids = [];

    $cursor = '-1'; do { $fs = fetchFollowers($profile->id, $cursor); $ids = array_merge($ids, $fs->ids); $cursor = $fs->next_cursor_str; } while ($cursor != "0") return [$profile, $ids];
  10. // async clusterfuck fetchProfile($userName)->then(function ($profile) { $fetch = function($userId, $cursor)

    use (&$fetch) { return fetchFollowers($userId, $cursor)->then(function ($fs) use ($userId) { $cursor = $fs->next_cursor_str; if ($cursor == "0") return When::resolve($fs->ids); else return $fetch($userId, $cursor)->then(function($ids) use($fs) { return array_merge($fs->ids, ids); }); }); }; $fetch($profile->id, '-1')->then(function($ids) use($ids) { return [$profile, $ids]; }) });
  11. CPS

  12. // async flow(function() { $profile = (yield fetchProfile('kaja47')); $ids =

    []; $cursor = '-1'; do { $fs = (yield fetchFollowers($profile->id, $cursor)); $ids = array_merge($ids, $fs->ids); $cursor = $fs->next_cursor_str; } while ($cursor != "0") yield [$profile, $ids]; });
  13. function flow(Closure $f) { $gen = $f(); $fst = true;

    $recur = function($pureValue) use($gen, &$recur, &$fst) { $x = $fst ? $gen->current() : $gen->send($pureValue); $fst = false; if (!$gen->valid()) return $pureValue; if ($x instanceof Promise) return $x->then($recur); else return $recur($x); }; return $recur(null); }
  14. This is the end Beautiful friend This is the end

    My only friend, the end Of our elaborate plans, the end Of everything that stands, the end No safety or surprise, the end I'll never look into your eyes...again Can you picture what will be So limitless and free Desperately in need...of some...stranger's hand In a...desperate land Lost in a Roman...wilderness of pain And all the children are insane All the children are insane Waiting for the summer rain, yeah