Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)
Search
Tatsuya Tsuruoka
April 23, 2016
Programming
12
8.1k
GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)
2016年4月23日の Go Conference 2016 Spring の発表資料です。
Tatsuya Tsuruoka
April 23, 2016
Tweet
Share
More Decks by Tatsuya Tsuruoka
See All by Tatsuya Tsuruoka
「メルカリ アッテ」を支える Google App Engine と Golang
ttsuruoka
18
12k
アッテ開発の技術 : Golang と Google App Engine
ttsuruoka
20
48k
メルカリの超高速開発を支えるPHP (PHPCon2014)
ttsuruoka
17
25k
メルカリ開発の歴史
ttsuruoka
2
2k
LT_有給とって温泉に行こう
ttsuruoka
0
230
Other Decks in Programming
See All in Programming
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
840
座談会 「Strict ConcurrencyとSwift 6が開く新時代: 私たちはどう生きるか?」
shiz
4
2.9k
Amebaチョイス立ち上げの裏側 ~依存システムとの闘い~
daichi_igarashi
0
220
GoのIteratorに詳しくなってしまう
inatonix
1
170
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
210
ESLint Rule により事業, 技術ドメインに沿った制約と誓約を敷衍させるアプローチのすゝめ
shinyaigeek
1
2.7k
Swiftコードバトル必勝法
toshi0383
0
140
What we keep in mind when migrating from Serverless Framework to AWS CDK and AWS SAM
kasacchiful
1
130
Scala におけるコンパイラエラーとの付き合い方
chencmd
1
150
Ruby Parser progress report 2024
yui_knk
2
180
The Future of Frontend i18n : Intl.MessageFormat
sajikix
1
2.4k
現代のVueとTypeScript - 型安全の活用術
minako__ph
4
3.1k
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
662
120k
Building a Modern Day E-commerce SEO Strategy
aleyda
35
6.8k
What the flash - Photography Introduction
edds
67
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
122
18k
4 Signs Your Business is Dying
shpigford
179
21k
In The Pink: A Labor of Love
frogandcode
139
22k
Ruby is Unlike a Banana
tanoku
96
10k
Web Components: a chance to create the future
zenorocha
308
41k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
157
15k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
The Pragmatic Product Professional
lauravandoore
30
6.2k
Transcript
(Pͱ("&ʹΑΔ 8FCΞϓϦέʔγϣϯ։ൃ 2016/04/23
ࣗݾհ 2 • גࣜձࣾικ ˍ גࣜձࣾϝϧΧϦ • ٕज़తͳҙࢥܾఆͱνʔϜ࡞Γ͕ࣄͰ͢ !UUTVSVPLB Ԭ
ୡ
ࠓճ͓͍͑ͨ͜͠ͱ 3 1. Google App Engine / Go Ͱ ݱ࣮తͳ
Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘
INDEX Why Google App Engineʁ εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ ϩάੳͱӡ༻
Why GAE? - GAE ͱ 5 • GAE Web
ΞϓϦͷͨΊͷ PaaS • ༷ʑͳ։ൃݴޠʹରԠ͓ͯ͠Γ Go ͦͷ͏ͪͷͻͱͭ • ࣮ݱͰ͖Δ͜ͱ • ۃΊͯߴ͍εέʔϥϏϦςΟ • req/sec ेສreq/sec ಉ͡Α͏ʹѻ͑Δ • Google ϨϕϧͷՄ༻ੑ • োʹΑͬͯαʔϏε͕ఀࢭ͢Δ͜ͱك • ϝϯςφϯεϑϦʔ • ࠷ॳʹΞʔΩςΫνϟΛܾΊΕ͋ͱ͏ख͕͔͔Βͳ͍
Why GAE? - GAE ͱ 6 • ࣮ࡍʹ GAE ͰͲΜͳ
Web ΞϓϦΛ࡞͍͚ͬͯΔʁ • ࣄྫɿSnapchatɺIngress • ผͷར༻Ϣʔβ͕1ԯਓΛ͑Δنͷ࣮͕ɺ͢Ͱʹ͋Δ ※ͨͩ͠ݴޠ Go Ͱͳ͍ͱࢥΘΕ·͢ • ϝϧΧϦࢠձࣾͷ৽نࣄۀʮϝϧΧϦ Ξοςʯ GAE Λ࠷େݶʹར༻ • ΠϯϑϥΤϯδχΞ 0 ਓ • ϦϦʔε͔Βͷ1ϲ݄ؒʹٸܹͳΞΫηε૿ͳͲͰࠔͬͨ͜ͱҰͳ͠ • μϯλΠϜθϩɻܭըఀࢭͳ͠ • → ݱ࣮తͳ Web ΞϓϦΛ࡞ΔͨΊͷػೳඋΘ͍ͬͯΔ
Why GAE? 7 • ࠓճɺGAE ͷػೳΛհ͢ΔͨΊʹ Web ΞϓϦΛߟ͑ͯΈΔ • ྫ͑ɺ͍·Γ্͕͍ͬͯΔ
Bot ΞϓϦ 4݄7 LINE BOT API ެ։ɺ4݄13 Facebook bots on Messenger ެ։ • → ͜ͷ Bot ΞϓϦΛ GAE ͰϋΠεέʔϥϒϧʹ࡞Δͱͨ͠Β...
εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ
εέʔϥϏϦςΟ - ୯७ͳ Bot 9 • ػೳɿ • νϟοταʔόʢ͜ͷྫͰLINEʣ͔Β ίʔϧόοΫΛड͚औΓɺ
ϢʔβͷϝοηʔδͳͲΛऔಘͰ͖Δ • ϝοηʔδΛݩʹԿΒ͔ͷॲཧΛͯ͠ɺ Ϣʔβʹฦ৴Ͱ͖Δ
εέʔϥϏϦςΟ - ୯७ͳ Bot 10 • ίʔϧόοΫΛϋϯυϦϯά͢Δ func init() {
http.HandleFunc("/line/callback", receiveLine) } func receiveLine(w http.ResponseWriter, r *http.Request) { ... } • func init GAE/Go ΞϓϦͷཪଆ͔ΒݺΕΔॏཁͳؔ • func receiveLine ͷதͰૹΒΕͯ͘ΔϝοηʔδΛॲཧͯ͠ฦ৴ͨ͠Γ͢Δ
εέʔϥϏϦςΟ - ୯७ͳ Bot 11 • ͜ΕΛσϓϩΠ͢Δ͚ͩͰ… ʢ͍͔ͭ͘՝͋Δͷͷʣεέʔϥϒϧͳ Bot ͕Ͱ͖͕͋Γ
• ͜ͷ࣌ͷεέʔϥϏϦςΟɿ • ༑ୡ͕͍ͳ͍ͱ͖ → Πϯελϯεىಈ͍ͯ͠ͳ͍ • ༑ୡ͕૿͑ͨΓϝοηʔδ͕ૹΒΕͯ͘ΔͱίʔϧόοΫ͕ୟ͔ΕΔ → ༑ୡͷ૿͑ํϝοηʔδͷྲྀྔʹ߹ΘͤͯΠϯελϯε͕ࣗಈͰ૿ݮ • Πϯελϯεͷىಈेʙ200msͳͷͰऔΓ͜΅͠ͳ͠ • Πϯελϯεͷىಈ࣮࣌ؒݴޠʹΑͬͯҟͳΔ • ͪΖΜ Go ͕࠷
εέʔϥϏϦςΟ - ୯७ͳ Bot 12 • ՝ɿ • ͻͱͭͷϦΫΤετΛॲཧ͢Δͷʹ͕͔͔࣌ؒͬͯ͠·͏ͱ͖ •
GAE ଆɿϦΫΤετ60ඵͰλΠϜΞτ͢Δ • LINE BOT ଆɿ 1ϦΫΤετ10ඵͰλΠϜΞτ͢Δ • → ίʔϧόοΫͷडͱͦͷޙͷॲཧΛඇಉظʹ͢Δඞཁ͋Γ • աڈͷϝοηʔδͷΓͱΓͱൺͯฦ৴Λ͍ͨ͠ • → աڈͷΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ
εέʔϥϏϦςΟ - Task Queue 13 • ΩϡʔΛͬͯඇಉظʹॲཧ͢Δ • GAE Ͱ
Task Queue ͱ͍͏ػೳ͕ ఏڙ͞Ε͍ͯΔ プロジェクト App Engine App Engine Datastore Instances Search BigQuery Task Queue - GCE - Logs etc. - Networks
εέʔϥϏϦςΟ - Task Queue 14 • Task Queue ͷ Push
Queue Λ͏ • ͖Ίࡉ͔͍࣮ߦ੍ޚ͕؆୯ʹͰ͖Δ • Ұఆ࣌ؒʹॲཧ͢ΔλεΫͷʢྫɿຖඵ100݅ɺຖ࣌50݅ʣ • ಉ࣌ʹॲཧ͢ΔλεΫͷ੍ݶɺࣦഊ࣌ͷ࠶࣮ߦ੍ޚͳͲ • ؆୯ͳઃఆϑΝΠϧʹΩϡʔΛఆٛ͢Δ total_storage_limit: 1T queue: - name: received-messages rate: 100/s RVFVFZBNM
εέʔϥϏϦςΟ - 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 ϦΫΤετʹͳΔ
εέʔϥϏϦςΟ - Task Queue 16
εέʔϥϏϦςΟ - ӬଓԽ 17 • ՝ɿաڈͷϝοηʔδͷΓͱΓͱൺͯฦ৴Λ͍ͨ͠ • → աڈͷΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ •
NoSQL σʔλϕʔεʮDatastoreʯΛ͏ • ΩʔʹΑΔ CRUD • ෳ߹ΠϯσοΫε • ʢ੍ଟΊͷʣτϥϯβΫγϣϯ • ΫΤϦͳͲ
εέʔϥϏϦςΟ - ӬଓԽ 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 λά͕͍͍ͯΔ • อଘ࣌ͷ໊લมߋ • อଘ͢Δ͔Ͳ͏͔ • ୯ҰߦΠϯσοΫεΛ͚Δ͔
εέʔϥϏϦςΟ - ӬଓԽ 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&
εέʔϥϏϦςΟ - ӬଓԽ 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) • ΫΤϦ
εέʔϥϏϦςΟ - Datastore ͷಛ 21 • Pros • ۃΊͯߴ͍εέʔϥϏϦςΟ •
ӬଓԽͰ͖ΔσʔλʢΤϯςΟςΟʣͷʹ੍ݶͳ͍ • ӬଓԽσʔλ͕ͲΕ͚ͩ૿͑ͯऔಘͱՃͷྼԽ͠ͳ͍ • 1݅Ͱ10ԯ݅Ͱಉ͡ • ಉҰΤϯςΟςΟͰͳ͚Ε੍ݶͳ͠ͷฒྻॻ͖ࠐΈ • ۃΊͯߴ͍Մ༻ੑ • μϯλΠϜͳ͠ • Cons • BigTable ΑΓߴػೳͱ͍͑ϓϦϛςΟϒ • ݶఆ͞Εͨ݅Ͱ͔͠ ACID τϥϯβΫγϣϯ͕αϙʔτ͞Εͳ͍ • JOIN ͕ͳ͍ɺू͕ؔͳ͍ɺetc.
ϩάੳͱӡ༻
ϩάੳͱӡ༻ 23 • Web ΞϓϦΛެ։͢Εɺ ͦͷΞΫηεϩάΛݟͨΓɺ༷ʑͳσʔλੳΛ͢Δඞཁ͋Δ • Web ΞϓϦ͕ਖ਼ৗʹಈ͍͍ͯΔͷ͔Ͳ͏͔ࢹ͍ͨ͠ •
→ ͪΖΜ GAEʢGCPʣ͚ͩͰରԠՄೳ
౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ 24
౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 25
౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 26
౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 27
ϩάੳͱӡ༻ 28 • ϦΫΤετʹͻ͍ͨΞϓϦέʔγϣϯϩάΛग़ྗ͢Δ log.Infof(ctx, "Read Message: id=%v", ids[i])
log.Warningf(ctx, "missing Message entity: id=%v, err=%v", ids[i], err) ͦͷ··ϩάϏϡʔϫʹग़ྗ͞ΕΔ #JH2VFSZͷϦΞϧλΠϜૠೖ ઃఆϘλϯΛԡ͚ͩ͢
ϩάੳͱӡ༻ 29 ϩάͷݕࡧ݅Λࢦఆ ϑΟϧλ໊Λ͚Δ
ϩάੳͱӡ༻ 30 • ࡞ͬͨϩάϑΟϧλΛͬͯɺ ݅Λ࡞ɻ ͖͍͠Λ͑ͨΒΞϥʔτ ʢϝʔϧɺSlack ͳͲ௨ʣ
·ͱΊ 31 1. Google App Engine / Go Ͱ ݱ࣮తͳ
Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘