Slide 1

Slide 1 text

(Pͱ("&ʹΑΔ
 8FCΞϓϦέʔγϣϯ։ൃ 2016/04/23

Slide 2

Slide 2 text

ࣗݾ঺հ 2 • גࣜձࣾι΢κ΢ ˍ גࣜձࣾϝϧΧϦ • ٕज़తͳҙࢥܾఆͱνʔϜ࡞Γ͕࢓ࣄͰ͢ !UUTVSVPLB ௽Ԭ ୡ໵

Slide 3

Slide 3 text

ࠓճ͓఻͍͑ͨ͜͠ͱ 3 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘

Slide 4

Slide 4 text

INDEX Why Google App Engineʁ εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ ϩά෼ੳͱӡ༻

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Why GAE? 7 • ࠓճɺGAE ͷػೳΛ঺հ͢ΔͨΊʹ Web ΞϓϦΛߟ͑ͯΈΔ • ྫ͑͹ɺ͍·੝Γ্͕͍ͬͯΔ Bot ΞϓϦ
 4݄7೔ LINE BOT API ެ։ɺ4݄13೔ Facebook bots on Messenger ެ։ • → ͜ͷ Bot ΞϓϦΛ GAE ͰϋΠεέʔϥϒϧʹ࡞Δͱͨ͠Β...

Slide 8

Slide 8 text

εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ

Slide 9

Slide 9 text

εέʔϥϏϦςΟ - ୯७ͳ Bot 9 • ػೳɿ • νϟοταʔόʢ͜ͷྫͰ͸LINEʣ͔Β ίʔϧόοΫΛड͚औΓɺ
 ϢʔβͷϝοηʔδͳͲΛऔಘͰ͖Δ • ϝοηʔδΛݩʹԿΒ͔ͷॲཧΛͯ͠ɺ
 Ϣʔβʹฦ৴Ͱ͖Δ

Slide 10

Slide 10 text

εέʔϥϏϦςΟ - ୯७ͳ Bot 10 • ίʔϧόοΫΛϋϯυϦϯά͢Δ func init() {
 http.HandleFunc("/line/callback", receiveLine)
 }
 
 func receiveLine(w http.ResponseWriter, r *http.Request) {
 ...
 } • func init ͸ GAE/Go ΞϓϦͷཪଆ͔Βݺ͹ΕΔॏཁͳؔ਺ • func receiveLine ͷதͰૹΒΕͯ͘ΔϝοηʔδΛॲཧͯ͠ฦ৴ͨ͠Γ͢Δ

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

εέʔϥϏϦςΟ - ୯७ͳ Bot 12 • ՝୊ɿ • ͻͱͭͷϦΫΤετΛॲཧ͢Δͷʹ͕͔͔࣌ؒͬͯ͠·͏ͱ͖ • GAE ଆɿϦΫΤετ͸60ඵͰλΠϜΞ΢τ͢Δ • LINE BOT ଆɿ 1ϦΫΤετ͸10ඵͰλΠϜΞ΢τ͢Δ • → ίʔϧόοΫͷड෇ͱͦͷޙͷॲཧΛඇಉظʹ͢Δඞཁ͋Γ • աڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ

Slide 13

Slide 13 text

εέʔϥϏϦςΟ - Task Queue 13 • ΩϡʔΛ࢖ͬͯඇಉظʹॲཧ͢Δ • GAE Ͱ͸ Task Queue ͱ͍͏ػೳ͕
 ఏڙ͞Ε͍ͯΔ プロジェクト App Engine App Engine Datastore Instances Search BigQuery Task Queue - GCE - Logs etc. - Networks

Slide 14

Slide 14 text

εέʔϥϏϦςΟ - Task Queue 14 • Task Queue ͷ Push Queue Λ࢖͏ • ͖Ίࡉ͔͍࣮ߦ੍ޚ͕؆୯ʹͰ͖Δ • Ұఆ࣌ؒʹॲཧ͢ΔλεΫͷ਺ʢྫɿຖඵ100݅ɺຖ࣌50݅ʣ • ಉ࣌ʹॲཧ͢ΔλεΫͷ੍ݶɺࣦഊ࣌ͷ࠶࣮ߦ੍ޚͳͲ • ؆୯ͳઃఆϑΝΠϧʹΩϡʔΛఆٛ͢Δ total_storage_limit: 1T
 queue:
 - name: received-messages
 rate: 100/s RVFVFZBNM

Slide 15

Slide 15 text

εέʔϥϏϦςΟ - 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 ϦΫΤετʹͳΔ

Slide 16

Slide 16 text

εέʔϥϏϦςΟ - Task Queue 16

Slide 17

Slide 17 text

εέʔϥϏϦςΟ - ӬଓԽ 17 • ՝୊ɿաڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ • NoSQL σʔλϕʔεʮDatastoreʯΛ࢖͏ • ΩʔʹΑΔ CRUD • ෳ߹ΠϯσοΫε • ʢ੍໿ଟΊͷʣτϥϯβΫγϣϯ • ΫΤϦͳͲ

Slide 18

Slide 18 text

εέʔϥϏϦςΟ - ӬଓԽ 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 λά͕෇͍͍ͯΔ • อଘ࣌ͷ໊લมߋ • อଘ͢Δ͔Ͳ͏͔ • ୯ҰߦΠϯσοΫεΛ෇͚Δ͔

Slide 19

Slide 19 text

εέʔϥϏϦςΟ - ӬଓԽ 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&

Slide 20

Slide 20 text

εέʔϥϏϦςΟ - ӬଓԽ 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) • ΫΤϦ

Slide 21

Slide 21 text

εέʔϥϏϦςΟ - Datastore ͷಛ௃ 21 • Pros • ۃΊͯߴ͍εέʔϥϏϦςΟ • ӬଓԽͰ͖ΔσʔλʢΤϯςΟςΟʣͷ਺ʹ੍ݶ͸ͳ͍ • ӬଓԽσʔλ͕ͲΕ͚ͩ૿͑ͯ΋औಘͱ௥Ճͷ଎౓͸ྼԽ͠ͳ͍ • 1݅Ͱ΋10ԯ݅Ͱ΋ಉ͡ • ಉҰΤϯςΟςΟͰͳ͚Ε͹੍ݶͳ͠ͷฒྻॻ͖ࠐΈ • ۃΊͯߴ͍Մ༻ੑ • μ΢ϯλΠϜͳ͠ • Cons • BigTable ΑΓߴػೳͱ͸͍͑ϓϦϛςΟϒ • ݶఆ͞Εͨ৚݅Ͱ͔͠ ACID τϥϯβΫγϣϯ͕αϙʔτ͞Εͳ͍ • JOIN ͕ͳ͍ɺू໿ؔ਺͕ͳ͍ɺetc.

Slide 22

Slide 22 text

ϩά෼ੳͱӡ༻

Slide 23

Slide 23 text

ϩά෼ੳͱӡ༻ 23 • Web ΞϓϦΛެ։͢Ε͹ɺ
 ͦͷΞΫηεϩάΛݟͨΓɺ༷ʑͳσʔλ෼ੳΛ͢Δඞཁ΋͋Δ • Web ΞϓϦ͕ਖ਼ৗʹಈ͍͍ͯΔͷ͔Ͳ͏͔؂ࢹ΋͍ͨ͠ • → ΋ͪΖΜ GAEʢGCPʣ͚ͩͰରԠՄೳ

Slide 24

Slide 24 text

౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ 24

Slide 25

Slide 25 text

౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 25

Slide 26

Slide 26 text

౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 26

Slide 27

Slide 27 text

౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 27

Slide 28

Slide 28 text

ϩά෼ੳͱӡ༻ 28 • ϦΫΤετʹͻ΋෇͍ͨΞϓϦέʔγϣϯϩάΛग़ྗ͢Δ log.Infof(ctx, "Read Message: id=%v", ids[i])
 
 log.Warningf(ctx, "missing Message entity: id=%v, err=%v", ids[i], err) ͦͷ··ϩάϏϡʔϫʹग़ྗ͞ΕΔ #JH2VFSZ΁ͷϦΞϧλΠϜૠೖ΋
 ઃఆϘλϯΛԡ͚ͩ͢

Slide 29

Slide 29 text

ϩά෼ੳͱӡ༻ 29 ϩάͷݕࡧ৚݅Λࢦఆ ϑΟϧλ໊Λ෇͚Δ

Slide 30

Slide 30 text

ϩά෼ੳͱӡ༻ 30 • ࡞ͬͨϩάϑΟϧλΛ࢖ͬͯɺ
 ৚݅Λ࡞੒ɻ
 ͖͍͠஋Λ௒͑ͨΒΞϥʔτ
 ʢϝʔϧɺSlack ͳͲ΁௨஌ʣ

Slide 31

Slide 31 text

·ͱΊ 31 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘