Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

ࣄྫͱ͔ϕϯνͱ͔

Slide 5

Slide 5 text

͋Δ೔ͷࣄྫ • ಥવͷMOSUCON

Slide 6

Slide 6 text

MO-ney ga SUCO-shi sika N-ai

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

• !Ϣʔβʔ͸ؾܰʹϦϩʔυͳ͞Δ • "஗͍ͱπΠʔτ͞ΕͯɺౖΒΕΔ • #ͬ͞͞ͱฦ͚ͩ͢Ͱ͘͢͝มΘΔ • $εύΠΫ͸͍͔ͭऴΘΔͷͰɺҰॠ଱͑ΒΕΕ͹উͪ • %ҰൃͷΩϟϯϖʔϯͳͷͰɺϝϯς͸ͳ͍ • &(ͱݟ͔͚ͤͯԿճ΋΍Δࣄ͸Α͋͘Δ)

Slide 10

Slide 10 text

࣮ࡍPHP͕ʢׂͱʣ޲͔ͳ͍ࣄҊ • !preforkͷΈʢ΄΅ʣಉظॲཧͷPHP • nginx+php-fpmͩͱ͍ͩͿϚγ͕ͩɺApache+PHP͸ࢮ • ࣌ؒଳʹΑͬͯ͸ڱଳҬͳϞόΠϧ૬ख͸ݫ͠Ί • !ΞΫηεຖʹશ෦ΛॳظԽ(ಛʹॏྔڃFW) • "ΞΫηε͕ڈΕ͹໭Δ • #จݴमਖ਼ʹ࠶ىಈ͕͍Βͳ͍ʢॏཁʣ

Slide 11

Slide 11 text

࣌ؒͷ͔͔ΔॲཧΛͳ͍ͨ͘͠… • ֎෦API΁ͷΞΫηε • DB΁ͷอଘ • …ͳͤ͘ͳ͍

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

޻෉ͷํ๏͸͍Ζ͍Ζ͋Δ͕ • ฒ΂Δʁ -> ͓͕ۚͳ͍ • ཁ݅Λม͑Δʁ -> ͓٬͞Μ͕NO • nginx + php-fpm + ૉPHPʁ -> ී௨͗͢Δʢʁʣ

Slide 14

Slide 14 text

ͱ͍͏͜ͱͰຊ୊ • ʮ͔͔ͤͬͩ͘ΒԶ͸swooleΛબͿͥʂʯ • ͱ͍͏͜ͱͰɺswooleͰ૊ΜͩΒͲ͏ͳΔͷʁͱ͍͏࿩Λ͠· ͢ɻ

Slide 15

Slide 15 text

αϯϓϧͱϕϯν

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

ϕϯνϚʔΫ − બख঺հ - nginx + swoole - nginx + php-fpm

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

php (ڞ௨ɺ΄΅aptͰ͍Εͨ··) • PHP 7.2.6 • opcache • xdebugͳͲ͸ແ͠ • swoole֦ு͸swooleςετ࣌ͷΈ • (cliʹͷΈ֦ுΛϩʔυʣ

Slide 23

Slide 23 text

nginx (ڞ௨ɺ΄΅aptͰ͍Εͨ··) • worker_processes auto; • worker_connections 768; • gzip on; • tcp_nopush on; • tcp_nodelay on; • keepalive_timeout 65;

Slide 24

Slide 24 text

php-fpmʢൺֱ༻ʣ • FW͸ slim 3 • fpmઃఆ • pm = static • pm.max_children = 8 • fastcgi unix socket઀ଓ

Slide 25

Slide 25 text

swoole • swoole࠷৽൛ʴFWͱͯ͠easyswoole • ϫʔΧʔઃఆ • worker_num 8 • max_resquest 50000 • fastcgi unix socket઀ଓ

Slide 26

Slide 26 text

࣮ଌ

Slide 27

Slide 27 text

ϕϯν̍ • / (ϑΥʔϜ)Λճ͢ɺrpsΛܭଌ • wrk -c {N} -t 8 -d 10s -R 100000 http:/ /{host}/ • ಉ࣌઀ଓ਺ΛมԽ͍ͤͯ͘͞(c͕1ͷ࣌ͷΈɺt΋1) • ̎ճճͯ͠ߴ͍ํΛ࠾༻ • Τϥʔ͕ͰͨΒ0ѻ͍

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

N php-fpm swoole 1 402 2530 50 946 5782 100 931 5429 150 Τϥʔ 5335 200 - 5994 500 - 6612 1000 - Τϥʔ

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

ϕϯν̎ • /post (อଘ)Λճ͢ɺrpsΛܭଌ • ab -c {N} -n 1000 -p postdata -m POST -T "application/x-www- form-urlencoded" http:/ /{host}/post • ಉ࣌઀ଓ਺ΛมԽ͍ͤͯ͘͞ • ̎ճճͯ͠ߴ͍ํΛ࠾༻ • Τϥʔ͕ͰͨΒ0ѻ͍ • (͕ࠩϠό͗͢ΔͷͰDNS໰͍߹ΘͤΛൈ͍ͯ͋Γ·͢)

Slide 32

Slide 32 text

N php-fpm swoole 1 110 303 10 412 477 100 408 514 150 Τϥʔ 537 200 520 400 540

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

• 500Ҏ্͸ɺab͕ΤϥʔΛు͍ͨͷͰ…

Slide 35

Slide 35 text

ࢀߟه࿥ helloworldϕϯν • wrk -c {N} -t 8 -d 10s -R 100000 http:/ /{host}/hello • ୯ʹʮhelloʯ͚ͩΛฦ͢ɺయܕతͳʮϕϯν൪௕ʯίʔυ • ͸͖ͬΓݴͬͯҙຯ͸ͳ͍͆

Slide 36

Slide 36 text

N php-fpm swoole 1 948 3187 50 1984 5935 100 1909 7303 150 Τϥʔ 6649 200 7649 500 8915

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

ࣄྫɾϕϯν·ͱΊ • ݟͯͷ௨Γɺଟॏ઀ଓ਺͕૿͑ͨΒ͕ࠩ͋Γ·͢ɻ • Write͕ೖΔͱɺϩδοΫʹඞཁͳ͕͔͔࣌ؒΔͷͰɺ྆ऀͷࠩ ͸ॖ·Δ • php-fpm΋ϓϩηε਺Λ૿΍͢ͱվળ͢Δࣄ΋͋Δ͕ɺ࣮ߦί ετ΋্͕Δ • BacklogΛ;΍͢ͱόοϑΝʹͳΔ͕ɺ΍͸Γ͍͔ͭ٧·Δ

Slide 39

Slide 39 text

• readʹ͍ͭͯ͸αϯϓϧίʔυͷߏ଄తʹswoole͕ζϧ͍ॴ΋ ͋Γ·͢ • swoole᩾ርͰ݁ߏҧ͏͠Ͷɺৄ͘͠͸͓खݩͰࢼͯ͠Έͯ͘ ͍ͩ͞ • ʮؾ߹Λ͍Εͯͭ͘Ε͹ʯɺ΋ͬͱੑೳ͸͕͋Δ͸ͣʢ͔͠͠ ͦ͜·ͰखؒΛ͔͚Δͷ͔ʁʣ • ʮͱ͜ΖͰɺ͜ΕϝʔϧϑΥʔϜͳͷ͔ʁʯʮ࣮ࡍʹ͸mailgun ͱ૊Έ߹ΘͤΔΜͰ͚͢Ͳɺ·͋ͦͷ͔ΘΓʹdnsΫΤϦ͍Εͯ ͋Γ·͢…ʯ

Slide 40

Slide 40 text

͜ͷࠩ͸… • swooleͷྗʂ • …ͱ·Ͱ͸͍͑ͳ͍͚ͲɺϩδοΫ͕ϔϏʔͰͳ͚Ε͹swoole ͷ΄͏͕ଟগ଎͍ɻ • rps͕ࡾܻ௒͑ΔͳΒɺ༗ҙ • ٯʹɺ1ܻ2ܻͩͱେࠩ͸ͳ͍

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

• ʢ͜͜·Ͱ̑෼ʣ

Slide 43

Slide 43 text

easy-swooleͷ঺հ

Slide 44

Slide 44 text

easy-swooleͱ͸? • https:/ /www.easyswoole.com/ • swoole Λ͔ͭͬͨϑϨʔϜϫʔΫ • े෼࢓ࣄʹ΋࢖͑Δʢݸਓͷײ૝Ͱ͢ʣ • ؆୯γϯϓϧʂ • ݁ߏαΠτཱ͕೿ɺ͔͠͠େମதࠃޠ

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

ᐽՍಛੑ EasySwoole ੋҰ׺جဋSwoole Server 䇖ؿ֥ӈሁଽթ྘֥ٳ҃ൔ PHPᐽՍɼህູAPIࣕੜɼϥຂԮ๤PHPᄎྛଆൔᄝࣉӱߒఏބ໓ࡱ ࡆᄛഈջট֥ྟି෥ാbEasySwoole ߴ౓෧૷ྃ Swoole Server ࣕ ґچົӻ Swoole Server ݪ༗ಛੑɼࢧ࣋ಉൈࠁކࡓ๐HTTPɺࣗఆ ၬTCPɺUDPླྀၰđಞषؿᆀၛቋ֥֮࿐༝ӮЧބࣚ৯щཿԛ؟ࣉ ӱđॖၳ҄đۚॖႨ֥ႋႨڛༀb

Slide 47

Slide 47 text

ʢػց຋༁ͯ͠Έ·͠ΐ͏ʣ

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

• ʮͳΔ΄ͲΘ͔ΒΜʯʮͱʹ͔ͦ͘͢͝͏ʯ • ൈਮ͢Δͱ… • Swooleͱ͍͏΍ͭΛϕʔεʹ͍ͯ͠Δ • SwooleΛ͏·͘ϥοϓͯ͠࢖͍΍͍ͯ͘͢͠Δ • ࠷খݶͷίετͰֶशɺ࣮૷͕Ͱ͖ΔΑ͏ʹ

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

࢖ PHP 䇖ؿದჴॖၛщཿۚྟି֥ၳ҄ѩؿ TCPɺUDPɺUnix SocketɺHTTPɼWebSocket ෰ༀbSwoole ՄҎኅᔟႋႨႿ޺৳ຩa ၍׮๙ྐaఒြೈࡱaᄉ࠹ෘaຩ઎Ⴓ༤a໾৳ຩčIOTʣɺӚ৳ ຩaᇆିࡅं֩ਵთb ࢖༻ PHP + Swoole ࡞ູຩ઎๙ྐॿࡏđॖၛ ൐ఒြ IT ݚؿ๶ི֥ؒੱննิശđ۷ࡆህᇿႿषؿԷྍӁ௖b

Slide 53

Slide 53 text

Swooleɿຊ൪؀ڥ༻PHPඇಉظωοτϫʔΫ௨৴Τϯδϯ PHP։ൃऀ͸ɺߴੑೳͳඇಉظͷTCPɺUDPɺUnix SocketɺHTTPɺ ͓ΑͼWebSocketͷಉ࣌αʔϏεΛ࡞੒Ͱ͖·͢ɻε΢Υʔϧ͸ɺ ΠϯλʔωοτɺϞόΠϧ௨৴ɺΤϯλʔϓϥΠζιϑτ΢ΣΞɺ Ϋϥ΢υίϯϐϡʔςΟϯάɺΦϯϥΠϯήʔϜɺΠϯλʔωο τʢIOTʣɺΧʔωοτϫʔΩϯάɺεϚʔτϗʔϜͳͲͷ෼໺Ͱ ޿͘࢖༻Ͱ͖·͢ɻωοτϫʔΫ௨৴ϑϨʔϜϫʔΫͱͯ͠PHP + SwooleΛ࢖༻͢Δ͜ͱͰɺاۀͷITݚڀ։ൃνʔϜͷޮ཰Λେ෯ ʹߴΊɺֵ৽తͳ੡඼ͷ։ൃʹ΋ͬͱूத͢Δ͜ͱ͕Ͱ͖·͢ɻ

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

• ͓ΑͦPHPΒ͘͠ͳ͍ػೳ͕௥ՃͰ͖ΔC֦ு • ࠷ۙݸਓతʹ޷͖ • ࠓճ͸ʮΠϕϯτۦಈͰߴ଎ͳΞϓϦ͕PHPͰ࣮ݱͰ͖Δʯ͕ ॏཁ • httpdʹͳΕΔʂʢݸਓతʹॏཁʣ • ʢੋඇௐ΂ͯΈ͍ͯͩ͘͞ɺӳޠαΠτ΋͋ΔΑʂʣ

Slide 56

Slide 56 text

• ·ͱΊΔͱɺ • swooleͰಈ͘ϑϨʔϜϫʔΫͷҰ͕ͭeasy swoole • ʢphpͰಈ͘ϑϨʔϜϫʔΫͷslimɺΈ͍ͨͳଊ͑ํͰྑ͍Ͱ ͢ʣ

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

ॏཁͳϑΝΠϧ project App Root dir !"App App Class # !"HttpController # # $"Index.php ίϯτϩʔϥ !"Config.php ઃఆ !"EasySwooleEvent.php Πϕϯτ !"easyswoole easyswoole ίϚϯυ • ͜ΕΒ͸composer create-projectͰੜ੒Ͱ͖Δ • ॻ͔ͳ͖Ό͍͚ͳ͍ͷ͸࠷௿ݶIndex.phpͷΈʢޙड़ʣ

Slide 61

Slide 61 text

• ͡Ό͋ɺͱΓ͋͑ͣىಈͯ͠Έ͔ͬ

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

• ʮΊͬͪΌΧϥϑϧͳதࠃޠ…͚ͩͲ·͋ɺͳΜͱͳ͘Θ͔ Δɺ͍͚Δ͍͚Δʂʯ • ͨͱ͑͹… • --d -> ͚ͭΔͱσʔϞϯԽ • --workerNum-1 -> ϫʔΧʔ਺ • (--workerNum 1 Ͱ͸ͳ͍…) • --user-nobody -> nobodyϢʔβʔͰ࣮ߦ • (--user nobodyͰ͸ͳ͍…ʣ

Slide 65

Slide 65 text

ϓϩηεɾϫʔΧʔϚωʔδϟʔ • ຊ౰ʹ࢖͏ͳΒɺ͘͢͝ॏཁͳ΍ͭ • start,stop,reload,daemonize • ϫʔΧʔ਺ௐ੔ • respawn • UIDɺGUIDઃఆ

Slide 66

Slide 66 text

ʮPHPͳͷʹ͔ͬ͠Γ͠ͱΔʯ • ຊ౰ʹʢׂͱʣͪΌΜͱϓϩηεΛϚωʔδ͢Δɻ • ͪΌΜͱىಈ͢Δ͠ɺͪΌΜͱࡴͤΔɻ • γάφϧͰϦϩʔυͱ͔΋Ͱ͖Δʢָʂʂʣ • gracefull restart΋ͪΌΜͱ͢ΔʢΒ͍͠ɺ͋Μ·Γݫີʹݕূ ͯ͠ͳ͍ʣ • ʮPHPͰ͜ΜͳʹͪΌΜͱ࡞ͬͯ͋ΔϓϩηεϚωʔδϟɺॳ Ίͯݟ͔ͨ΋…ʯ

Slide 67

Slide 67 text

(଍ճΓ͕ଞͷ͜Εܥͷ΍ͭΑΓ͔ͬ͠Γ͍ͯ͠Δ͜ͱΛ঺հ͢Δ ͨΊʹ࠷ॳʹ঺հ͠·ͨ͠ɻ ͕ɺ·͋ɺօ͞ΜPHPͰσʔϞϯ࡞ͬͨΓ͠ͳ͍Ͱ͔͢Ͷ…͸͸ ͸…)

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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; } } }

Slide 70

Slide 70 text

ָʂʂʂ

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

• REST͸ͮ͠Β͍ • HTTP Method੾Γ෼͚͸ແ͍ɺURL಺ύϥϝλ΋ύʔε͠ͳ͍ • ʮผʹRESTͳͯ͘΋ࢮͳͳ͍͠…ʯʮࢮ͵ਓ΋͍Δʯ • ͦ͏͍͏ਓ͸΋͏ҰͭͷΧελϜϧʔλΛ͔͓ͭ͏ • γϯϓϧͩ͠ɺઃఆ͍Βͳ͍͠ɺURL͕៉ྷͰͳͯ͘΋͍͍͠… ͱ͍͏͜ͱͰݸਓతʹ͸ͪ͜Β͕… • ʢGET ?id=1͘Β͍ͳΒڐͤΔ͠ɺෳࡶͳύϥϝλ͸JSONΛ POST͢Ε͹͍͍ͱࢥ͍ͬͯΔݹ͍ਓؒͷൃ૝Ͱ͢ʣ

Slide 76

Slide 76 text

ʢඪ४ͰͷʣσΟεύον͞ΕΔΫϥεͷߏ଄ • ʢϧʔλʔ͔ΒσΟεύον͞ΕΔίϯτϩʔϥ͜ͱΞΫγϣ ϯ…ʣ • EasySwoole\Core\Http\AbstractInterface\ControllerΛΠ ϯϓϦ͢Δ • PSR-7ޓ׵ͷrequest/response͕ϓϩύςΟܦ༝Ͱ࢖͑Δɻ

Slide 77

Slide 77 text

response()->write('Hello easySwoole!'); } }

Slide 78

Slide 78 text

ʮPSR-7͠Βͳ͍ਓʹ΋ૣΘ͔Γʂʯ • echoͱ͔͠ͳ͍(ͯ͠΋ϒϥ΢βʹ͍͔ͣɺίϯιʔϧʹग़Δ) • $this->response()->write('Hello easySwoole!');ͱ͔͢ Δ • $_POST['name']ͱ͔͠ͳ͍ • $this->request()->getParsedBody()['name'];ͱ͔͢Δ • (ར఺͸ผ్άάͬͯ͘Ε)

Slide 79

Slide 79 text

͍͔ͭ͘ͷಛผͳϝιου͕͋Γ·͢ • actionNotFound() -> 404ͷͨΊ • onException() -> ΤϥʔϋϯυϦϯάͷͨΊ • ʢ͜ͷ͋ͨΓ͸ __hook()ͷ࣮૷ΛΈΔͱཧղͰ͖·͢ʣ

Slide 80

Slide 80 text

ΞΫγϣϯͷલͱޙͷॲཧࠩ͠ࠐΈ • onRequest() • ϦΫΤετલʹॲཧ • ϨεϙϯεΛฦͯ͠͠·͏ࣄ΋Ͱ͖ΔʢೝՄͳͲʣ • afterAction() • Action͕ਖ਼ৗʹऴ͑ͨΒॲཧ • ͲͪΒ΋࣍ʹ஋Λ౉͚ͨ͠Ε͹ɺ$this͕࢖͑·͢

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

΋͏Ұͭͷख๏ɺΧελϜϧʔλʔ • 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(); });

Slide 83

Slide 83 text

ίϯτϩʔϥ·ͱΊ • ݟͯͷ௨Γɺී௨ͷʢPSR-7ରԠFWͷʣPHPͱେࠩͳ͍

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

• ஫ҙਂ͘࢖͏ͱ࠷ߴʹؾ෼͕͍͍ʢؾ෼ͳͷʁʣ • (PHP͸े෼ʹૣ͍ͷͰɺ࣮͸ۜͷ஄ؙͰ΋ͳ͍) • ҆શʹγϯάϧτϯʹ͢Δͷ͸ɺ͘͢͝ɺ೉͍͠ • ྫɿΫΤϦ࣮ߦॱং͕༧૝֎ʹͳΔɺϑΝΠϧग़ྗ่͕Ε ΔɺผͳॴͰΤϥʔͰյΕΔ • ීஈΤϥʔϋϯυϦϯάΛࡶʹߦ͍ͬͯΔਓ͸ࢮ͵ • ঢ়ଶΛ࣋ͨͳ͍ɺϓϦΩϟογϡͳͲॳظԽ͕ॏ͍͚ͩͱ͔ ͳΒൺֱత҆શ

Slide 93

Slide 93 text

• prefork+eventͷ࢓૊ΈͳͷͰɺ࡞Δ৔ॴʹΑͬͯ͸άϩʔόϧ ʹ͸ͳΒͳ͍ • ͳͷͰɺmainServerCreate΍frameInitializeͰ࡞ΔͳͲ͢Δ • ϦʔΫ͕ා͍ͷͰɺे෼ʹmax_requestΛͷ͹ͯ͠ɺࢠʢϫ ʔΧʔʣͰੜ੒ͨ͠΄͏͕҆શ…ɻ • ઃఆճ਺ॲཧ͢ΔͱϫʔΧʔ͸࠶ىಈ͢Δ • ·͋ɺ࣮ߦස౓Λ1/10000ʹͳΕ͹े෼

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

public static function mainServerCreate(/* ... */): void { // timerͰ1ඵຖʹΠϯλʔόϧ࣮ߦ $register->add($register::onWorkerStart, function ($server, $workerId) { if ($workerId == 0) { Timer::loop(1000, function () { echo 'TICK!'; }); } }); }

Slide 96

Slide 96 text

ඇಉظϦΫΤετʢΠϕϯτۦಈʣ • Πϕϯτۦಈͱ͍͑͹͜Εʢཁग़యʣ • ྫɿ֎෦APIʹϦΫΤετͯ݁͠ՌΛදࣔ͢Δ • ྫ̎ɿυϝΠϯΛνΣοΫ͍͕ͨ͠DNS໰͍߹Θͤ͸஗͍ • ී௨ͷphpͰ͸݁Ռऔಘ·Ͱී௨ʹϒϩοΩϯάʢϓϩηεઐ ༗ʣͯ͠͠·͏ • ΠϕϯτۦಈͰ࡞Ε͹ϓϩηεઐ༗͠ͳ͍ • easyswoole͸ෳ਺ϓϩηεಈ࡞Ͱ͖Δ͔Βଈഁ໓తʹ͸ͳΒ

Slide 97

Slide 97 text

• ʮϝΞυݕূͰDNS໰͍߹Θ͍ͤͨ͠ʯ • ඇಉظͰਖ਼Ҿ͖͢Δʹ͸swooleͷco::gethostbyname͕͋Δ • ίϧʔνϯͰͭ͘ΒΕͨ΋ͷ • ແବͳ࣌ؒΛɺϦΫΤετؒͰ༥௨ • co::gethostbyname͸ύϥϨϧऔಘͰ͸ͳ͍ͷͰʮͦͷϦΫΤ ετ͸଎͘͸ͳΒͳ͍ʯ • ͦͷ͔ΘΓɺଞͷϓϩηεͷअຐΛ͠ͳ͘ͳΔ

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

• lookupNormalͷํʹෛՙΛ͔͚ͭͭɺhelloʹෛՙΛ͔͚Δࢼݧ • lookupNormal: 1.65 rps • hello: 1.69 rps • lookup͕ϒϩοΫͯ͠ɺhello͕΄΅ಉ͡଎౓ʹ

Slide 102

Slide 102 text

• lookupCoʹෛՙΛ͔͚ͭͭɺhelloʹෛՙΛ͔͚Δࢼݧ • lookupCo: 1.64 rps / / ͜͜͸େ͖͘มΘΒͳ͍ • hello: 147.36 rps / / 100ഒʹ • lookup͕໰͍߹ΘͤதʹଞͷॲཧΛਐΊΔͷͰϒϩοΫͮ͠ Β͘ɺhello͕଎͘ͳͬͨ

Slide 103

Slide 103 text

• ΠϕϯτۦಈʹϒϩοΩϯάॲཧ͸ఢ • ϚϧνϓϩηεͳΒɺ͜ͷ੍ݶ͸ଟগ؇࿨͞ΕΔ • ʮϒϩοΫ͢Δಉ࣌઀ଓ਺ʻϫʔΧʔ਺ʯ ͳΒ଎͍ • Swoole͸ͦͷػೳ͕ͪΌΜͱἧͬͯΔɺ࠷ߴʂ • ʢͱ͸͍͑ɺ҆қʹϫʔΧʔ਺૿΍͢ͷ΋ඍົʣ

Slide 104

Slide 104 text

• ղܾʂͱ͍͍͍ͨͷ͕ͩ…ɻ

Slide 105

Slide 105 text

͜͜Ͱ࣮༻্ͷ໰୊ • swooleʹ͸ίϧʔνϯͷco::gethostbyname͕͋Δͷ͕ͩ… • λΠϓΛࢦఆͰ͖ͳ͍… • https:/ /wiki.swoole.com/wiki/page/790.html • ϝʔϧϑΥʔϜҊ݅Ͱ͸ɺMX΋Ҿ͚ͳ͍ͱ͜·Δ • ʮ͡Ό͋DNS protoΛswooleͰ࣮૷͠Α͏ʂʯ…ͱ͸ͳΒͳ͍ ʢͰ͖Δ͕ɺ໘౗͗͘͢͞Δʣ

Slide 106

Slide 106 text

• HTTPSͰDNSͻ͚͹͍͍ΑͶɺͱ͍͏͜ͱͰ • DNS-over-HTTPS • https:/ /developers.google.com/speed/public-dns/docs/dns-over- https • swoole ʹ͸ Coroutineͷhttp client͕͋Δ

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

$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"; }

Slide 109

Slide 109 text

• ͱ͍͏͜ͱͰɺແࣄAϨίʔυͱMXϨίʔυΛҾ͚ΔΑ͏ʹɺ • ΋ͪΖΜεοΩϦCoroutineͰͶʂ

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

λεΫͷొ࿥ class Index extends Controller { function index() // ͱ͋Δίϯτϩʔϥʔ { // ... $this->response()->write('Ϩεϙϯε'); $some = 'data'; TaskManager::async(function () use ($some) { $log = new Log; $log->set($some); // ॏ͍ॲཧͳͲ }); // ผʹλεΫొ࿥͸࠷ޙͰͳͯ͘΋Α͍ } }

Slide 117

Slide 117 text

• TaskManager::async()͸δϣϒొ࿥͔͠͠ͳ͍ͷͰ • ʢλεΫͱͳΔΫϩʔδϟΛγϦΞϥΠζͯ͠อଘͨ͠Βʣ • ࣍ͷߦ΁͢͢Ή • ʢͭ·ΓɺϦΫΤετ͕ऴΘΔʣ

Slide 118

Slide 118 text

λεΫͷॲཧ • ޙͰtaskΛϫʔΧʔϓϩηε͕र࣮ͬͯߦ • ಛʹઃఆ͸ແ͍ʂ؆୯ͩʂ • ʮ͑ͬɺผϓϩηεʹΫϩʔδϟΛ౤͛Δͷʁʯʮ͸͍ʯ • SuperClosureͰγϦΞϥΠζͯ͠౤͍͛ͯΔ • ʢͳͷͰɺෳࡶͳσʔλʢFDΛ΋ͬͯΔͱ͔ʣΛΫϩʔδϟ ʹ͍ΕΔͱͩΊͰ͢ʣ

Slide 119

Slide 119 text

easySwoole\Cache • ʮ໰͍߹Θͤ͢ΔͳΒΩϟογϡ͸ඞਢͰ͠ΐʯʮͤ΍ͳʯ • easyswooleͰ࢖͑Δ؆୯ͳKVS • ϩʔΧϧFSɺredisɺmemcachedͳͲΞμϓλ • ʢʮੌ͘ྑ͍΋ͷʯͰ͸ͳ͍ɺಛʹswooleಠಛͷػೳ͸͔ͭͬ ͯͳ͍͠ʣ • ·͋ɺ؆୯ศརͳͷͰɻ

Slide 120

Slide 120 text

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) ]));

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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 }

Slide 123

Slide 123 text

தؒ·ͱΊ • TaskͰॏ͍ॲཧ͸શ෦ޙΖʹ౤͛ͯɺϨεϙϯεΛฦͤΔ • લड़ͷඇಉظϦΫΤετΛ͔ͭ͏ʹͯ͠΋΍ͬͺΓΩϟογϡ ͸͍͔ͨ͠ΒCache͸ศར • easyswooleʹ͸Task΍CacheͳͲɺ΄΅ઃఆͳ͘γϯϓϧʹύο ͱ͔ͭ͑Δػೳ͕͋ͬͯศར

Slide 124

Slide 124 text

easy-swoole঺հ·ͱΊ • ϝʔϧϑΥʔϜΛߴ଎Խ͢Δʹ͋ͨͬͯ • ϑΥʔϜΛදࣔ -> ूܭσʔλΛද͓ࣔͯ͘͠ • ֬ೝը໘ -> ϝΞυଘࡏ֬ೝͷDNS໰͍߹Θͤ͸ඇಉظͰ • อଘ -> λεΫԽͯ͠ͱʹ͔͘ฦ͢ • ͱ͍ͬͨࣄ͕easy swooleͰϥΫϥΫͰ͖Δ

Slide 125

Slide 125 text

• ී௨ͷPHPͰॻ͘ͱɺ΍ΕผϓϩηεʹδϣϒΩϡʔͩɺ΍Ε redisͩɺͳͲͱෳࡶԽ͕ͪ͠ɻ • ϑϧελοΫFWͳΒେମೖͬͯΔ͚Ͳɺ๩͍࣌͠ʹyak shaving ͕ḿΔࣄଶ΋͠͹͠͹ • ʢ·͋ɺԿͰ΋ॳΊͯ࢖͏࣌͸৭ʑמΔඞཁ͸͋ΔͰ͠ΐ͏ ͕…ʣ • ͦ΋ͦ΋୯ͳΔςϯϙϥϦͳԠืϑΥʔϜʹͰ͔͍ϑϨʔϜ ϫʔΫ࢖͍ͨ͘ͳ͍

Slide 126

Slide 126 text

• ͦ͜Ͱɺeasy swooleͳΒɺΑ͋͘Δʮ͋Ε͕΄͍͠ʂʯʹγϯ ϓϧͳͨ͑ͯ͘͜ΕΔͧʂ • ʢΠϕϯτۦಈʹ׳Ε͍ͯͳ͍ਓͰ΋ɺeasyswoole͸γϯϓ ϧͩͱࢥ͏…ଟ෼ʣ • swoole·ͰؚΊͯɺ͞Βʹ৭ʑػೳ͕͋Δͧʂࠓճ঺հ͠ͳ͔ ͚ͬͨͲɺDBͷ઀ଓpoolͱ͔΋͋Δͧʂ • ͱ͍͏͜ͱͰswoole͸ͪΐͬͱ…ͱ͓΋͍ͬͯͨօ͞Μɺͥͻ easy swooleͰপʹೖͬͯΈ·͠ΐ͏ʂ

Slide 127

Slide 127 text

͜͜·Ͱ͘Ε͹ͱΓ͋͑ͣ੒ޭʢʁʣ

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

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

Slide 130

Slide 130 text

$ 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

Slide 131

Slide 131 text

͍͢͝ɺ୹ظؒʹ̎̑΋্͕͍ͬͯ Δʂʂ • ʢ୹ظؒͱ͸ʁʁʁʣ • Cake΍Slim,Laravelͱͷ૬ରײΛָ͓͠Έ͍ͩ͘͞ɻ

Slide 132

Slide 132 text

GitHubͰ΋εΰΠଟ͍

Slide 133

Slide 133 text

packagist ΋ଟ͍

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

awesome-swoole ΋૿͑ͨʢʁʣ • https:/ /github.com/swooletw/awesome-swoole • https:/ /github.com/yannsun/awesome-swoole • https:/ /github.com/swoole/swoole-src/wiki/Awesome • ʢଟ͚Ε͹ྑ͍ͱ͍͏΋ͷͰ΋ͳ͍͕ʣ

Slide 136

Slide 136 text

• laravelͱ͔ɺ֤छwafͷόΠϯσΟϯάʢ΍ྫʣ΋૿͑ͨ • Ͱ΋·͋ɺϑϧελοΫͷFWΛSwooleͰಈ͔͢ҙຯ͸…ɻ • Promiseͱ͔mqttͱ͔ftpserverʢʁʁʁʣͱ͔࣮༻తͳपลϥΠ ϒϥϦ΋

Slide 137

Slide 137 text

࣍ͷόʔδϣϯ͸̐ʂ

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

• Tencentɺͬ͞͞ͱʢུʣͱ͔ผͷݴޠʹ͍͔ͣɺҊ֎ͪΌΜͱ ͚͍͍ͭͮͯͯ͢͝ɻ • PHPݴޠʹϋϚ͍ͬͯΔʢʣձࣾ͸ࠜੑͰPHPΛ೧͡ۂ͛Δ͔ Β͍͢͝ɻ

Slide 143

Slide 143 text

͸͍

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

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

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

• యܕతͳΠϕϯτۦಈ • ࣮ߦ͞ΕΔPHPίʔυ͸͔̍ͭ͠ͳ͍ • ྫ reactPHPͳͲ • ϝΠϯϧʔϓ͕ҰͭͳͷͰɺϝΠϯϧʔϓͰͳʹ͔٧·Δॲཧ ͕͋Ε͹શ෦ࢭ·Δ • ຊ౰ʹϊϯϒϩοΩϯάʹॻ͚Ε͹ޮ཰తʹΈ͑Δ͕ɺίΞ਺ ͕׆͔ͮ͠Β͍

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

• ΠϕϯτͱϓϩηεΛ͋Θͤͨ΋ͷɺswoole͸͜ΕɻreactPHP ΛϚϧνϓϩηεʹ͢Δ΋ͷ΋͋Δ • ʢجຊతʹ͸ʣࣄલʹPHPίʔυΛ࣮ߦ͠ίʔϧόοΫʢϝΠ ϯϧʔϓʣΛ࡞ΔͷͰɺPHPίʔυ͸Ұͭ • ϝΠϯϓϩηε͕ड͚ͯࢠʹΘͨ͢౳࣮૷͕΍΍͍͜͠ • ଟগϒϩοΩϯά͕͋ͬͯ΋ɺଞͷϓϩηε͕͕Μ͹ΕΔ༨஍ ͕࢒Δ • ཧ࿦্ɺreload͕Ͱ͖Δ • ເ͕͋Δʂʢʁʣ

Slide 151

Slide 151 text

͸͍

Slide 152

Slide 152 text

ʮ…஗͍Μ͚ͩͲʁʯ • ·ͣɺଟॏԽͰ͖ͳ͖ΌͦΕͳΓͰ͢ • 100rpsҎ্͕ඞཁͳ͍ͳΒɺޡ͔ࠩͳ… • MacͰ΋ಈ͕͘ɺ͍ͬ͢͝஗͍ • ϕϯν͢ΔͳΒLinuxͰɻ • ʢಉ͡MacͷVM্Ͱ͏͔ͯ͝͠ɺ5ʙ50ഒ଎͍ʣ

Slide 153

Slide 153 text

͸͍

Slide 154

Slide 154 text

Πϯετʔϧํ๏͸ʁ • 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

Slide 155

Slide 155 text

• ґଘ͕ͳ͍ͷͰɺ͢ΜͳΓ͸͍Δͱࢥ͍·͢ • peclͰ΋ೖΕΒΕΔΒ͍͠ʢ࠷৽όʔδϣϯΛ͔Μ͕͑Δͱɺ git͕Α͍͔ͱ) • easy-swoole͸create-project͕଎͍ $ composer create-project easyswoole/app easyswoole

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

։ൃαϙʔτ • 䎔ဋ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Λ͍ΕͨΒೖΔʣ

Slide 158

Slide 158 text

• ಣࡆᄛ(Hotreload) • ػೳͱͯ͋͠ΔΘ͚Ͱ͸ͳ͍͕ɺfswatchΛ͔ͭͬͨྫΛهࡌ • https:/ /www.easyswoole.com/Manual/2.x/Cn/_book/Introduction/ server.html

Slide 159

Slide 159 text

͸͍

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

[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

Slide 162

Slide 162 text

reloadपΓʹ͍ͭͯ • ϝΠϯϓϩηεʹSIGUSR1ΛૹΔͱϦϩʔυ • opcache΋ΫϦΞ͞ΕΔ • easyswooleͷreload͸ͳΜ͔͓͔͍͠ࣄ͕͋ΔͷͰɺʮࢲ͸ʯϝ ΠϯϓϩηεʹγάφϧΛૹ͍ͬͯΔ • ΋͠ࢠϓϩηεͷPID͕มΘΒͳ͚Ε͹(respawnͯ͠ͳ͚Ε ͹)͓͔͍͠ • ʮίʔυΛಡ΋͏ʯ

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

nginx ͱͷ࿈ܞ • nginxͱswooleΛಉډͤ͞ΔͳΒunix socket͕Φεεϝ • ͨͩɺߴෛՙαʔϏεͳΒswooleͱnginxΛN:1ʹ͢Δ͜ͱ΋͋ ΔͩΖ͏ • ͜͏͍͏࣌ɺhttpdʹͳΔͷ͸ָͰ͍͍Ͱ͢Ͷʂ • ʢ࣍εϥΠυʹઃఆαϯϓϧʣ

Slide 165

Slide 165 text

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:; } }

Slide 166

Slide 166 text

unix socket ࣌ͷConfig.php • HOSTʹύεΛࢦఆ͢Δ • PORT͸ແࢹ͞ΕΔ • sockϑΝΠϧͷPermission͸ࢦఆෆՄɺnginxͱҧ͏UIDͰಈ͔͢ ͳΒsockϑΝΠϧΛchmod͢ΔͱΑ͍(swooleͷαϯϓϧίʔυ Ͱ͸ͦ͏ͳ͍ͬͯͨ) • Bad gatewayͰɺchmod 777 /tmp/swoole.sock࣏ͯ͠Ε͹͜ ΕɺϩάΈΕ͹Θ͔Δɻ

Slide 167

Slide 167 text

return [ // ... 'MAIN_SERVER'=>[ 'HOST'=>'/tmp/swoole.sock', 'PORT'=>0, 'SERVER_TYPE'=>\EasySwoole\Core\Swoole\ServerManager::TYPE_WEB_SERVER, 'SOCK_TYPE'=>SWOOLE_UNIX_STREAM, // ...

Slide 168

Slide 168 text

EOL