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.2k
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
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Remix on Hono on Cloudflare Workers
yusukebe
1
300
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Amazon Qを使ってIaCを触ろう!
maruto
0
410
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
CSC509 Lecture 11
javiergs
PRO
0
180
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
630
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Side Projects
sachag
452
42k
Faster Mobile Websites
deanohume
305
30k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Git: the NoSQL Database
bkeepers
PRO
427
64k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Invisible Side of Design
smashingmag
298
50k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
What's in a price? How to price your products and services
michaelherold
243
12k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
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 ΞϓϦ։ൃ͕ɺ ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘