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

GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)

GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)

2016年4月23日の Go Conference 2016 Spring の発表資料です。

Tatsuya Tsuruoka

April 23, 2016
Tweet

More Decks by Tatsuya Tsuruoka

Other Decks in Programming

Transcript

  1. ࠓճ͓఻͍͑ͨ͜͠ͱ 3 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ

    Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘
  2. Why GAE? - GAE ͱ͸ 5 • GAE ͸ Web

    ΞϓϦͷͨΊͷ PaaS • ༷ʑͳ։ൃݴޠʹରԠ͓ͯ͠Γ Go ΋ͦͷ͏ͪͷͻͱͭ • ࣮ݱͰ͖Δ͜ͱ • ۃΊͯߴ͍εέʔϥϏϦςΟ • ਺req/sec ΋਺ेສreq/sec ΋ಉ͡Α͏ʹѻ͑Δ • Google ϨϕϧͷՄ༻ੑ • ো֐ʹΑͬͯαʔϏε͕ఀࢭ͢Δ͜ͱ͸ك • ϝϯςφϯεϑϦʔ • ࠷ॳʹΞʔΩςΫνϟΛܾΊΕ͹͋ͱ͸΋͏ख͕͔͔Βͳ͍
  3. Why GAE? - GAE ͱ͸ 6 • ࣮ࡍʹ GAE ͰͲΜͳ

    Web ΞϓϦΛ࡞͍͚ͬͯΔʁ • ࣄྫɿSnapchatɺIngress • ೔ผͷར༻Ϣʔβ͕1ԯਓΛ௒͑Δن໛ͷ࣮੷͕ɺ͢Ͱʹ͋Δ
 ※ͨͩ͠ݴޠ͸ Go Ͱ͸ͳ͍ͱࢥΘΕ·͢ • ϝϧΧϦࢠձࣾͷ৽نࣄۀʮϝϧΧϦ Ξοςʯ΋ GAE Λ࠷େݶʹར༻ • ΠϯϑϥΤϯδχΞ 0 ਓ • ϦϦʔε͔Βͷ໿1ϲ݄ؒʹٸܹͳΞΫηε૿ͳͲͰࠔͬͨ͜ͱ͸Ұ౓΋ͳ͠ • μ΢ϯλΠϜθϩɻܭըఀࢭͳ͠
 • → ݱ࣮తͳ Web ΞϓϦΛ࡞ΔͨΊͷػೳ͸උΘ͍ͬͯΔ
  4. Why GAE? 7 • ࠓճɺGAE ͷػೳΛ঺հ͢ΔͨΊʹ Web ΞϓϦΛߟ͑ͯΈΔ • ྫ͑͹ɺ͍·੝Γ্͕͍ͬͯΔ

    Bot ΞϓϦ
 4݄7೔ LINE BOT API ެ։ɺ4݄13೔ Facebook bots on Messenger ެ։ • → ͜ͷ Bot ΞϓϦΛ GAE ͰϋΠεέʔϥϒϧʹ࡞Δͱͨ͠Β...
  5. εέʔϥϏϦςΟ - ୯७ͳ Bot 9 • ػೳɿ • νϟοταʔόʢ͜ͷྫͰ͸LINEʣ͔Β ίʔϧόοΫΛड͚औΓɺ


    ϢʔβͷϝοηʔδͳͲΛऔಘͰ͖Δ • ϝοηʔδΛݩʹԿΒ͔ͷॲཧΛͯ͠ɺ
 Ϣʔβʹฦ৴Ͱ͖Δ
  6. εέʔϥϏϦςΟ - ୯७ͳ Bot 10 • ίʔϧόοΫΛϋϯυϦϯά͢Δ func init() {


    http.HandleFunc("/line/callback", receiveLine)
 }
 
 func receiveLine(w http.ResponseWriter, r *http.Request) {
 ...
 } • func init ͸ GAE/Go ΞϓϦͷཪଆ͔Βݺ͹ΕΔॏཁͳؔ਺ • func receiveLine ͷதͰૹΒΕͯ͘ΔϝοηʔδΛॲཧͯ͠ฦ৴ͨ͠Γ͢Δ
  7. εέʔϥϏϦςΟ - ୯७ͳ Bot 11 • ͜ΕΛσϓϩΠ͢Δ͚ͩͰ…
 ʢ͍͔ͭ͘՝୊͸͋Δ΋ͷͷʣεέʔϥϒϧͳ Bot ͕Ͱ͖͕͋Γ

    • ͜ͷ࣌఺ͷεέʔϥϏϦςΟɿ • ༑ୡ͕͍ͳ͍ͱ͖ → Πϯελϯε͸ىಈ͍ͯ͠ͳ͍ • ༑ୡ͕૿͑ͨΓϝοηʔδ͕ૹΒΕͯ͘ΔͱίʔϧόοΫ͕ୟ͔ΕΔ
 → ༑ୡͷ૿͑ํ΍ϝοηʔδͷྲྀྔʹ߹ΘͤͯΠϯελϯε͕ࣗಈͰ૿ݮ • Πϯελϯεͷىಈ͸਺ेʙ200msͳͷͰऔΓ͜΅͠ͳ͠ • Πϯελϯεͷىಈ࣌ؒ͸࣮૷ݴޠʹΑͬͯҟͳΔ • ΋ͪΖΜ Go ͕࠷଎
  8. εέʔϥϏϦςΟ - ୯७ͳ Bot 12 • ՝୊ɿ • ͻͱͭͷϦΫΤετΛॲཧ͢Δͷʹ͕͔͔࣌ؒͬͯ͠·͏ͱ͖ •

    GAE ଆɿϦΫΤετ͸60ඵͰλΠϜΞ΢τ͢Δ • LINE BOT ଆɿ 1ϦΫΤετ͸10ඵͰλΠϜΞ΢τ͢Δ • → ίʔϧόοΫͷड෇ͱͦͷޙͷॲཧΛඇಉظʹ͢Δඞཁ͋Γ • աڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ
  9. εέʔϥϏϦςΟ - Task Queue 13 • ΩϡʔΛ࢖ͬͯඇಉظʹॲཧ͢Δ • GAE Ͱ͸

    Task Queue ͱ͍͏ػೳ͕
 ఏڙ͞Ε͍ͯΔ プロジェクト App Engine App Engine Datastore Instances Search BigQuery Task Queue - GCE - Logs etc. - Networks
  10. εέʔϥϏϦςΟ - Task Queue 14 • Task Queue ͷ Push

    Queue Λ࢖͏ • ͖Ίࡉ͔͍࣮ߦ੍ޚ͕؆୯ʹͰ͖Δ • Ұఆ࣌ؒʹॲཧ͢ΔλεΫͷ਺ʢྫɿຖඵ100݅ɺຖ࣌50݅ʣ • ಉ࣌ʹॲཧ͢ΔλεΫͷ੍ݶɺࣦഊ࣌ͷ࠶࣮ߦ੍ޚͳͲ • ؆୯ͳઃఆϑΝΠϧʹΩϡʔΛఆٛ͢Δ total_storage_limit: 1T
 queue:
 - name: received-messages
 rate: 100/s RVFVFZBNM
  11. εέʔϥϏϦςΟ - Task Queue 15 • enqueue ͷํ๏ɿλεΫΛ࡞ͬͯΩϡʔʹ௥Ճ͢Δ • λεΫʹ͸ॲཧઌͱϖΠϩʔυΛࢦఆ

    t := taskqueue.NewPOSTTask("/worker/reply-message", url.Values{...})
 resultTask, err := taskqueue.Add(ctx, t, "received-message")
 if err != nil {
 return err
 } • dequeue ͷํ๏ɿ • Ωϡʔͷઃఆʹ΋ͱ͍ͮͯλεΫ͕ॲཧ͞Ε͍ͯ͘ • ্ͷ৔߹ POST ϦΫΤετͰ /worker/reply-message ͕ୟ͔ΕΔ • → λεΫ͸୯७ͳ HTTP ϦΫΤετʹͳΔ
  12. εέʔϥϏϦςΟ - ӬଓԽ 17 • ՝୊ɿաڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ •

    NoSQL σʔλϕʔεʮDatastoreʯΛ࢖͏ • ΩʔʹΑΔ CRUD • ෳ߹ΠϯσοΫε • ʢ੍໿ଟΊͷʣτϥϯβΫγϣϯ • ΫΤϦͳͲ
  13. εέʔϥϏϦςΟ - ӬଓԽ 18 type Message struct {
 Id int64


    SenderId int64
 Sender *User `datastore:"-"`
 Text string
 Unread bool
 Location Location
 CreatedTime time.Time
 }
 
 type Location struct {
 Lat float64
 Lng float64
 } • Datastore ʹอଘ͢ΔΤϯςΟςΟΛ
 ఆٛ͢Δ • ͍ͨͬͯී௨ͷߏ଄ମ • datastore λά͕෇͍͍ͯΔ • อଘ࣌ͷ໊લมߋ • อଘ͢Δ͔Ͳ͏͔ • ୯ҰߦΠϯσοΫεΛ෇͚Δ͔
  14. εέʔϥϏϦςΟ - ӬଓԽ 19 key := datastore.NewKey(ctx, "Message", "", msg.Id,

    nil)
 if _, err := datastore.Put(ctx, key, msg); err != nil {
 return err
 } • ϝοηʔδͷอଘ *% 4FOEFS*E 5FYU 6OSFBE -PDBUJPO-BU -PDBUJPO-OH $SFBUFE5JNF   ͜Μʹͪ͸ 536&      ͓ෲ͍ͨ͢ '"-4&   
  15. εέʔϥϏϦςΟ - ӬଓԽ 20 • ϝοηʔδͷऔಘ key := datastore.NewKey(c, "Message",

    "", id, nil)
 if err := datastore.Get(c, key, &msg); err != nil {
 return nil, err
 } q := datastore.NewQuery("Message")
 q = q.Filter("SenderId =", senderId).Order("-CreatedTime").Limit(10)
 var msgs []Message
 _, err := q.GetAll(c, &msgs) • ΫΤϦ
  16. εέʔϥϏϦςΟ - Datastore ͷಛ௃ 21 • Pros • ۃΊͯߴ͍εέʔϥϏϦςΟ •

    ӬଓԽͰ͖ΔσʔλʢΤϯςΟςΟʣͷ਺ʹ੍ݶ͸ͳ͍ • ӬଓԽσʔλ͕ͲΕ͚ͩ૿͑ͯ΋औಘͱ௥Ճͷ଎౓͸ྼԽ͠ͳ͍ • 1݅Ͱ΋10ԯ݅Ͱ΋ಉ͡ • ಉҰΤϯςΟςΟͰͳ͚Ε͹੍ݶͳ͠ͷฒྻॻ͖ࠐΈ • ۃΊͯߴ͍Մ༻ੑ • μ΢ϯλΠϜͳ͠ • Cons • BigTable ΑΓߴػೳͱ͸͍͑ϓϦϛςΟϒ • ݶఆ͞Εͨ৚݅Ͱ͔͠ ACID τϥϯβΫγϣϯ͕αϙʔτ͞Εͳ͍ • JOIN ͕ͳ͍ɺू໿ؔ਺͕ͳ͍ɺetc.
  17. ϩά෼ੳͱӡ༻ 28 • ϦΫΤετʹͻ΋෇͍ͨΞϓϦέʔγϣϯϩάΛग़ྗ͢Δ log.Infof(ctx, "Read Message: id=%v", ids[i])
 


    log.Warningf(ctx, "missing Message entity: id=%v, err=%v", ids[i], err) ͦͷ··ϩάϏϡʔϫʹग़ྗ͞ΕΔ #JH2VFSZ΁ͷϦΞϧλΠϜૠೖ΋
 ઃఆϘλϯΛԡ͚ͩ͢
  18. ·ͱΊ 31 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ

    Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘