Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ReactPHPとの戯れ
Search
s-ichikawa
June 15, 2018
Technology
0
490
ReactPHPとの戯れ
PHPカンファレンス福岡2018 前夜祭リジェクトコン #phpconfuk_rej
s-ichikawa
June 15, 2018
Tweet
Share
More Decks by s-ichikawa
See All by s-ichikawa
15分で分かった気になるGraphQL
ichikawa
3
3.1k
GraphQL入門
ichikawa
3
1.1k
商品監視を支える技術.key.pdf
ichikawa
0
86
SendGridで人生変わった
ichikawa
2
2.2k
Laravel with SendGrid
ichikawa
0
2.8k
Other Decks in Technology
See All in Technology
バクラクにおける可観測性向上の取り組み
yuu26
3
400
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
失敗しないOpenJDKの非互換調査
tabatad
0
270
신뢰할 수 있는 AI 검색 엔진을 만들기 위한 Liner의 여정
huffon
0
260
Gradle: The Build System That Loves To Hate You
aurimas
2
140
君は隠しイベントを見つけれるか?
mujyun
0
270
【若手エンジニア応援LT会】AWSで繋がり、共に成長! ~コミュニティ活動と新人教育への挑戦~
kazushi_ohata
0
170
顧客が本当に必要だったもの - パフォーマンス改善編 / Make what is needed
soudai
24
6.7k
なんで、私がAWS Heroに!? 〜社外の広い世界に一歩踏み出そう〜
minorun365
PRO
6
1.1k
プロダクトチームへのSystem Risk Records導入・運用事例の紹介/Introduction and Case Studies on Implementing and Operating System Risk Records for Product Teams
taddy_919
1
160
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
9
120k
AWS CDKでデータリストアの運用、どのように設計する?~Aurora・EFSの実践事例を紹介~/aws-cdk-data-restore-aurora-efs
mhrtech
4
630
Featured
See All Featured
Statistics for Hackers
jakevdp
796
220k
Music & Morning Musume
bryan
46
6.1k
Designing for Performance
lara
604
68k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Designing Experiences People Love
moore
138
23k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Practical Orchestrator
shlominoach
186
10k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
Speed Design
sergeychernyshev
24
570
Measuring & Analyzing Core Web Vitals
bluesmoon
1
39
Transcript
ReactPHPͱͷٔΕ PHPΧϯϑΝϨϯεԬ2018 લલࡇϦδΣΫτίϯ @ichikawa_0829
Twitter: @ichikawa_0829 ϝϧΧϦ CRE Backend Engineer PHPྺ 6
None
/PU1)1 3FBDUKT 1VSF1)1
ReactPHP?
ReactPHP? • ϊϯϒϩοΩϯάI/OɺΠϕϯτۦಈܕϓϩάϥϛϯάΛՄೳʹ͢ΔҝͷϥΠϒϥϦ • جຊతʹಛผͳ֦ுϞδϡʔϧΛඞཁͱ͠ͳ͍ • Core Components • EventLoop
ΠϕϯτΛࢹͯ͠ɺରԠ͢ΔॲཧΛ࣮ߦ͢ΔͨΊͷϧʔϓ • Stream ϊϯϒϩοΩϯάI/OΛ࣮ݱ͢ΔͨΊͷίϯϙʔωϯτ • Promise Promise/A ࣮ΛՄೳʹ͢Δίϯϙʔωϯτ • Network Components (Socket, Datagram) • Protocol Components (HTTP, HTTP Client, DNS)
ϊϯϒϩοΩϯάI/Oʁ Πϕϯτۦಈʁʁ
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
ಉظ(ϒϩοΩϯά) I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍ͨ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ "QQMJDBUJPO ,FSOFM GE
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
Non Blocking I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍ͨ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ ࠓΕͳ͍ ਐḿͲ͏Ͱ͔͢ ਐḿμϝͰ͢ Εͨʁ
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
ඇಉظ(Asynchronous) I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍͔ͨΒ ΕͨΒڭ͑ͯ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ
Πϕϯτۦಈܕϓϩάϥϛϯά • ΠϕϯτͷࢹΛߦ͍ɺൃੜͨ͠ΠϕϯτʹैͬͯॲཧΛ࣮ߦ͢Δ • ΠϕϯτʹϚεΩʔϘʔυͷૢ࡞ɾೖྗɺιέοτ͔Βͷϝοηʔ δͷड৴ͳͲ৭ʑ͋Δ • ͦΕΒͷΠϕϯτΛػ͢Δϧʔϓͷ͜ͱΛΠϕϯτϧʔϓͱݺͼɺΠϕϯ τ͕ൃੜ͔ͨ͠ϑΝΠϧσΟεΫϦϓλιέοτσΟεΫϦϓλΛ ࢹ͢Δ
• ReactPHPNode.jsI/OͷଟॏԽ(I/O multiplexing)Λߦ͍ෳͷσΟεΫϦϓλ ͷࢹΛՄೳʹ͍ͯ͠Δ
I/OଟॏԽ • select(), poll(), epoll()ͳͲͷγεςϜίʔϧΛͬͯγϯάϧϓϩηεͰෳͷ fdͷঢ়ଶมԽΛࢹ͢Δɻ • γεςϜίʔϧϥΠϒϥϦ • select()ɿ࠷ҰൠతͳγεςϜίʔϧɻpollʹൺΔͱଟ͘ͷOSͰ࣮͞
Ε͍ͯΔͷͰҠ২ੑߴΊɻ • poll()ɿselect()ΑΓଟػೳˍগ͠ੑೳ͕Α͍ɻ • epoll() (Linux2.6~), kqueue(BSD), /dev/poll(Solaris)ɿ্هͷߋʹߴੑೳ൛ • libevɿڥ͝ͱʹ࠷దͳγεςϜίʔϧબΜͰ͘ΕΔ
Hello World
Hello World
Start Event Loop in ReactPHP
Hello World
ResourceStreamͷΠϕϯτ • ReadableResourceSream data: σʔλΛಡΈࠐΜͩ࣌ʹൃੜ error: ಡΈࠐΈʹࣦഊͨ࣌͠ʹൃੜ end: σʔλΛಡΈࠐΈऴΘͬͨ࣌ʹൃੜ close:
stream͕ดͨ͡ͱ͖ʹൃੜ • WritableResourceStream drain: ॻ͖ࠐΈόοϑΝ͕์ग़͞Εͨ࣌ʹൃੜ pipe: readableετϦʔϜ͔Βpipe()ͰσʔλΛड৴ͨ͠ͱ͖ʹൃੜ error: ॻ͖ࠐΈ͕ࣦഊͨ͠ͱ͖ʹൃੜ close: streamΛด͡Δͱ͖ʹൃੜ
None
Hello World Server
४උˍ֓ཁ • composer require react/http • ϦΫΤετΛૹ৴͢ΔͱHello Worldͱฦ͚ͩ͢ͷαʔόΛ࡞Γ·͢
server.php
None
/PEFKTͱࣅͨײ͡Ͱ)FMMP8PSME4FSWFSΛ࡞Ͱ͖·ͨ͠ server.js
Making Simple Chat
४උˍ֓ཁ • composer require react/socket • React\Socket\ServerΫϥεΛ͍ɺSocket௨৴Λߦ͏ServerΛಈ͔͠ɺ Client͔ΒͷConnectionΛPool͠ɺϝοηʔδͷૹड৴Λߦ͍·͢ • ଓ༻ΫϥΠΞϯτReact\Socket\ConnectorΫϥεΛ༻
• https://github.com/s-ichikawa/react-php-chat
ଓ ໊લೖྗͯ͠Ͷ TJDIJLBXBͰ͢ Α͏ͦ͜ʂ TJDIJLBXB͕+PJO͠·ͨ͠
)FMMP8PSME TJDIJLBXB)FMMP8PSME
None
HTTP Request
४උˍ֓ཁ • 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
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
ReactPHP UJNFQIQGSFBDU@CVML@TFOEFSQIQ SFBM NT VTFS NT TZTNT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/react_bulk_sender.php
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
• GuzzleHttp\ClientͰྻ࣮ߦ͢ΔΑΓReactPHPͷ΄͏͕ૣ͍ • GuzzleHttp\PoolͰϦΫΤετ͢Δ߹ѹతʹGuzzleͷউར • GuzzleඇಉظϦΫΤετ͢Δࡍɺ෦Ͱcurl-multiΛݺΜͰ͍Δɻcurl-multi pthreadΛ༻ͯ͠ฒߦॲཧ͍ͯ͠Δɻ • ReactPHP͋͘·ͰγϯάϧϓϩηεɾγϯάϧεϨου •
ReactPHPͱGuzzleͷAsyncXxx10000ճͱ͔࿈ଓͰϦΫΤετ͢Δͱ Too many open files ~ ͷΤϥʔʹͳͬͯ͠·͏߹͕͋ͬͨ
ͦͷଞ
• PHP-PM https://github.com/php-pm/php-pm • Async RedisΫϥΠΞϯτ https://github.com/nrk/predis-async • υϩʔϯඈͤΔ https://youtu.be/i07m_TFR9no?t=8m16s
None
https://youtu.be/i07m_TFR9no?t=8m16s
None
https://github.com/reactphp/react/wiki/Users
Learning Event-Driven PHP With ReactPHP w ͓ͦΒ͘།Ұͷ3FBDU1)1ͷॻ ੶ w -FBO1VC·ͨ,JOEMFͰൃചத
IUUQTMFBOQVCDPNFWFOU ESJWFOQIQ IUUQTXXXBNB[PODPKQ EQ#:49'
·ͱΊ • ϊϯϒϩοΩϯάI/O͕׆͖Δ໘ͰͬͯΈΔͷྑͦ͞͏ • PHPͷΈͰಈͨ͘ΊI/OपΓͷॲཧΛվળ͍͚ͨ͠ͲɺGoNode.jsʹΓସ͑ ΒΕͳ͍Α͏ͳ߹ʹࢼͯ͠ΈΔՁ͋Γͦ͏ • ΠϕϯτϧʔϓI/OͷछྨɾଟॏԽͷΈɺΠϕϯτۦಈͳͲීஈ͋· Γҙࣝͯ͠ͳ͔ͬͨ෦ΛษڧͰ͖ͨͷྑ͔ͬͨ •
ڵຯ͕༙͍ͨํͥͻAfter HackͰ࿔ͬͯΈ͍ͯͩ͘͞ PHP Conference Fukuoka After Hack!! https://fusic.connpass.com/event/83312/