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

phpアプリケーションをGo言語へ

 phpアプリケーションをGo言語へ

17d4ef53b432ebf7c566fd6a11345570?s=128

yuuki takezawa

November 03, 2016
Tweet

Transcript

  1. phpΞϓϦέʔγϣϯΛ Goݴޠ΁ go func() { messages <- "php" }() yuuki

    takezawa phpcon 2016
  2. GoݴޠΛ࠾༻͢Δ͜ͱʹͳͬͨഎܠ

  3. ڊେͳαʔϏεͷݶք

  4. ڊେαʔϏεʹΑΔ໰୊ • 1Օॴͷमਖ਼͕க໋తͳ໰୊ΛҾ͖ى͜͢έʔε • ෳࡶԽ͢Δ࢓༷ʹରԠ͢Δҝͷ࣮૷͕ύϑΥʔϚϯε ʹӨڹΛ༩͑࢝Ίͨ • ڊେʹͳΓ͗ͨ͢σʔλϕʔε • ୲౰֎ͷαʔϏε࢓༷͕Θ͔Βͳ͚Ε͹मਖ਼Ͱ͖ͳ͍

    ঢ়ଶ • API͕͘͢ͳ͘ɺଟ͕͘σʔλϕʔεʹґଘ • ϏδωεͷมԽʹ௥ैͰ͖ͳ͍ঢ়ଶ • େྔσʔλͷόονॲཧ
  5. ໰୊ʹཱͪ޲͔͏ • σʔλϕʔεࢀরͷ੾Γ཭͠ • APIͷ੔උ json, xml, etc… • ΫϥΠΞϯτͷॲཧΛ؆ུԽ͢ΔҝʹɺHATEOAS΁


    (औΓ૊Έத) • σʔλϕʔεࣗମͷ෼ׂ
 (ςʔϒϧͷ࠶ઃܭɺ໨తผʹ෼ׂɺNoSQLซ༻) • ԣஅతʹؔΘΔαʔϏεͱͷ࿈ܞ
 (message queue, ฒྻॲཧ)
  6. σʔλϕʔεࢀরͷ੾Γ཭͠ • ιʔείʔυͷ͍ͨΔͱ͜Ζʹ͋ΔσʔλϕʔεࢀরΛ
 ந৅ԽɺDependency Injection΁ • σʔλϕʔε΁઀ଓΛߦ͍ͬͯΔίʔυ͔Βɺ
 ֎తཁҼΛ੾Γ཭͠ • ΞϓϦέʔγϣϯߏ଄Λ·ͣ͸ϨΠϠʔԽ

    • ঃʑʹAPIࢀর΁ͱ੾Γସ͍͑ͯ͘ • PHPΞϓϦέʔγϣϯΛૄ݁߹ʹ
  7. class PointBalance { /** @var PointRepositoryInterface */ protected $repository; /**

    * @param PointRepositoryInterface $repository */ public function __construct(PointRepositoryInterface $repository) { $this->repository = $repository; } // snippet }
  8. Controller / Action Domain Infrastructure MySQL API Elasticsearch Aspect

  9. None
  10. APIͷ੔උ • ଞαʔϏεͷ஌ࣝΛෆཁʹ
 (URLͷߏங΍ɺͲΜͳAPIͰͲΜͳ͜ͱ͕Ͱ͖Δͷʁ) • ϦΫΤετ਺ͷ࡟ݮ • ΤϯυϙΠϯτʹಈࢺ͸࢖ΘͣɺϝιουͰදݱ͢Δ • ڞ௨Ͱར༻͢Δ΋ͷ͸ίϯϙʔωϯτԽ

    • ݹ͍APIͳͲɺଟ͘ͷαʔϏε͕ґଘ͍ͯ͠Δ΋ͷ͸ͳ ͔ͳ͔೉͍͠ • ύϑΥʔϚϯε͕ϘτϧωοΫͳ΋ͷ͸replace
  11. { "id": 1598, "user_id": 123, "title": "ίϝϯτʹͨ͠Α", "content": "ϝʔϧ͕དྷͳ͍ΑͶ", "tags":

    [ { "id": 5098, "name": "ඒന" } ], "created_at": "2016-10-12 17:31:50" }
  12. { "id": 3746, "user_id": 123, "title": "ςετ", "content": "aaa", "tags":

    [ { "id": 6081, "name": "ΞϩϚΦΠϧ", "_links": { "self": { "href": "http://example.com/tags/6081", "method": "GET" } } } ], "created_at": "2016-10-14 16:57:37", "_links": { "self": { "href": "http://example.com/3746", "method": "GET" } } }
  13. navi menu content content

  14. ϑϩϯτͰར༻͞ΕΔAPI • ϑϩϯτʹग़ྗ͞ΕΔ΋ͷ͚ͩͰ͸ͳ͘ɺ
 ͦͷޙΖʹ߇͑Δ΋ͷ • Ϣʔβʔొ࿥৘ใ • ϝʔϧૹ৴ • ͳʹ͔͠Βͷߦಈʹ൐͏Ճࢉݮࢉ

    • ඇಉظɺϦΞϧλΠϜ • τϥϯβΫγϣϯεΫϦϓτ • ฒྻϦΫΤετʹ଱͑͏Δ΋ͷ • ϑϩϯτ͔Β੾Γ཭͢͜ͱͰదࡐదॴͳݴޠ࠾༻
  15. େྔͷϦΫΤετΛޮ཰ྑ͘ࡹ͖͍ͨ

  16. Nginx + php-fpm Response time: 0.01 secs Transaction rate: 384.48

    trans/sec
  17. େ͖ͳ͸໰୊ͳ͠ • ΞΫηε͕ଟ͘ͳΕ͹ɺ୆਺Λ૿΍͢ • opcache΋ಋೖ͠(PHP7)ɺߴ଎ͳϨεϙϯε
 • ࣮૷໘ͰͷόάΛ࠷খݶʹ৯͍ࢭΊ͍ͨ • PHP7ʹͳΓɺݫ֨Ͱ͸͋Δ͕ɺ
 ։ൃऀશһ͕ཧղ͍ͯ͠ΔΘ͚Ͱ͸ແ͍

    • ϦΞϧλΠϜॲཧΛͱʹ͔͘ૣ͘ • ඞཁͳ৘ใΛଋͶͯฦ٫͍ͨ͠
  18. γϯϓϧɺฒߦ͔ͭߴ଎Ͱ੩తܕ෇͚ݴޠ

  19. Go • webαʔόͳͲ͕ෆཁͰ୯ମͰಈ࡞͢Δ • جຊ͸γϯάϧόΠφϦ഑෍ͷҝɺϦϦʔε͕༰қ • ίϯύΠϧ͕ૣ͘ɺΫϩείϯύΠϧ • ϝϞϦϦʔΫʹͳΓಘΔ΋ͷ͕ഉআ͞Ε͍ͯΔ •

    1αʔόͰෳ਺ىಈͤ͞Δ͜ͱ͕Ͱ͖Δ • ίϯύΠϧͱ࣮ߦ͕ಉ࣌ʹͰ͖ΔͨΊɺ
 LLݴޠ։ൃऀͰ΋؆୯ • Ϋϥε͕ແ͍
  20. // linux޲͚΍windows޲͚ͳͲ༷ʑͳOSʹରԠ $ GOOS=linux GOARCH=amd64 go build -tags=production app.go

  21. Struct • ΫϥεͰ͸ͳ͍ • ໊લͱܕͷू·Γ • ઌ಄͕େจࣈͷ΋ͷ͸public 
 ͦ͏Ͱ͸ͳ͍΋ͷ͸private •

    ίϯετϥΫλ͸ଘࡏͤͣɺܧঝ΋ͳ͍ • ߏ଄ମΛຒΊࠐΜͰར༻͢Δ
  22. type Item struct { Language } type Language struct {

    Name string } Item{Language{"PHP"}}
  23. func handler(w http.ResponseWriter, r *http.Request) { person := &Person{"yuuki", "takezawa"}

    fullName(person) } func fullName(name UserName) { fmt.Println(name.Name()) } type Person struct { FirstName string LastName string } func (p *Person) Name() string { return p.FirstName + " " + p.LastName } type UserName interface { Name() string }
  24. goroutine • ಉ࣌ʹෳ਺ͷλεΫΛॲཧ • ॲཧΛ෼ׂ͍ͯ͘͠ • εϨουͷΑ͏ʹ1εϨου্Ͱෳ਺ಈ࡞ • ಄ʹgo Λ͚ͭΔ͚ͩ

    go func() • ଴͕ͪ࣌ؒൃੜͯ͠͠·͏Α͏ͳॲཧΛඇಉظͰ • ฒߦॲཧ
  25. func f(from string) { for i := 0; i <

    3; i++ { fmt.Println(from, ":", i) } } // ॱ൪͸อূ͞Ε·ͤΜ fun main() { go f("routine") go f("routine_one") go f(“routine_two") }
  26. ฒߦͱฒྻ • ฒߦ
 ࣮ߦঢ়ଶΛෳ਺อͯΔͳΒ • ฒྻ
 ෳ਺ͷಈ࡞Λಉ࣌ʹग़དྷΔ
 • GoݴޠͷฒߦੑΛө૾Խ͢Δ
 http://postd.cc/go_concurrency_visualize/

  27. channel • <- (໼ҹͷํ޲ʹྲྀΕΔ) • goroutine ؒͰ΍ΓऔΓΛߦ͏ • phpͰ͸೉͍͜͠ͱΛΧόʔ͢Δ •

    ΩϡʔΠϯάɺॲཧ଴ͪঢ়ଶͳͲΛ࡞Δ
  28. func read(r io.Reader) <-chan message { lines := make(chan message)

    go func() { defer close(lines) scan := bufio.NewScanner(r) for scan.Scan() { lines <- message(scan.Bytes()) } }() return lines }
  29. defer ͱ panic • defer: ϝιου͕ऴྃ͢Δࡍʹίʔϧ͞ΕΔ • σʔλϕʔεͳͲͷ੾அͳͲॲཧͷ஗Ԇʹ࢖͏ • panic͸runtime

    errorʹ͋ͨΓɺ
 ࣮ߦ͕೉͍͠৔߹ʹͷΈར༻͢Δ • panic࣌ʹ΋defer͕ίʔϧ͞ΕΔ • ΤϥʔॲཧΛ͢ΔͷͰ͋Ε͹errorΛ໭͢ • panic͔Β͔ͳΒͣrecover()͢Δ
  30. nginxΛ࢖ͬͨϩʔυόϥϯγϯά

  31. upstream go_http { server 127.0.0.1:8080; server 127.0.0.1:8081; keepalive 300; }

    server { listen 80; server_name go.app; charset utf-8; access_log off; error_log /dev/null crit; location / { proxy_pass http://go_http; proxy_http_version 1.1; proxy_set_header Connection ""; } }
  32. Nginx + go Response time: 0.00 secs Transaction rate: 2142.67

    trans/sec
  33. খ͞ͳͱ͜Ζ͔Βஔ͖׵͑Λ࢝Ίͨ • PHPͰ࣮ߦ͞Ε͍ͯͨେྔσʔλͷόονॲཧ
 ਺͔͔࣌ؒΓɺσʔλͷྔ͕૿͑Δ͜ͱͰߋʹ࣌ؒ
 ϝϞϦ͕৺഑(݅਺͕૿͑Δ͝ͱʹ૿Ճ) • Goʹஔ͖׵͑ɺϝϞϦ࢖༻ྔ͕ܹݮ
 ਺͕࣌ؒ୹ॖ
 σʔλϕʔεʹΑͬͯ͸͋·ΓมΘΒͳ͍͜ͱ΋͋ͬͨ •

    web։ൃʹదͨ͠ඪ४ϥΠϒϥϦ͕ଟ͘ɺ
 ίʔυ΋ಡΈ΍͘͢ɺ݁ՌҰ෦Ͱ࠾༻
  34. ϑϨʔϜϫʔΫ echo

  35. echo • ϚΠΫϩϑϨʔϜϫʔΫͰɺlumen΍zend expressive ͱಉ͡ײ֮Ͱ։ൃ͕Ͱ͖Δ • fasthttp͕ΦϓγϣϯͰ࢖͑Δ • grace͕͙͢ʹ࢖͑Δঢ়ଶͰɺͱʹ͔͘؆୯ •

    ඞཁͳ΋ͷ͸ࣗ෼Ͱ૊ΈࠐΉ • ܰྔͳAPIͰ͋Ε͹ൺֱత୹࣌ؒͰ࣮૷Մೳ
  36. ಋೖલʹ࣮ફ • ϓϥΠϕʔτͰ։ൃ͍ͯͨ͠ΞϓϦͷAPIʹ·ͣಋೖ • ҆ఆՔಇͱ࣮૷͕ૣ͍ • OAuthαʔό࣮૷ • ߴෛՙͰ΋ඇৗʹ҆ఆ •

    facebookgo/inject • ΦϒδΣΫτࢦ޲ϥΠΫʹ࣮૷͍͕ͯͨ͠ɺ
 Goͷ࡞๏ʹͷͬͨ΄͏͕γϯϓϧ • ϦϑϨΫγϣϯ͸ۃྗ࢖Θͳ͍
  37. Go goroutine API API Database queue

  38. վળ • ΑΓૣ͘ɺݎ࣮ͳΞϓϦέʔγϣϯ • ͦΕͧΕͷαʔϏεͷॲཧʹదͨ͠΋ͷΛಋೖ • ϦϦʔε࡞ۀͷ୯७Խ
 ίϯύΠϧͯ͠഑෍΁(CIͳͲ) • supervisord

    • ύϑΥʔϚϯεվળ গͣͭ͠
  39. ๊͍͑ͯΔ໰୊ • ؀ڥઃఆ஋ͳͲͷڞ༗
 viper? • όΠφϦʹ͢΂ͯͷσʔλΛ࣋ͨͤΔ͔
 go-bindata etc • ϑϥάͱ؀ڥ͝ͱͷઃఆ஋


    ιʔείʔυ಺ʹηΩϡΞͳ৘ใΛؚΊΒΕͳ͍ • ؆୯ͱ͸ݴͬͯ΋ͳ͔ͳ͔։ൃऀ͕૿͑ͳ͍
  40. Request Response web server + php

  41. $_SERVER net/http application server΁

  42. phpΛิ͍ɺͦΕΛαʔϏεͱͯ͠׆͔͢

  43. վળ͸࢝·ͬͨ͹͔Γ ՝୊ղܾΛPHP+αͰ (దࡐదॴ)

  44. PHPΞϓϦέʔγϣϯΛGoݴޠͱ࡞Δ