ReactPHPとの戯れ

 ReactPHPとの戯れ

PHPカンファレンス福岡2018 前夜祭リジェクトコン #phpconfuk_rej

7b41309d419e0ac011d14774067b0be1?s=128

s-ichikawa

June 15, 2018
Tweet

Transcript

  1. ReactPHPͱͷٔΕ PHPΧϯϑΝϨϯε෱Ԭ2018 લલ໷ࡇϦδΣΫτίϯ @ichikawa_0829

  2. Twitter: @ichikawa_0829 ϝϧΧϦ CRE Backend Engineer PHPྺ 6೥

  3. None
  4. /PU1)1 3FBDUKT 1VSF1)1

  5. ReactPHP?

  6. ReactPHP? • ϊϯϒϩοΩϯάI/OɺΠϕϯτۦಈܕϓϩάϥϛϯάΛՄೳʹ͢ΔҝͷϥΠϒϥϦ • جຊతʹಛผͳ֦ுϞδϡʔϧΛඞཁͱ͠ͳ͍ • Core Components • EventLoop

    ΠϕϯτΛ؂ࢹͯ͠ɺରԠ͢ΔॲཧΛ࣮ߦ͢ΔͨΊͷϧʔϓ • Stream ϊϯϒϩοΩϯάI/OΛ࣮ݱ͢ΔͨΊͷίϯϙʔωϯτ • Promise Promise/A ࣮૷ΛՄೳʹ͢Δίϯϙʔωϯτ • Network Components (Socket, Datagram) • Protocol Components (HTTP, HTTP Client, DNS)
  7. ϊϯϒϩοΩϯάI/Oʁ Πϕϯτۦಈʁʁ

  8. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  9. ಉظ(ϒϩοΩϯά) I/O https://www.ibm.com/developerworks/library/l-async/index.html

  10. ΠΧ৯΂͍ͨ ௼͖ͬͯ·ʔ͢ ·ͩ௼Εͳ͍࣌ظ ௼ΕΔ࣌ظ ௼Εͨʂ ঌ্͕͠Εʂ "QQMJDBUJPO ,FSOFM GE

  11. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨου౳Ͱॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  12. Non Blocking I/O https://www.ibm.com/developerworks/library/l-async/index.html

  13. ΠΧ৯΂͍ͨ ௼͖ͬͯ·ʔ͢ ·ͩ௼Εͳ͍࣌ظ ௼ΕΔ࣌ظ ௼Εͨʂ ঌ্͕͠Εʂ ࠓ௼Εͳ͍ ਐḿͲ͏Ͱ͔͢ ਐḿμϝͰ͢ ௼Εͨʁ

  14. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  15. ඇಉظ(Asynchronous) I/O https://www.ibm.com/developerworks/library/l-async/index.html

  16. ΠΧ৯΂͍͔ͨΒ ௼ΕͨΒڭ͑ͯ ௼͖ͬͯ·ʔ͢ ·ͩ௼Εͳ͍࣌ظ ௼ΕΔ࣌ظ ௼Εͨʂ ঌ্͕͠Εʂ

  17. Πϕϯτۦಈܕϓϩάϥϛϯά • Πϕϯτͷ؂ࢹΛߦ͍ɺൃੜͨ͠ΠϕϯτʹैͬͯॲཧΛ࣮ߦ͢Δ • Πϕϯτʹ͸Ϛ΢ε΍ΩʔϘʔυͷૢ࡞ɾೖྗɺιέοτ͔Βͷϝοηʔ δͷड৴ͳͲ৭ʑ͋Δ • ͦΕΒͷΠϕϯτΛ଴ػ͢Δϧʔϓͷ͜ͱΛΠϕϯτϧʔϓͱݺͼɺΠϕϯ τ͕ൃੜ͔ͨ͠͸ϑΝΠϧσΟεΫϦϓλ΍ιέοτσΟεΫϦϓλΛ؂ ࢹ͢Δ

    • ReactPHP΍Node.js͸I/OͷଟॏԽ(I/O multiplexing)Λߦ͍ෳ਺ͷσΟεΫϦϓλ ͷ؂ࢹΛՄೳʹ͍ͯ͠Δ
  18. I/OଟॏԽ • select(), poll(), epoll()ͳͲͷγεςϜίʔϧΛ࢖ͬͯγϯάϧϓϩηεͰෳ਺ͷ fdͷঢ়ଶมԽΛ؂ࢹ͢Δɻ • γεςϜίʔϧ΍ϥΠϒϥϦ • select()ɿ࠷΋ҰൠతͳγεςϜίʔϧɻpollʹൺ΂Δͱଟ͘ͷOSͰ࣮૷͞

    Ε͍ͯΔͷͰҠ২ੑߴΊɻ • poll()ɿselect()ΑΓଟػೳˍগ͠ੑೳ͕Α͍ɻ • epoll() (Linux2.6~), kqueue(BSD), /dev/poll(Solaris)ɿ্هͷߋʹߴੑೳ൛ • libevɿ؀ڥ͝ͱʹ࠷దͳγεςϜίʔϧબΜͰ͘ΕΔ
  19. Hello World

  20. Hello World

  21. Start Event Loop in ReactPHP

  22. Hello World

  23. ResourceStreamͷΠϕϯτ • ReadableResourceSream
 data: σʔλΛಡΈࠐΜͩ࣌ʹൃੜ
 error: ಡΈࠐΈʹࣦഊͨ࣌͠ʹൃੜ
 end: σʔλΛಡΈࠐΈऴΘͬͨ࣌ʹൃੜ
 close:

    stream͕ดͨ͡ͱ͖ʹൃੜ • WritableResourceStream
 drain: ॻ͖ࠐΈόοϑΝ͕์ग़͞Εͨ࣌ʹൃੜ
 pipe: readableετϦʔϜ͔Βpipe()ͰσʔλΛड৴ͨ͠ͱ͖ʹൃੜ
 error: ॻ͖ࠐΈ͕ࣦഊͨ͠ͱ͖ʹൃੜ
 close: streamΛด͡Δͱ͖ʹൃੜ
  24. None
  25. Hello World Server

  26. ४උˍ֓ཁ • composer require react/http • ϦΫΤετΛૹ৴͢ΔͱHello Worldͱฦ͚ͩ͢ͷαʔόΛ࡞Γ·͢

  27. server.php

  28. None
  29. /PEFKTͱࣅͨײ͡Ͱ)FMMP8PSME4FSWFSΛ࡞੒Ͱ͖·ͨ͠ server.js

  30. Making Simple Chat

  31. ४උˍ֓ཁ • composer require react/socket • React\Socket\ServerΫϥεΛ࢖͍ɺSocket௨৴Λߦ͏ServerΛಈ͔͠ɺ
 Client͔ΒͷConnectionΛPool͠ɺϝοηʔδͷૹड৴Λߦ͍·͢ • ઀ଓ༻ΫϥΠΞϯτ͸React\Socket\ConnectorΫϥεΛ࢖༻

    • https://github.com/s-ichikawa/react-php-chat
  32. ઀ଓ ໊લೖྗͯ͠Ͷ TJDIJLBXBͰ͢ Α͏ͦ͜ʂ TJDIJLBXB͕+PJO͠·ͨ͠

  33. )FMMP8PSME TJDIJLBXB)FMMP8PSME

  34. None
  35. HTTP Request

  36. ४උˍ֓ཁ • composer require react/http-client • ςετ༻αʔόʹ1000ճϦΫΤετΛૹΔ
 $ time curl

    127.0.0.1:8080
 real 0m0.038s
 user 0m0.010s
 sys 0m0.013s • HttpΫϥΠΞϯτͱ͍͑͹͓ೃછΈͷGuzzlePHPͱൺֱͯ͠Έ·͢
 ˞MBP, 2.9GHz Core i7, Memory 16GB
  37. Guzzle UJNFQIQGHV[[MF@TJNQMF@TFOEFSQIQ SFBM NT VTFS NT TZT NT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/guzzle_simple_sender.php

  38. ReactPHP UJNFQIQGSFBDU@CVML@TFOEFSQIQ SFBM NT VTFS NT TZTNT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/react_bulk_sender.php

  39. Guzzle UJNFQIQGHV[[MF@QPPM@TFOEFSQIQ SFBM NT VTFS NT TZT NT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/guzzle_pool_sender.php

  40. • GuzzleHttp\ClientͰ௚ྻ࣮ߦ͢ΔΑΓ͸ReactPHPͷ΄͏͕ૣ͍ • GuzzleHttp\PoolͰϦΫΤετ͢Δ৔߹͸ѹ౗తʹGuzzleͷউར • Guzzle͸ඇಉظϦΫΤετ͢Δࡍɺ಺෦Ͱcurl-multiΛݺΜͰ͍Δɻcurl-multi͸ pthreadΛ࢖༻ͯ͠ฒߦॲཧ͍ͯ͠Δɻ • ReactPHP͸͋͘·ͰγϯάϧϓϩηεɾγϯάϧεϨου •

    ReactPHPͱGuzzleͷAsyncXxx͸10000ճͱ͔࿈ଓͰϦΫΤετ͢Δͱ Too many open files ~ ౳ͷΤϥʔʹͳͬͯ͠·͏৔߹͕͋ͬͨ
  41. ͦͷଞ

  42. • PHP-PM
 https://github.com/php-pm/php-pm • Async RedisΫϥΠΞϯτ
 https://github.com/nrk/predis-async • υϩʔϯ΋ඈ͹ͤΔ
 https://youtu.be/i07m_TFR9no?t=8m16s

  43. None
  44. https://youtu.be/i07m_TFR9no?t=8m16s

  45. None
  46. https://github.com/reactphp/react/wiki/Users

  47. Learning Event-Driven PHP With ReactPHP w ͓ͦΒ͘།Ұͷ3FBDU1)1ͷॻ ੶ w -FBO1VC·ͨ͸,JOEMFͰൃചத


    IUUQTMFBOQVCDPNFWFOU ESJWFOQIQ
 IUUQTXXXBNB[PODPKQ EQ#:49'
  48. ·ͱΊ • ϊϯϒϩοΩϯάI/O͕׆͖Δ৔໘Ͱ࢖ͬͯΈΔͷ͸ྑͦ͞͏ • PHPͷΈͰಈͨ͘ΊI/OपΓͷॲཧΛվળ͍͚ͨ͠ͲɺGo΍Node.jsʹ੾Γସ͑ ΒΕͳ͍Α͏ͳ৔߹ʹ͸ࢼͯ͠ΈΔՁ஋͸͋Γͦ͏ • Πϕϯτϧʔϓ΍I/OͷछྨɾଟॏԽͷ࢓૊ΈɺΠϕϯτۦಈͳͲීஈ͋· Γҙࣝͯ͠ͳ͔ͬͨ෦෼ΛษڧͰ͖ͨͷ͸ྑ͔ͬͨ •

    ڵຯ͕༙͍ͨํ͸ͥͻAfter Hack౳Ͱ΋࿔ͬͯΈ͍ͯͩ͘͞
 PHP Conference Fukuoka After Hack!!
 https://fusic.connpass.com/event/83312/