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

GAE/Go の勘どころ

GAE/Go の勘どころ

golang.tokyo #5

Osamu TONOMORI

April 27, 2017
Tweet

More Decks by Osamu TONOMORI

Other Decks in Programming

Transcript

  1. The golang.tokyo logo was made by Takuya Ueda. The Go

    gopher was designed by Renee French.
  2. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 3
  3. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 4
  4. ࣗݾ঺հ • ओ৿ ཧ - Osamu TONOMORI • @osamingo •

    גࣜձࣾι΢κ΢ • 2016/08 ~ • Software engineer • Go ྺ • 2014/10 ~ • GAE/Go ͸ɺ2016/08 ~ 8
  5. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 12
  6. GAE ͱ͸ • Google App Engine • Google ͕΍͍ͬͯΔ PaaS

    ɻ • 2008 ೥͙Β͍͔Β͋ΔɻʢGCPͰ͸࠷ݹࢀ૊ʣ • Scaling Λௐ੔͢Ε͹ɺແྉͰ࢖͑Δɻ 14
  7. GAE ͱ͸ • Google App Engine • Google ͕΍͍ͬͯΔ PaaS

    ɻ • 2008 ೥͙Β͍͔Β͋ΔɻʢGCPͰ͸࠷ݹࢀ૊ʣ • Scaling Λௐ੔͢Ε͹ɺແྉͰ࢖͑Δɻ • Heroku ͱಉ͡ײ͡Ͱ৮Δͱ (ʋ´ω`) ͳײ͡ʹͳΔɻ • ಠࣗͷϧʔϧ΍ɺ֮͑Δ͜ͱ͕ଟ͍ɻ 15
  8. Standard and Flexible Environment • Standard Environment (SE) • GAE

    ͱ͍ͬͯࢦ͢ͷ͸ɺ͍͍ͩͨͬͪ͜ɻ • ݴޠ੍໿΍ɺಠࣗͷϧʔϧ͸ଟ͍͕͓खܰʹࢼͤΔɻ • Go, Python, Java, PHP • Flexible Environment (FE) • Cloud Next 2017 Ͱɺ GA ʹͳͬͨɻ • Docker ͳͷͰɺಛʹ੍໿͸ແ͍ɻ 16
  9. Flexible Environment Λ࢖͏৔໘ • ωοτϫʔΫىҼͷཁٻ͕͋Δ৔߹ • ݻఆ IP Ͱ֎෦઀ଓ͠ͳ͍ͱ͍͚ͳ͍ɻ •

    ಺෦ެ։ݶఆͷαʔϏε΁ͷΞΫηεɻ • Ұ࣌తͳσΟεΫॻ͖ࠐΈ͕ඞཁͳ৔߹ • ϑΝΠϧΛ࡞੒͍ͨ͠৔߹ͳͲɻ (tmpfs) 17
  10. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… 18
  11. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… • context.Context Λ࣋ͪӡΜͰߦ͘ɻ • context Λ࢖ͬͯ DB ΞΫηε౳Λߦ͏ɻ • ٯʹ͍͏ͱɺcontext ͕ແ͍ͱ΄΅Կ΋ग़དྷͳ͍ɻ 19
  12. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… • context.Context Λ࣋ͪӡΜͰߦ͘ɻ • context Λ࢖ͬͯ DB ΞΫηε౳Λߦ͏ɻ • ٯʹ͍͏ͱɺcontext ͕ແ͍ͱ΄΅Կ΋ग़དྷͳ͍ɻ 20
  13. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 25
  14. DB ͷબ୒ʹ͍ͭͯ • Cloud Datastore • NoSQL (Based BigTable) •

    GAE ͷ৔߹ɺجຊతʹ Datastore Λར༻͢Δɻ • มଶྗઃܭྗ͕ࢼ͞ΕΔɻ 28
  15. DB ͷબ୒ʹ͍ͭͯ • Cloud Datastore • NoSQL (Based BigTable) •

    GAE ͷ৔߹ɺجຊతʹ Datastore Λར༻͢Δɻ • มଶྗઃܭྗ͕ࢼ͞ΕΔɻ • Cloud SQL • Managed MySQL/PostgreSQL(β) • ౦ژϦʔδϣϯͷ Latency ͕Ϋι໰୊΋ղܾࡁΈɻ 29
  16. Cloud Datastore ͷ৔߹ • ެࣜͷ datastore ύοέʔδΛ௚Ͱ͸࢖Θͳ͍ɻ • ඞͣ wrap

    ͯ͠࢖ͬͨํ͕Α͍ɻ • interface, validation ͳͲͪΌΜͱ΍Γ·͠ΐ͏ɻ • goon ͱ͔΋͋ΔͷͰɺࢀߟʹ͢ΔͱΑ͍ɻ 30
  17. Cloud Datastore ͷ৔߹ • ެࣜͷ datastore ύοέʔδΛ௚Ͱ͸࢖Θͳ͍ɻ • ඞͣ wrap

    ͯ͠࢖ͬͨํ͕Α͍ɻ • interface, validation ͳͲͪΌΜͱ΍Γ·͠ΐ͏ɻ • goon ͱ͔΋͋ΔͷͰɺࢀߟʹ͢ΔͱΑ͍ɻ • ͨͩɺઃܭ͕㙽ʹ֯೉͍͠ɻ 31
  18. Vendoring ʹ͍ͭͯ • GOPATH ͕ɺϓϩδΣΫτϕʔεʹͳΔɻ • vendor σΟϨΫτϦͱ͔ɺ࢖͑ͳ͍ɻ • dep,

    glide, gvt ͳͲݢฒΈ࢖͑·ͤΜɻ • ےྗͰΧόʔʂʂ • ϓϩδΣΫτϕʔεͷґଘ؅ཧ 45
  19. gb ʹΑΔvendoring • gb vendor ίϚϯυʹΑΔґଘ؅ཧ • fetch, update, purge

    ౳ͻͱ௨ΓͰ͖Δɻ • ͨͩɺinit ͚ͩαϒίϚϯυ͕ແ͍ͷͰɺےྗ 48
  20. gb ʹΑΔvendoring • gb vonder ίϚϯυʹΑΔґଘ؅ཧ • fetch, update, purge

    ౳ͻͱ௨ΓͰ͖Δɻ • ͨͩɺinit ͚ͩαϒίϚϯυ͕ແ͍ͷͰɺےྗ 49 HPMJTUGb\\SBOHF*NQPSUT^^\\^^\\FOE^^\\SBOHF5FTU*NQPSUT^^\\^^ \\FOE^^ 71"5) cUSaOcTPSUcVOJRcHSFQ& a DPNcPSHc JO  cYBSHTO*\^TIDHCWFOEPSGFUDI\^
  21. gb ʹΑΔ GOPATH ؅ཧ • gb gae ίϚϯυʹΑΔ GOPATH ͷ؅ཧ

    • test, serve, deploy ͷ GOPATH Λղܾͯ͘͠ΕΔɻ • ެࣜఏڙͰ͸ແ͍ͷͰɺผ్Πϯετʔϧ͢Δɻ • go get -u github.com/PalmStoneGames/gb-gae 50
  22. Configuration ʹ͍ͭͯ • The Twelve-Factor App - https://12factor.net/ • The

    twelve-factor app stores config in environment variables. 52
  23. Configuration ʹ͍ͭͯ • The Twelve-Factor App - https://12factor.net/ • The

    twelve-factor app stores config in environment variables. • GAE ΋ಉ͡ࢥ૝ͳͷͰɺ؀ڥม਺ʹೖΕ·͠ΐ͏ɻ 53
  24. Configuration ʹ͍ͭͯ • Test ͷͱ͖͸ɺͲ͏͢Δͷ͔ʁ • ં֯ Go ࢖ͬͯΔΜͩ͠ɺ direnv

    ࢖͓͏ɻ • ./test/.envrc ʹςετ༻ͷ؀ڥม਺Λઃஔ͢Δɻ 55 EJSFOWFYFDUFTUHCHBFUFTUTSDʜ
  25. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 56
  26. Monitoring ͬͯͲ͏ͳͷʁ • Stackdriver ͕ɺͻͱ௨Γʹͳͬͯ͘ΕΔɻ • http status code, datastore,

    taskqueue etc. • Error log ؂ࢹ΋ɺStackdriver Logging ͱ࿈ܞͰ͖Δɻ • Slack ࿈ܞ΋ϥΫϥΫ
  27. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 63