最近メールフォームかいてないメールフォーマーによる、ちょっとだけ変わった一応PHPのメールフォーム(?)のお話 〜副題 最近swooleがたのしいです〜

271fad8d53cd1f12f2b4b6d38e3d7bd3?s=47 uzulla
June 15, 2018

最近メールフォームかいてないメールフォーマーによる、ちょっとだけ変わった一応PHPのメールフォーム(?)のお話 〜副題 最近swooleがたのしいです〜

at 全然野菜(PHPCon Fukuoka 2018 非公式 前々夜祭)
20min talk
https://pepabo.connpass.com/event/85144/

271fad8d53cd1f12f2b4b6d38e3d7bd3?s=128

uzulla

June 15, 2018
Tweet

Transcript

  1. ࠷ۙϝʔϧϑΥʔϜ͔͍ͯͳ͍ϝʔϧ ϑΥʔϚʔʹΑΔɺͪΐͬͱ͚ͩม ΘͬͨҰԠPHPͷϝʔϧϑΥʔϜ ʢʁʣͷ͓࿩ ෭୊ ࠷ۙswoole͕ͨͷ͍͠Ͱ͢

  2. • uzulla • ౦ژീԦࢠ͔Β͖·ͨ͠ • ࠷ۙؾʹͳΔͷ͸nginx unit

  3. • ୈҰষ ࣄྫͱ͔ϕϯνͱ͔ • ୈೋষ easy-swooleͷ঺հ • େࡾএ ෇࿥

  4. ࣄྫͱ͔ϕϯνͱ͔

  5. ͋Δ೔ͷࣄྫ • ಥવͷMOSUCON

  6. MO-ney ga SUCO-shi sika N-ai

  7. MO-SUCO-N • ʮීஈͱஈҧ͍ͷΞΫηε͕͘Δͧʂ͔͠͠༧ࢉ͸Ͱ·ͤΜʯ • ʮ͑ͬʯ

  8. ྫʢ͋͘·Ͱ΋ྫʣ • ΩϟϯϖʔϯͷϝʔϧϑΥʔϜ • ʮϓϨθϯτCPʂ͝Ԡื͓଴ͪͯ͠·͢ʂʯ • ʮഔମʯͰଧͭͷͰɺҰؾʹདྷΔ • XXXrpsʙXXXXrps •

    ʢ͔͠͠༧ࢉ͸ͳ͍ʣ
  9. • !Ϣʔβʔ͸ؾܰʹϦϩʔυͳ͞Δ • "஗͍ͱπΠʔτ͞ΕͯɺౖΒΕΔ • #ͬ͞͞ͱฦ͚ͩ͢Ͱ͘͢͝มΘΔ • $εύΠΫ͸͍͔ͭऴΘΔͷͰɺҰॠ଱͑ΒΕΕ͹উͪ • %ҰൃͷΩϟϯϖʔϯͳͷͰɺϝϯς͸ͳ͍

    • &(ͱݟ͔͚ͤͯԿճ΋΍Δࣄ͸Α͋͘Δ)
  10. ࣮ࡍPHP͕ʢׂͱʣ޲͔ͳ͍ࣄҊ • !preforkͷΈʢ΄΅ʣಉظॲཧͷPHP • nginx+php-fpmͩͱ͍ͩͿϚγ͕ͩɺApache+PHP͸ࢮ • ࣌ؒଳʹΑͬͯ͸ڱଳҬͳϞόΠϧ૬ख͸ݫ͠Ί • !ΞΫηεຖʹશ෦ΛॳظԽ(ಛʹॏྔڃFW) •

    "ΞΫηε͕ڈΕ͹໭Δ • #จݴमਖ਼ʹ࠶ىಈ͕͍Βͳ͍ʢॏཁʣ
  11. ࣌ؒͷ͔͔ΔॲཧΛͳ͍ͨ͘͠… • ֎෦API΁ͷΞΫηε • DB΁ͷอଘ • …ͳͤ͘ͳ͍

  12. • …ͱ͍͏͜ͱΛͲ͏ʹ͔͍ͨ͠ • ……PHPͰʂʂ - ---- • !ʮͦ΋ͦ΋PHP͡Όͳ͍΄͏͕Α͍ͷͰ͸ʁʯ • ʮ·͋PHPerͳͷͰ…ʯ

    • !ʮ࢓ࣄΛબ΂͹͍͍ͷͰ͸ʁʯ • ʮʯ
  13. ޻෉ͷํ๏͸͍Ζ͍Ζ͋Δ͕ • ฒ΂Δʁ -> ͓͕ۚͳ͍ • ཁ݅Λม͑Δʁ -> ͓٬͞Μ͕NO •

    nginx + php-fpm + ૉPHPʁ -> ී௨͗͢Δʢʁʣ
  14. ͱ͍͏͜ͱͰຊ୊ • ʮ͔͔ͤͬͩ͘ΒԶ͸swooleΛબͿͥʂʯ • ͱ͍͏͜ͱͰɺswooleͰ૊ΜͩΒͲ͏ͳΔͷʁͱ͍͏࿩Λ͠· ͢ɻ

  15. αϯϓϧͱϕϯν

  16. αϯϓϧίʔυ • ϝΞυΛݕূͯ͠DBʹอଘ͢ΔϝʔϧϑΥʔϜʢʁʣ • https:/ /github.com/uzulla/easyswoole-sample • swoole൛ͱɺී௨ʹಈ͘൛ಉࠝ

  17. None
  18. • ʮԠื૯਺ʯ͸Ԡื਺Λcount()ͯ͠දࣔ • ͨͩ͠ɺ̑ඵຖͷूܭ

  19. • ϝΞυ͕໰୊ͳͦ͞͏ͳΒɺԠื׬ྃ • ϝΞυଥ౰ੑݕূͷͨΊʹɺDNSΛҾ͍͍ͯΔ

  20. ϕϯνϚʔΫ − બख঺հ - nginx + swoole - nginx +

    php-fpm
  21. ςετ༻؀ڥ • ͪΐͬͱݹ͍MBP্ͷLinux VM • Parallels • Ubuntu 18

  22. php (ڞ௨ɺ΄΅aptͰ͍Εͨ··) • PHP 7.2.6 • opcache • xdebugͳͲ͸ແ͠ •

    swoole֦ு͸swooleςετ࣌ͷΈ • (cliʹͷΈ֦ுΛϩʔυʣ
  23. nginx (ڞ௨ɺ΄΅aptͰ͍Εͨ··) • worker_processes auto; • worker_connections 768; • gzip

    on; • tcp_nopush on; • tcp_nodelay on; • keepalive_timeout 65;
  24. php-fpmʢൺֱ༻ʣ • FW͸ slim 3 • fpmઃఆ • pm =

    static • pm.max_children = 8 • fastcgi unix socket઀ଓ
  25. swoole • swoole࠷৽൛ʴFWͱͯ͠easyswoole • ϫʔΧʔઃఆ • worker_num 8 • max_resquest

    50000 • fastcgi unix socket઀ଓ
  26. ࣮ଌ

  27. ϕϯν̍ • / (ϑΥʔϜ)Λճ͢ɺrpsΛܭଌ • wrk -c {N} -t 8

    -d 10s -R 100000 http:/ /{host}/ • ಉ࣌઀ଓ਺ΛมԽ͍ͤͯ͘͞(c͕1ͷ࣌ͷΈɺt΋1) • ̎ճճͯ͠ߴ͍ํΛ࠾༻ • Τϥʔ͕ͰͨΒ0ѻ͍
  28. root@ubu:~/wrk2# ./wrk -c 1 -t 1 -d 10s -R 100000

    http://127.0.0.1/ Running 10s test @ http://127.0.0.1/ 1 threads and 1 connections Thread Stats Avg Stdev Max +/- Stdev Latency 5.13s 2.83s 9.95s 58.97% Req/Sec -nan -nan 0.00 0.00% 4022 requests in 10.00s, 2.14MB read Requests/sec: 402.19 Transfer/sec: 218.75KB
  29. N php-fpm swoole 1 402 2530 50 946 5782 100

    931 5429 150 Τϥʔ 5335 200 - 5994 500 - 6612 1000 - Τϥʔ
  30. None
  31. ϕϯν̎ • /post (อଘ)Λճ͢ɺrpsΛܭଌ • ab -c {N} -n 1000

    -p postdata -m POST -T "application/x-www- form-urlencoded" http:/ /{host}/post • ಉ࣌઀ଓ਺ΛมԽ͍ͤͯ͘͞ • ̎ճճͯ͠ߴ͍ํΛ࠾༻ • Τϥʔ͕ͰͨΒ0ѻ͍ • (͕ࠩϠό͗͢ΔͷͰDNS໰͍߹ΘͤΛൈ͍ͯ͋Γ·͢)
  32. N php-fpm swoole 1 110 303 10 412 477 100

    408 514 150 Τϥʔ 537 200 520 400 540
  33. None
  34. • 500Ҏ্͸ɺab͕ΤϥʔΛు͍ͨͷͰ…

  35. ࢀߟه࿥ helloworldϕϯν • wrk -c {N} -t 8 -d 10s

    -R 100000 http:/ /{host}/hello • ୯ʹʮhelloʯ͚ͩΛฦ͢ɺయܕతͳʮϕϯν൪௕ʯίʔυ • ͸͖ͬΓݴͬͯҙຯ͸ͳ͍͆
  36. N php-fpm swoole 1 948 3187 50 1984 5935 100

    1909 7303 150 Τϥʔ 6649 200 7649 500 8915
  37. None
  38. ࣄྫɾϕϯν·ͱΊ • ݟͯͷ௨Γɺଟॏ઀ଓ਺͕૿͑ͨΒ͕ࠩ͋Γ·͢ɻ • Write͕ೖΔͱɺϩδοΫʹඞཁͳ͕͔͔࣌ؒΔͷͰɺ྆ऀͷࠩ ͸ॖ·Δ • php-fpm΋ϓϩηε਺Λ૿΍͢ͱվળ͢Δࣄ΋͋Δ͕ɺ࣮ߦί ετ΋্͕Δ •

    BacklogΛ;΍͢ͱόοϑΝʹͳΔ͕ɺ΍͸Γ͍͔ͭ٧·Δ
  39. • readʹ͍ͭͯ͸αϯϓϧίʔυͷߏ଄తʹswoole͕ζϧ͍ॴ΋ ͋Γ·͢ • swoole᩾ርͰ݁ߏҧ͏͠Ͷɺৄ͘͠͸͓खݩͰࢼͯ͠Έͯ͘ ͍ͩ͞ • ʮؾ߹Λ͍Εͯͭ͘Ε͹ʯɺ΋ͬͱੑೳ͸͕͋Δ͸ͣʢ͔͠͠ ͦ͜·ͰखؒΛ͔͚Δͷ͔ʁʣ •

    ʮͱ͜ΖͰɺ͜ΕϝʔϧϑΥʔϜͳͷ͔ʁʯʮ࣮ࡍʹ͸mailgun ͱ૊Έ߹ΘͤΔΜͰ͚͢Ͳɺ·͋ͦͷ͔ΘΓʹdnsΫΤϦ͍Εͯ ͋Γ·͢…ʯ
  40. ͜ͷࠩ͸… • swooleͷྗʂ • …ͱ·Ͱ͸͍͑ͳ͍͚ͲɺϩδοΫ͕ϔϏʔͰͳ͚Ε͹swoole ͷ΄͏͕ଟগ଎͍ɻ • rps͕ࡾܻ௒͑ΔͳΒɺ༗ҙ • ٯʹɺ1ܻ2ܻͩͱେࠩ͸ͳ͍

  41. • ଟগ଎͍ͱͯ͠ɺ͜Ε͕ʮ΋ͷ͍͢͝େมʯͩͬͨΒҙຯ͕ͳ ͍ • ࢲ͕͔͚ͯ΋ɺօ͞Μ͕͔͚ͳ͚Ε͹ҙຯͳ͍ • झຯͰ͔͔ͭ͑͠ͳ͍ͳΒʢࢲ͕͍͍͚Ͳʣօ͞Μʹ͸ҙຯͳ ͍ʢ͔΋ʣ • ଞͷΠϕϯτۦಈࣜϑϨʔϜϫʔΫͩͱ݁ߏେม

    • ͱ͍͏͜ͱͰɺeasyswooleͷ঺հʹҠΓ·͢
  42. • ʢ͜͜·Ͱ̑෼ʣ

  43. easy-swooleͷ঺հ

  44. easy-swooleͱ͸? • https:/ /www.easyswoole.com/ • swoole Λ͔ͭͬͨϑϨʔϜϫʔΫ • े෼࢓ࣄʹ΋࢖͑Δʢݸਓͷײ૝Ͱ͢ʣ •

    ؆୯γϯϓϧʂ • ݁ߏαΠτཱ͕೿ɺ͔͠͠େମதࠃޠ
  45. None
  46. ᐽՍಛੑ EasySwoole ੋҰ׺جဋSwoole Server 䇖ؿ֥ӈሁଽթ྘֥ٳ҃ൔ PHPᐽՍɼህູAPIࣕੜɼϥຂԮ๤PHPᄎྛଆൔᄝࣉӱߒఏބ໓ࡱ ࡆᄛഈջট֥ྟି෥ാbEasySwoole ߴ౓෧૷ྃ Swoole Server

    ࣕ ґچົӻ Swoole Server ݪ༗ಛੑɼࢧ࣋ಉൈࠁކࡓ๐HTTPɺࣗఆ ၬTCPɺUDPླྀၰđಞषؿᆀၛቋ֥֮࿐༝ӮЧބࣚ৯щཿԛ؟ࣉ ӱđॖၳ҄đۚॖႨ֥ႋႨڛༀb
  47. ʢػց຋༁ͯ͠Έ·͠ΐ͏ʣ

  48. ϑϨʔϜಛੑ EasySwoole͸ɺSwoole ServerΛϕʔεʹͨ͠෼ࢄϝϞϦ಺෼ࢄ PHPϑϨʔϜϫʔΫͰɺϓϩηεͷ֮੧΍ϑΝΠϧಡΈࠐΈʹ൐͏ ύϑΥʔϚϯε্ͷෆརӹΛऔΓআͨ͘Ίʹಛผʹ࡞੒͞Ε·͠ ͨɻ·ͩSwoole Server͕ಉ࣌ࠞ߹ϞχλʔHTTPɺΧελϜTCPɺ UDPϓϩτίϧɺ։ൃऀ͸ϚϧνϓϩηεΛॻ͖ࠐΉͨΊͷ࠷খ ݶͷίετͱ࿑ྗͰֶशͰ͖ΔΑ͏ʹɺඇಉظతʹɺߴՄ༻ੑΞ ϓϦέʔγϣϯΛαϙʔτຊདྷͷಛੑΛҡ࣋͠ͳ͕Βɺ

    EasySwooleͷߴ͞͸SwooleαʔόʔΛΧϓηϧԽαʔϏεɻ
  49. • ʮͳΔ΄ͲΘ͔ΒΜʯʮͱʹ͔ͦ͘͢͝͏ʯ • ൈਮ͢Δͱ… • Swooleͱ͍͏΍ͭΛϕʔεʹ͍ͯ͠Δ • SwooleΛ͏·͘ϥοϓͯ͠࢖͍΍͍ͯ͘͢͠Δ • ࠷খݶͷίετͰֶशɺ࣮૷͕Ͱ͖ΔΑ͏ʹ

  50. • ݸਓతʹ͸ɺSwooleͷίʔϧόοΫ஍ࠈΠϕϯτۦಈνοΫͳ ॻ͖ํΛ͠ͳͯ͘Α͘ͳΔͷ͕࠷ߴ • Web API༻ͳΒ֮͑Δ͜ͱ͸࣮ࡍ͘͢ͳ͍ • ΘΓͱPOPOͳײ͡Ͱɺมͳ͜ͱ͸ڧཁ͞Εͳ͍ɺී௨ʹϞμϯ ͳPHP͕׆͔ͤΔ

  51. swooleͱ͸ʁ • https:/ /www.swoole.com/

  52. ࢖ PHP 䇖ؿದჴॖၛщཿۚྟି֥ၳ҄ѩؿ TCPɺUDPɺUnix SocketɺHTTPɼWebSocket ෰ༀbSwoole ՄҎኅᔟႋႨႿ޺৳ຩa ၍׮๙ྐaఒြೈࡱaᄉ࠹ෘaຩ઎Ⴓ༤a໾৳ຩčIOTʣɺӚ৳ ຩaᇆିࡅं֩ਵთb ࢖༻

    PHP + Swoole ࡞ູຩ઎๙ྐॿࡏđॖၛ ൐ఒြ IT ݚؿ๶ི֥ؒੱննิശđ۷ࡆህᇿႿषؿԷྍӁ௖b
  53. Swooleɿຊ൪؀ڥ༻PHPඇಉظωοτϫʔΫ௨৴Τϯδϯ PHP։ൃऀ͸ɺߴੑೳͳඇಉظͷTCPɺUDPɺUnix SocketɺHTTPɺ ͓ΑͼWebSocketͷಉ࣌αʔϏεΛ࡞੒Ͱ͖·͢ɻε΢Υʔϧ͸ɺ ΠϯλʔωοτɺϞόΠϧ௨৴ɺΤϯλʔϓϥΠζιϑτ΢ΣΞɺ Ϋϥ΢υίϯϐϡʔςΟϯάɺΦϯϥΠϯήʔϜɺΠϯλʔωο τʢIOTʣɺΧʔωοτϫʔΩϯάɺεϚʔτϗʔϜͳͲͷ෼໺Ͱ ޿͘࢖༻Ͱ͖·͢ɻωοτϫʔΫ௨৴ϑϨʔϜϫʔΫͱͯ͠PHP + SwooleΛ࢖༻͢Δ͜ͱͰɺاۀͷITݚڀ։ൃνʔϜͷޮ཰Λେ෯

    ʹߴΊɺֵ৽తͳ੡඼ͷ։ൃʹ΋ͬͱूத͢Δ͜ͱ͕Ͱ͖·͢ɻ
  54. None
  55. • ͓ΑͦPHPΒ͘͠ͳ͍ػೳ͕௥ՃͰ͖ΔC֦ு • ࠷ۙݸਓతʹ޷͖ • ࠓճ͸ʮΠϕϯτۦಈͰߴ଎ͳΞϓϦ͕PHPͰ࣮ݱͰ͖Δʯ͕ ॏཁ • httpdʹͳΕΔʂʢݸਓతʹॏཁʣ •

    ʢੋඇௐ΂ͯΈ͍ͯͩ͘͞ɺӳޠαΠτ΋͋ΔΑʂʣ
  56. • ·ͱΊΔͱɺ • swooleͰಈ͘ϑϨʔϜϫʔΫͷҰ͕ͭeasy swoole • ʢphpͰಈ͘ϑϨʔϜϫʔΫͷslimɺΈ͍ͨͳଊ͑ํͰྑ͍Ͱ ͢ʣ

  57. Ͱ͸࿩Λ΋Ͳͯ͠ɺeasy swooleͷ࿩͠Λ͠·͢

  58. easy-swoole ͷ֓ཁ • ϓϩηεɾϫʔΧʔϚωʔδϟʔ • ؆୯ͳϧʔλʔ • ؆୯ͳδϣϒΩϡʔ • γϯϓϧͳΠϕϯτϑοΫ

    • ͳͲ͕ೖ͍ͬͯΔ
  59. ೖͬͯͳ͍΋ͷ • ςϯϓϨʔτΤϯδϯ • ϞσϧʢϞσϧͱ͸ʁʁʣ • ηογϣϯ • ͳͲ͸ผ్͝༻ҙʢͰ͖ΔͰ͠ΐʁʣ

  60. ॏཁͳϑΝΠϧ project App Root dir !"App App Class # !"HttpController

    # # $"Index.php ίϯτϩʔϥ !"Config.php ઃఆ !"EasySwooleEvent.php Πϕϯτ !"easyswoole easyswoole ίϚϯυ • ͜ΕΒ͸composer create-projectͰੜ੒Ͱ͖Δ • ॻ͔ͳ͖Ό͍͚ͳ͍ͷ͸࠷௿ݶIndex.phpͷΈʢޙड़ʣ
  61. • ͡Ό͋ɺͱΓ͋͑ͣىಈͯ͠Έ͔ͬ

  62. easyswoole͸σʔϞϯʹͳΔ • ·͋ɺphp-fpmͷΑ͏ͳ΋ͷ • easyswoole ίϚϯυʹҾ਺Λ͚ͭͯૢ࡞͢Δ • easyswoole startͱ͔

  63. None
  64. • ʮΊͬͪΌΧϥϑϧͳதࠃޠ…͚ͩͲ·͋ɺͳΜͱͳ͘Θ͔ Δɺ͍͚Δ͍͚Δʂʯ • ͨͱ͑͹… • --d -> ͚ͭΔͱσʔϞϯԽ •

    --workerNum-1 -> ϫʔΧʔ਺ • (--workerNum 1 Ͱ͸ͳ͍…) • --user-nobody -> nobodyϢʔβʔͰ࣮ߦ • (--user nobodyͰ͸ͳ͍…ʣ
  65. ϓϩηεɾϫʔΧʔϚωʔδϟʔ • ຊ౰ʹ࢖͏ͳΒɺ͘͢͝ॏཁͳ΍ͭ • start,stop,reload,daemonize • ϫʔΧʔ਺ௐ੔ • respawn •

    UIDɺGUIDઃఆ
  66. ʮPHPͳͷʹ͔ͬ͠Γ͠ͱΔʯ • ຊ౰ʹʢׂͱʣͪΌΜͱϓϩηεΛϚωʔδ͢Δɻ • ͪΌΜͱىಈ͢Δ͠ɺͪΌΜͱࡴͤΔɻ • γάφϧͰϦϩʔυͱ͔΋Ͱ͖Δʢָʂʂʣ • gracefull restart΋ͪΌΜͱ͢ΔʢΒ͍͠ɺ͋Μ·Γݫີʹݕূ

    ͯ͠ͳ͍ʣ • ʮPHPͰ͜ΜͳʹͪΌΜͱ࡞ͬͯ͋ΔϓϩηεϚωʔδϟɺॳ Ίͯݟ͔ͨ΋…ʯ
  67. (଍ճΓ͕ଞͷ͜Εܥͷ΍ͭΑΓ͔ͬ͠Γ͍ͯ͠Δ͜ͱΛ঺հ͢Δ ͨΊʹ࠷ॳʹ঺հ͠·ͨ͠ɻ ͕ɺ·͋ɺօ͞ΜPHPͰσʔϞϯ࡞ͬͨΓ͠ͳ͍Ͱ͔͢Ͷ…͸͸ ͸…)

  68. httpdʹͳΕΔʂ • ܁Γฦ͠ʹͳΓ·͕͢ɺhttpdʹͳΕ·͢ɻ • ͳΕΔͱ͍͏Ҏ্ʹಘʹॏཁͳ࿩͸ͳ͍ɺͳΕΔͱ͍͏͜ͱ͕ ॏཁɻ • fastCGI΋͏ઃఆͨ͘͠ͳ͍ΆΑ… • nginxͷίϯϑΟά͕…

  69. server { root /path/to/htdocs/; location / { proxy_http_version 1.1; proxy_set_header

    Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; if (!-e $request_filename) { proxy_pass http://127.0.0.1:9501; } } }
  70. ָʂʂʂ

  71. தؒ·ͱΊ • easyswooleίϚϯυͰىಈͰ͖Δ • httpd αʔόʔʹͳΕΔ • ʢ͜͜·Ͱ̍̌෼ʣ

  72. • ࣍͸ίʔυΛݟ͍͖ͯ·͠ΐ͏ • ΢ΣϒΞϓϦ͸ࡶʹ͍͑͹ίϯτϩʔϥʔ͕͋Ε͹Ͳ͏ͱͰ΋ ͳΔʢ๫ݴʣ

  73. ίϯτϩʔϥʔ(ϧʔλʔ) • easyswooleʹ͸ɺ֓Ͷϧʔλʔ͕2छྨ͋Δ • ඪ४ͷίϯτϩʔϥʔ • ΧελϜϧʔλʔ • ʢ͋Δ͍͸ΧελϜϧʔλʔ΋ࠩ͠ସ͑ʣ

  74. ඪ४ͷίϯτϩʔϥʔ • /{Ϋϥε໊}/{ϝιου໊} Έ͍ͨͳελΠϧͰϚοϓɺઃఆϑΝ Πϧ͸ແ͍ • /Auth/Login -> \App\HttpController\Auth::login() •

    / -> \App\HttpController\Index::index() • /HelloWorld -> \App\HttpController \HelloWorld::index() or \App\HttpController \Index::HelloWorld()
  75. • REST͸ͮ͠Β͍ • HTTP Method੾Γ෼͚͸ແ͍ɺURL಺ύϥϝλ΋ύʔε͠ͳ͍ • ʮผʹRESTͳͯ͘΋ࢮͳͳ͍͠…ʯʮࢮ͵ਓ΋͍Δʯ • ͦ͏͍͏ਓ͸΋͏ҰͭͷΧελϜϧʔλΛ͔͓ͭ͏ •

    γϯϓϧͩ͠ɺઃఆ͍Βͳ͍͠ɺURL͕៉ྷͰͳͯ͘΋͍͍͠… ͱ͍͏͜ͱͰݸਓతʹ͸ͪ͜Β͕… • ʢGET ?id=1͘Β͍ͳΒڐͤΔ͠ɺෳࡶͳύϥϝλ͸JSONΛ POST͢Ε͹͍͍ͱࢥ͍ͬͯΔݹ͍ਓؒͷൃ૝Ͱ͢ʣ
  76. ʢඪ४ͰͷʣσΟεύον͞ΕΔΫϥεͷߏ଄ • ʢϧʔλʔ͔ΒσΟεύον͞ΕΔίϯτϩʔϥ͜ͱΞΫγϣ ϯ…ʣ • EasySwoole\Core\Http\AbstractInterface\ControllerΛΠ ϯϓϦ͢Δ • PSR-7ޓ׵ͷrequest/response͕ϓϩύςΟܦ༝Ͱ࢖͑Δɻ

  77. <?php namespace App\HttpController; use EasySwoole\Core\Http\AbstractInterface\Controller; class Hello extends Controller {

    function index() { $this->response()->write('Hello easySwoole!'); } }
  78. ʮPSR-7͠Βͳ͍ਓʹ΋ૣΘ͔Γʂʯ • echoͱ͔͠ͳ͍(ͯ͠΋ϒϥ΢βʹ͍͔ͣɺίϯιʔϧʹग़Δ) • $this->response()->write('Hello easySwoole!');ͱ͔͢ Δ • $_POST['name']ͱ͔͠ͳ͍ •

    $this->request()->getParsedBody()['name'];ͱ͔͢Δ • (ར఺͸ผ్άάͬͯ͘Ε)
  79. ͍͔ͭ͘ͷಛผͳϝιου͕͋Γ·͢ • actionNotFound() -> 404ͷͨΊ • onException() -> ΤϥʔϋϯυϦϯάͷͨΊ •

    ʢ͜ͷ͋ͨΓ͸ __hook()ͷ࣮૷ΛΈΔͱཧղͰ͖·͢ʣ
  80. ΞΫγϣϯͷલͱޙͷॲཧࠩ͠ࠐΈ • onRequest() • ϦΫΤετલʹॲཧ • ϨεϙϯεΛฦͯ͠͠·͏ࣄ΋Ͱ͖ΔʢೝՄͳͲʣ • afterAction() •

    Action͕ਖ਼ৗʹऴ͑ͨΒॲཧ • ͲͪΒ΋࣍ʹ஋Λ౉͚ͨ͠Ε͹ɺ$this͕࢖͑·͢
  81. None
  82. ΋͏Ұͭͷख๏ɺΧελϜϧʔλʔ • nikic/FastRoute ͦͷ΋ͷɻͳͷͰলུ • https:/ /www.easyswoole.com/Manual/2.x/En/_book/Http/ router.html • ΞΫγϣϯͷ࡞Γ͸ඪ४ͷίϯτϩʔϥʔͱ͸͜ͱͳΓɺҾ਺

    ʹreq/resΛ͍ΕΔPSR-7ରԠFWʹΑ͋͘ΔܗʹͳΓ·͢ $routeCollector->get( '/user/{id:\d+}',function (Request $request ,Response $response,$id){ $response->write("this is router user ,your id is {$id}"); $response->end(); });
  83. ίϯτϩʔϥ·ͱΊ • ݟͯͷ௨Γɺී௨ͷʢPSR-7ରԠFWͷʣPHPͱେࠩͳ͍

  84. • ͪΐͬͱҧ͏ͷ͕࣍ͷΠϕϯτपΓ͔Β

  85. ΠϕϯτϑοΫ • Swooleʹ௚઀αʔϏεʢεϨουʢεϨουͰ͸ͳ͍ʣͷΑ͏ ͳ΋ͷʣΛ௥ՃͰ͖Δ • ΢ΣϒΞϓϦʢίϯτϩʔϥͳͲʣΑΓ্Ґʹଘࡏ

  86. frameInitialize() • easyswooleϑϨʔϜϫʔΫىಈ࣌ʹ࣮ߦ • swooleΛ࣮ߦ͢Δલ • ઃఆಡΈࠐΈ΍֤छΩϟογϡύʔδͳͲ

  87. mainServerCreate() • ϝΠϯαʔόʔʢϚελʔϓϩηεʣ࡞੒தɺswooleΤϯδϯ ࣮ߦલ • httpdҎ֎ʹಉ࣌ʹಈ͔͢αʔϏεొ࿥ͱ͔ • λΠϚʔ΍Cronͱ͔ • Ұ෦յΕʹ͍͘γϯάϧτϯॳظԽͱ͔

    • DB΁ͷ઀ଓPoolॳظԽͱ͔ • Ωϟογϡ༻Redis΁ͷ઀ଓͱ͔
  88. onRequest() • ΞΫγϣϯͷͦΕͱࣅ͍ͯΔͷͰলུ

  89. afterAction() • ΞΫγϣϯͷͦΕͱࣅ͍ͯΔͷͰলུ

  90. • ʮΠϕϯτɺԿʹ͔ͭ͏ͷʁʯ • ʮ͍΍͍΍ɺ͜ͷ͋ͨΓ͕ݸਓతʹΞπ͍ͱ͜Ζ΍Ͱʂʯ

  91. γϯάϧτϯͩʂʂʂ • PHPer͕ਆʹېࢭ͞ΕͨېஅͷՌ࣮ • ʮϦΫΤετΛ·͍ͨͰʯଘଓͤ͞ΒΕΔ͞·͟· • mainServerCreate΍frameInitializeͰ࡞Δͱɺࢠ΋࢖͑Δ • class΍ؔ਺ͷstaticͰ࡞Δͷ͸มΘΒͣ •

    ʢҰԠDI(DIͱ͸ʁ)΋͋ΔΑʣ
  92. • ஫ҙਂ͘࢖͏ͱ࠷ߴʹؾ෼͕͍͍ʢؾ෼ͳͷʁʣ • (PHP͸े෼ʹૣ͍ͷͰɺ࣮͸ۜͷ஄ؙͰ΋ͳ͍) • ҆શʹγϯάϧτϯʹ͢Δͷ͸ɺ͘͢͝ɺ೉͍͠ • ྫɿΫΤϦ࣮ߦॱং͕༧૝֎ʹͳΔɺϑΝΠϧग़ྗ่͕Ε ΔɺผͳॴͰΤϥʔͰյΕΔ •

    ීஈΤϥʔϋϯυϦϯάΛࡶʹߦ͍ͬͯΔਓ͸ࢮ͵ • ঢ়ଶΛ࣋ͨͳ͍ɺϓϦΩϟογϡͳͲॳظԽ͕ॏ͍͚ͩͱ͔ ͳΒൺֱత҆શ
  93. • prefork+eventͷ࢓૊ΈͳͷͰɺ࡞Δ৔ॴʹΑͬͯ͸άϩʔόϧ ʹ͸ͳΒͳ͍ • ͳͷͰɺmainServerCreate΍frameInitializeͰ࡞ΔͳͲ͢Δ • ϦʔΫ͕ා͍ͷͰɺे෼ʹmax_requestΛͷ͹ͯ͠ɺࢠʢϫ ʔΧʔʣͰੜ੒ͨ͠΄͏͕҆શ…ɻ • ઃఆճ਺ॲཧ͢ΔͱϫʔΧʔ͸࠶ىಈ͢Δ

    • ·͋ɺ࣮ߦස౓Λ1/10000ʹͳΕ͹े෼
  94. λΠϚʔͩʂʂʂ • ී௨ͷPHPͰ͸͍ͩͿ໘౗ͳࣄͷҰͭ • ʮΩϟογϡΛ10ඵʹ1౓ߋ৽͢ΔʯʮϋʔτϏʔτΛૹ৴͢ ΔʯΈ͍ͨͳ΍͕ͭ؆୯҆શʹͰ͖Δ • mainServerCreateʹɺλΠϚͷαʔϏεΛొ࿥͢Δͱಠཱͯ͠ಈ ͍ͯ͘ΕΔ

  95. public static function mainServerCreate(/* ... */): void { // timerͰ1ඵຖʹΠϯλʔόϧ࣮ߦ

    $register->add($register::onWorkerStart, function ($server, $workerId) { if ($workerId == 0) { Timer::loop(1000, function () { echo 'TICK!'; }); } }); }
  96. ඇಉظϦΫΤετʢΠϕϯτۦಈʣ • Πϕϯτۦಈͱ͍͑͹͜Εʢཁग़యʣ • ྫɿ֎෦APIʹϦΫΤετͯ݁͠ՌΛදࣔ͢Δ • ྫ̎ɿυϝΠϯΛνΣοΫ͍͕ͨ͠DNS໰͍߹Θͤ͸஗͍ • ී௨ͷphpͰ͸݁Ռऔಘ·Ͱී௨ʹϒϩοΩϯάʢϓϩηεઐ ༗ʣͯ͠͠·͏

    • ΠϕϯτۦಈͰ࡞Ε͹ϓϩηεઐ༗͠ͳ͍ • easyswoole͸ෳ਺ϓϩηεಈ࡞Ͱ͖Δ͔Βଈഁ໓తʹ͸ͳΒ
  97. • ʮϝΞυݕূͰDNS໰͍߹Θ͍ͤͨ͠ʯ • ඇಉظͰਖ਼Ҿ͖͢Δʹ͸swooleͷco::gethostbyname͕͋Δ • ίϧʔνϯͰͭ͘ΒΕͨ΋ͷ • ແବͳ࣌ؒΛɺϦΫΤετؒͰ༥௨ • co::gethostbyname͸ύϥϨϧऔಘͰ͸ͳ͍ͷͰʮͦͷϦΫΤ

    ετ͸଎͘͸ͳΒͳ͍ʯ • ͦͷ͔ΘΓɺଞͷϓϩηεͷअຐΛ͠ͳ͘ͳΔ
  98. • ʮίϧʔνϯͬͯͳʹʁʯ • ʮίʔϧόοΫແؒ஍ࠈΛ͖ͬ͢Γͤ͞ΔͨΊͷ΋ͷʯ • ίϧʔνϯ͚ͩͰ͸଍Γͳ͍ͱ͖͸࢓ํͳ͘ΠϕϯτۦಈͰॻ ͘ࣄ΋ͳ͍Θ͚Ͱ͸ͳ͍… • ͨͩɺswoole͸֤ॴͷίϧʔνϯԽΛઈࢍਪਐதʢΒ͍͠ʣ

  99. ϕϯνͰ࣮ଌ • ϗετ໊ΛҾ͖·͘ΔURLʹෛՙΛ͔͚Δʢlookup(Normal|Co)) • ͦͷ࣌ʹɺผͷΞΫγϣϯʹ΋ෛՙΛ͔͚ͯΈΔʢhelloΞΫγ ϣϯʣ • helloΞΫγϣϯΛlookup͕अຐ͠ͳ͍͔ʁͱ͍͏ςετ • Θ͔Γ΍͘͢͢ΔͨΊɺϫʔΧʔ਺:1ɺಉ࣌઀ଓ਺:32

  100. function lookupCo() {// ίϧʔνϯ൛ for($i=0; $i<100; $i++) { $ip =

    \Swoole\Coroutine::gethostbyname("dns.google.com"); } } function lookupNormal() {//ී௨൛ for($i=0; $i<100; $i++) { $ip = gethostbyname("dns.google.com"); } } function hello() { $this->response()->write('hello'); }
  101. • lookupNormalͷํʹෛՙΛ͔͚ͭͭɺhelloʹෛՙΛ͔͚Δࢼݧ • lookupNormal: 1.65 rps • hello: 1.69 rps

    • lookup͕ϒϩοΫͯ͠ɺhello͕΄΅ಉ͡଎౓ʹ
  102. • lookupCoʹෛՙΛ͔͚ͭͭɺhelloʹෛՙΛ͔͚Δࢼݧ • lookupCo: 1.64 rps / / ͜͜͸େ͖͘มΘΒͳ͍ •

    hello: 147.36 rps / / 100ഒʹ • lookup͕໰͍߹ΘͤதʹଞͷॲཧΛਐΊΔͷͰϒϩοΫͮ͠ Β͘ɺhello͕଎͘ͳͬͨ
  103. • ΠϕϯτۦಈʹϒϩοΩϯάॲཧ͸ఢ • ϚϧνϓϩηεͳΒɺ͜ͷ੍ݶ͸ଟগ؇࿨͞ΕΔ • ʮϒϩοΫ͢Δಉ࣌઀ଓ਺ʻϫʔΧʔ਺ʯ ͳΒ଎͍ • Swoole͸ͦͷػೳ͕ͪΌΜͱἧͬͯΔɺ࠷ߴʂ •

    ʢͱ͸͍͑ɺ҆қʹϫʔΧʔ਺૿΍͢ͷ΋ඍົʣ
  104. • ղܾʂͱ͍͍͍ͨͷ͕ͩ…ɻ

  105. ͜͜Ͱ࣮༻্ͷ໰୊ • swooleʹ͸ίϧʔνϯͷco::gethostbyname͕͋Δͷ͕ͩ… • λΠϓΛࢦఆͰ͖ͳ͍… • https:/ /wiki.swoole.com/wiki/page/790.html • ϝʔϧϑΥʔϜҊ݅Ͱ͸ɺMX΋Ҿ͚ͳ͍ͱ͜·Δ

    • ʮ͡Ό͋DNS protoΛswooleͰ࣮૷͠Α͏ʂʯ…ͱ͸ͳΒͳ͍ ʢͰ͖Δ͕ɺ໘౗͗͘͢͞Δʣ
  106. • HTTPSͰDNSͻ͚͹͍͍ΑͶɺͱ͍͏͜ͱͰ • DNS-over-HTTPS • https:/ /developers.google.com/speed/public-dns/docs/dns-over- https • swoole

    ʹ͸ Coroutineͷhttp client͕͋Δ
  107. protected function isProbablyExistsMailServer($host) { $goog_dns = "dns.google.com"; $dns_ip = static::getHostByNameWithCache($goog_dns);

    $cli = new \Swoole\Coroutine\Http\Client($dns_ip, 443, true); $cli->setHeaders([ 'Host' => $goog_dns, "User-Agent" => 'php', 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip', ]); $encoded_host = urlencode($host);
  108. $cli->get("/resolve?name={$encoded_host}&type=MX"); $mx_records = json_decode($cli->body, true); $cli->get("/resolve?name={$encoded_host}&type=A"); $a_records = json_decode($cli->body, true);

    $cli->close(); return isset($mx_records['Answer']) || isset($a_records['Answer']); } function some() { $res = $this->isProbablyExistsMailServer('example.jp') ? "found":"notfound"; }
  109. • ͱ͍͏͜ͱͰɺແࣄAϨίʔυͱMXϨίʔυΛҾ͚ΔΑ͏ʹɺ • ΋ͪΖΜεοΩϦCoroutineͰͶʂ

  110. தؒ·ͱΊ • ϝΠϯϓϩηεͷଘࡏʢΠϕϯτʣ • ΠϕϯτۦಈΛ೰·ͤΔϒϩοΩϯάॲཧΛ؆ศʹճආͰ͖Δ ͋Ε͜Ε • ͦͯ͠ΠϕϯτʹϓϩάϥϜΛొ࿥ͯ͠αʔϏεԽ

  111. None
  112. • (લͷਤ͸֓೦ਤͰ͢ɺϝΠϯϓϩηε͋ͨΓͷߏ଄͕ຊ෺ͱҧ ͏͸ͣ) • ݸਓతʹ͸৭ʑΞπ͍͠ɺօ͞Μʹ΋આ໌Λ্͍͕ࠩ͛ͨ͠ɺ ͕࣌ؒͳ͍ͷͰলུ

  113. • ͦͷଞʹ΋easy swooleʹ͸ศརػೳ͋Γ·͢ɻ

  114. λεΫ • easy swooleʹ͸૊ΈࠐΈͰδϣϒϫʔΧʔ͕͋Δ • ΫϩʔδϟΛ࡞ͬͯTaskManager::async()ʹೖΕΔ͚ͩͳͷ Ͱָ • ஋ΛΫϩʔδϟʹuseͰ͚ͭͯૹΕ·͢ •

    ผ్δϣϒϫʔΧʔ͕͍Βͳ͍ͷͰ؆୯ศརʂʂ
  115. None
  116. λεΫͷొ࿥ class Index extends Controller { function index() // ͱ͋Δίϯτϩʔϥʔ

    { // ... $this->response()->write('Ϩεϙϯε'); $some = 'data'; TaskManager::async(function () use ($some) { $log = new Log; $log->set($some); // ॏ͍ॲཧͳͲ }); // ผʹλεΫొ࿥͸࠷ޙͰͳͯ͘΋Α͍ } }
  117. • TaskManager::async()͸δϣϒొ࿥͔͠͠ͳ͍ͷͰ • ʢλεΫͱͳΔΫϩʔδϟΛγϦΞϥΠζͯ͠อଘͨ͠Βʣ • ࣍ͷߦ΁͢͢Ή • ʢͭ·ΓɺϦΫΤετ͕ऴΘΔʣ

  118. λεΫͷॲཧ • ޙͰtaskΛϫʔΧʔϓϩηε͕र࣮ͬͯߦ • ಛʹઃఆ͸ແ͍ʂ؆୯ͩʂ • ʮ͑ͬɺผϓϩηεʹΫϩʔδϟΛ౤͛Δͷʁʯʮ͸͍ʯ • SuperClosureͰγϦΞϥΠζͯ͠౤͍͛ͯΔ •

    ʢͳͷͰɺෳࡶͳσʔλʢFDΛ΋ͬͯΔͱ͔ʣΛΫϩʔδϟ ʹ͍ΕΔͱͩΊͰ͢ʣ
  119. easySwoole\Cache • ʮ໰͍߹Θͤ͢ΔͳΒΩϟογϡ͸ඞਢͰ͠ΐʯʮͤ΍ͳʯ • easyswooleͰ࢖͑Δ؆୯ͳKVS • ϩʔΧϧFSɺredisɺmemcachedͳͲΞμϓλ • ʢʮੌ͘ྑ͍΋ͷʯͰ͸ͳ͍ɺಛʹswooleಠಛͷػೳ͸͔ͭͬ ͯͳ͍͠ʣ

    • ·͋ɺ؆୯ศརͳͷͰɻ
  120. public static function frameInitialize(): void { // ॳظԽ Cache::init(new Files([

    'expire' => 0, // ߏթݖ௹ൈࡗ 'cache_subdir' => true, // 䇖ఓሰଢ੣թ٢ 'prefix' => '', // ߏթ໓ࡱުሗ଀ 'path' => __DIR__.'/cache', // อଘDIR 'hash_type' => 'md5', // จ໊݅తᄒرํࣜ 'data_compress' => false, // ఓႨߏթଽಸ࿢෪ 'thread_safe' => false, // ϑΝΠϧϩοΫ͢Δ͔ 'lock_timeout' => 3000, // จ݅࠷Ӊ෭קൈࡗ(ms) ]));
  121. Cache::get('name', 'औಘͰ͖ͳ͔ͬͨΒ'); Cache::set('name', 1, $ttl); Cache::has('name'); Cache::delete('name'); Cache::clear(); Cache::set('name', 1);

    Cache::inc('name');// +1 Cache::dec('name', 10);// -10 Cache::pull('name', 'औಘͰ͖ͳ͔ͬͨΒ');// pop Cache::remember('name', 'value'); // ͳ͚Ε͹set
  122. protected function getHostByNameWithCache($host){ $val = Cache::get($host, null); if(!is_null($val)){ return $val;

    } $ip = \Swoole\Coroutine::gethostbyname("dns.google.com"); Cache::set($host, $ip, 3600); return $ip }
  123. தؒ·ͱΊ • TaskͰॏ͍ॲཧ͸શ෦ޙΖʹ౤͛ͯɺϨεϙϯεΛฦͤΔ • લड़ͷඇಉظϦΫΤετΛ͔ͭ͏ʹͯ͠΋΍ͬͺΓΩϟογϡ ͸͍͔ͨ͠ΒCache͸ศར • easyswooleʹ͸Task΍CacheͳͲɺ΄΅ઃఆͳ͘γϯϓϧʹύο ͱ͔ͭ͑Δػೳ͕͋ͬͯศར

  124. easy-swoole঺հ·ͱΊ • ϝʔϧϑΥʔϜΛߴ଎Խ͢Δʹ͋ͨͬͯ • ϑΥʔϜΛදࣔ -> ूܭσʔλΛද͓ࣔͯ͘͠ • ֬ೝը໘ ->

    ϝΞυଘࡏ֬ೝͷDNS໰͍߹Θͤ͸ඇಉظͰ • อଘ -> λεΫԽͯ͠ͱʹ͔͘ฦ͢ • ͱ͍ͬͨࣄ͕easy swooleͰϥΫϥΫͰ͖Δ
  125. • ී௨ͷPHPͰॻ͘ͱɺ΍ΕผϓϩηεʹδϣϒΩϡʔͩɺ΍Ε redisͩɺͳͲͱෳࡶԽ͕ͪ͠ɻ • ϑϧελοΫFWͳΒେମೖͬͯΔ͚Ͳɺ๩͍࣌͠ʹyak shaving ͕ḿΔࣄଶ΋͠͹͠͹ • ʢ·͋ɺԿͰ΋ॳΊͯ࢖͏࣌͸৭ʑמΔඞཁ͸͋ΔͰ͠ΐ͏ ͕…ʣ

    • ͦ΋ͦ΋୯ͳΔςϯϙϥϦͳԠืϑΥʔϜʹͰ͔͍ϑϨʔϜ ϫʔΫ࢖͍ͨ͘ͳ͍
  126. • ͦ͜Ͱɺeasy swooleͳΒɺΑ͋͘Δʮ͋Ε͕΄͍͠ʂʯʹγϯ ϓϧͳͨ͑ͯ͘͜ΕΔͧʂ • ʢΠϕϯτۦಈʹ׳Ε͍ͯͳ͍ਓͰ΋ɺeasyswoole͸γϯϓ ϧͩͱࢥ͏…ଟ෼ʣ • swoole·ͰؚΊͯɺ͞Βʹ৭ʑػೳ͕͋Δͧʂࠓճ঺հ͠ͳ͔ ͚ͬͨͲɺDBͷ઀ଓpoolͱ͔΋͋Δͧʂ

    • ͱ͍͏͜ͱͰswoole͸ͪΐͬͱ…ͱ͓΋͍ͬͯͨօ͞Μɺͥͻ easy swooleͰপʹೖͬͯΈ·͠ΐ͏ʂ
  127. ͜͜·Ͱ͘Ε͹ͱΓ͋͑ͣ੒ޭʢʁʣ

  128. ͔͜͜Β༨ஊ • ͋Δ͍͸ຊฤ • ʢ͜͜·Ͱ̍̑෼Ͱ͍͖͍ͨʣ

  129. Swoole͕དྷ͍ͯΔ݅ • օ͞Μ൒৴൒ٙɺ͋Δ͍͸ྵ৴શٙͰ͠ΐ͏͕ • Composer ύοέʔδొ࿥؂ࢹϘοτ(@call_user_func)ͷ௚ۙ πΠʔτΛgrepͯ͠ΈΔͱ…

  130. $ cat bot_log |grep swoole| wc -l 25 $ cat

    bot_log |grep laravel| wc -l 333 $ cat bot_log |grep slim| wc -l 15 $ cat bot_log |grep cakephp| wc -l 22 $ cat bot_log |grep zend| wc -l 8
  131. ͍͢͝ɺ୹ظؒʹ̎̑΋্͕͍ͬͯ Δʂʂ • ʢ୹ظؒͱ͸ʁʁʁʣ • Cake΍Slim,Laravelͱͷ૬ରײΛָ͓͠Έ͍ͩ͘͞ɻ

  132. GitHubͰ΋εΰΠଟ͍

  133. packagist ΋ଟ͍

  134. • (ݟͮΒ͍…ʣ • 1ϖʔδ20͕݅18p͋ͬͨͷͰɺ350ݸ͘Β͍͸͋Δ͸ͣ

  135. awesome-swoole ΋૿͑ͨʢʁʣ • https:/ /github.com/swooletw/awesome-swoole • https:/ /github.com/yannsun/awesome-swoole • https:/

    /github.com/swoole/swoole-src/wiki/Awesome • ʢଟ͚Ε͹ྑ͍ͱ͍͏΋ͷͰ΋ͳ͍͕ʣ
  136. • laravelͱ͔ɺ֤छwafͷόΠϯσΟϯάʢ΍ྫʣ΋૿͑ͨ • Ͱ΋·͋ɺϑϧελοΫͷFWΛSwooleͰಈ͔͢ҙຯ͸…ɻ • Promiseͱ͔mqttͱ͔ftpserverʢʁʁʁʣͱ͔࣮༻తͳपลϥΠ ϒϥϦ΋

  137. ࣍ͷόʔδϣϯ͸̐ʂ

  138. • ͳΜͰ͕̏εΩοϓ͞ΕΔͷ͔͸Θ͔ΒΜ • ੈͷதʹ͸Ṗ͕ଟ͍ɻ

  139. ϑϨʔϜϫʔΫ΋ग़࢝Ί͍ͯΔ

  140. None
  141. ͱ͍͏͜ͱͰ • ࠓճ͸ʮָʯͳͷͰeasyswooleΛ঺հ͕ͨ͠ɺຊ౰ʹ༷ʑͳϥ ΠϒϥϦ͕Ͱ͍ͯΔ • ͨΓͳ͍ͷ͸೔ຊਓ͕ಡΈ΍͍͢৘ใ΍νϡʔτϦΞϧ͚ͩͰ ͸ʁʢʁʁʣ • swooleͷϏοά΢Σʔϒʹ৐Δ͔͠ແ͍ʢݹ͍ʣ •

    ʢࣗ෼Ҏ֎ʹ࢖ͬͯΔਓΈͨ͜ͱ͕ͳ͍ͷ͕೰Έʣ
  142. • Tencentɺͬ͞͞ͱʢུʣͱ͔ผͷݴޠʹ͍͔ͣɺҊ֎ͪΌΜͱ ͚͍͍ͭͮͯͯ͢͝ɻ • PHPݴޠʹϋϚ͍ͬͯΔʢʣձࣾ͸ࠜੑͰPHPΛ೧͡ۂ͛Δ͔ Β͍͢͝ɻ

  143. ͸͍

  144. Πϕϯτۦಈͱͦͷଞʹ͍ͭͯൺֱ • Πϕϯτۦಈͬͯ৭ʑ͋Δ • swoole͸Πϕϯτʴϓϩηε • reactPHP౳͸Πϕϯτ • fpm΍mod_php͸ϓϩηεʢpreforkʣ •

    cgi͸ϓϩηε
  145. None
  146. • શ෦͕ผΕ͍ͯΔ • ͦΕͧΕ࣮ߦ͍ͯ͠ΔPHPίʔυ͕ҟͳͬͯ΋Α͍ • phpࣗମ͸prefork͞ΕΔ͕ɺຖճϓϩάϥϜ͸ىಈ͍ͯ͠Δ • ͳͷͰɺCGIɺmod_phpͱ΁Μͳ͕ࠩগͳ͍

  147. None
  148. • యܕతͳΠϕϯτۦಈ • ࣮ߦ͞ΕΔPHPίʔυ͸͔̍ͭ͠ͳ͍ • ྫ reactPHPͳͲ • ϝΠϯϧʔϓ͕ҰͭͳͷͰɺϝΠϯϧʔϓͰͳʹ͔٧·Δॲཧ ͕͋Ε͹શ෦ࢭ·Δ

    • ຊ౰ʹϊϯϒϩοΩϯάʹॻ͚Ε͹ޮ཰తʹΈ͑Δ͕ɺίΞ਺ ͕׆͔ͮ͠Β͍
  149. None
  150. • ΠϕϯτͱϓϩηεΛ͋Θͤͨ΋ͷɺswoole͸͜ΕɻreactPHP ΛϚϧνϓϩηεʹ͢Δ΋ͷ΋͋Δ • ʢجຊతʹ͸ʣࣄલʹPHPίʔυΛ࣮ߦ͠ίʔϧόοΫʢϝΠ ϯϧʔϓʣΛ࡞ΔͷͰɺPHPίʔυ͸Ұͭ • ϝΠϯϓϩηε͕ड͚ͯࢠʹΘͨ͢౳࣮૷͕΍΍͍͜͠ • ଟগϒϩοΩϯά͕͋ͬͯ΋ɺଞͷϓϩηε͕͕Μ͹ΕΔ༨஍

    ͕࢒Δ • ཧ࿦্ɺreload͕Ͱ͖Δ • ເ͕͋Δʂʢʁʣ
  151. ͸͍

  152. ʮ…஗͍Μ͚ͩͲʁʯ • ·ͣɺଟॏԽͰ͖ͳ͖ΌͦΕͳΓͰ͢ • 100rpsҎ্͕ඞཁͳ͍ͳΒɺޡ͔ࠩͳ… • MacͰ΋ಈ͕͘ɺ͍ͬ͢͝஗͍ • ϕϯν͢ΔͳΒLinuxͰɻ •

    ʢಉ͡MacͷVM্Ͱ͏͔ͯ͝͠ɺ5ʙ50ഒ଎͍ʣ
  153. ͸͍

  154. Πϯετʔϧํ๏͸ʁ • swoole͸ී௨ͷ֦ுͱಉ͡ $ git clone https://github.com/swoole/swoole-src.git $ cd swoole-src/

    $ phpize $ ./configure --enable-openssl $ make all $ make install $ echo "extension=swoole" >> /etc/php/7.2/cli/conf.d/30-swoole.ini
  155. • ґଘ͕ͳ͍ͷͰɺ͢ΜͳΓ͸͍Δͱࢥ͍·͢ • peclͰ΋ೖΕΒΕΔΒ͍͠ʢ࠷৽όʔδϣϯΛ͔Μ͕͑Δͱɺ git͕Α͍͔ͱ) • easy-swoole͸create-project͕଎͍ $ composer create-project

    easyswoole/app easyswoole
  156. swooleͷόʔδϣϯʹ͍ͭͯ • 2ܥʹͳ͍ͬͯͩͿͨͬͯɺͦΕͳΓʹ҆ఆ͖ͯ͠·ͨ͠ɻ • ͕ɺεςʔϒϧͱ͍͏֓೦͸͋Μ·Γͳ͍ؾ͕͠·͢ɻ • มͳڍಈ͕͋ͬͨΒɺόʔδϣϯΛ͛ͯ͞Έ͍ͯͩ͘͞ • ৽نػೳ͸େମόάͬͯ·͢ɺಛʹOSXͰ͸ɺ͝஫ҙɻ

  157. ։ൃαϙʔτ • 䎔ဋIDEॿख(ิ׬༻ϔϧύ) • composer require easyswoole/swoole-ide-helper • https:/ /www.easyswoole.com/Manual/2.x/Cn/_book/Introduction/

    install.html • (Swoole͸C֦ுͳͷͰɺϔϧύΛ͍Εͳ͍ͱิ׬Ͱ͖ͳ͍Օ ॴ΋ʣ • ʢrequire-devͰeasy-swooleΛ͍ΕͨΒೖΔʣ
  158. • ಣࡆᄛ(Hotreload) • ػೳͱͯ͋͠ΔΘ͚Ͱ͸ͳ͍͕ɺfswatchΛ͔ͭͬͨྫΛهࡌ • https:/ /www.easyswoole.com/Manual/2.x/Cn/_book/Introduction/ server.html

  159. ͸͍

  160. systemd • ී௨ʹॻ͚͹ී௨ʹಈ͘ • systemdͰىಈ͢Δͱɺϩά͸LogsͰͳ͘journalctlͰݟΔ͜ͱʹ ͳΔ • ʢ࣍εϥΠυʹઃఆαϯϓϧʣ

  161. [Unit] Description=swoole service After=network.target [Service] Type=simple LimitNOFILE=65535 WorkingDirectory=/path/to/easyswoole-sample/ ExecStart=/usr/bin/php /path/to/easyswoole-sample/easyswoole

    start --pid-/path/to/easyswoole-sample/Log/pid.pid ExecReload=/bin/sh -c "/bin/kill -USR1 `cat /path/to/easyswoole-sample/Log/pid.pid`" ExecStop=/usr/bin/php /path/to/easyswoole-sample/easyswoole stop --pid-/path/to/easyswoole-sample/Log/pid.pid Restart=always User=www-data [Install] WantedBy=multi-user.target
  162. reloadपΓʹ͍ͭͯ • ϝΠϯϓϩηεʹSIGUSR1ΛૹΔͱϦϩʔυ • opcache΋ΫϦΞ͞ΕΔ • easyswooleͷreload͸ͳΜ͔͓͔͍͠ࣄ͕͋ΔͷͰɺʮࢲ͸ʯϝ ΠϯϓϩηεʹγάφϧΛૹ͍ͬͯΔ • ΋͠ࢠϓϩηεͷPID͕มΘΒͳ͚Ε͹(respawnͯ͠ͳ͚Ε

    ͹)͓͔͍͠ • ʮίʔυΛಡ΋͏ʯ
  163. ௥هɿࣗલͰcacheΛੜ੒͢ΔϥΠϒϥϦʹ஫ҙ • Πϕϯτۦಈ͕લఏͰॻ͔Ε͍ͯͳ͍ίʔυ΋ଟ͍ • reloadͯ͠΋൓ө͞Εͳ͍ʂ౳ • ϑΝΠϧʹॻ͖ग़͢΍ͭ͸ແ࿦ͦͷΩϟογϡ΋ফͯ͠reload͢ Δ͜ͱ • respawn·Ͱͷִ͕ؒ௕͍ͳΒɺcache͸͠ͳͯ͘΋Α͍ͷͰ

    ͸ʁ
  164. nginx ͱͷ࿈ܞ • nginxͱswooleΛಉډͤ͞ΔͳΒunix socket͕Φεεϝ • ͨͩɺߴෛՙαʔϏεͳΒswooleͱnginxΛN:1ʹ͢Δ͜ͱ΋͋ ΔͩΖ͏ • ͜͏͍͏࣌ɺhttpdʹͳΔͷ͸ָͰ͍͍Ͱ͢Ͷʂ

    • ʢ࣍εϥΠυʹઃఆαϯϓϧʣ
  165. server { listen 80; root /path/to/htdocs; server_name _; location /

    { proxy_http_version 1.1; proxy_set_header Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; # proxy_pass http://127.0.0.1:9501; proxy_pass http://unix:/tmp/swoole.sock:; } }
  166. unix socket ࣌ͷConfig.php • HOSTʹύεΛࢦఆ͢Δ • PORT͸ແࢹ͞ΕΔ • sockϑΝΠϧͷPermission͸ࢦఆෆՄɺnginxͱҧ͏UIDͰಈ͔͢ ͳΒsockϑΝΠϧΛchmod͢ΔͱΑ͍(swooleͷαϯϓϧίʔυ

    Ͱ͸ͦ͏ͳ͍ͬͯͨ) • Bad gatewayͰɺchmod 777 /tmp/swoole.sock࣏ͯ͠Ε͹͜ ΕɺϩάΈΕ͹Θ͔Δɻ
  167. return [ // ... 'MAIN_SERVER'=>[ 'HOST'=>'/tmp/swoole.sock', 'PORT'=>0, 'SERVER_TYPE'=>\EasySwoole\Core\Swoole\ServerManager::TYPE_WEB_SERVER, 'SOCK_TYPE'=>SWOOLE_UNIX_STREAM, //

    ...
  168. EOL