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
Advanced API Calling in Go
Search
Seiji Takahashi
August 22, 2017
Programming
6
3k
Advanced API Calling in Go
Go 1.9 Release Party in Tokyo での発表資料です by @__timakin__
Seiji Takahashi
August 22, 2017
Tweet
Share
More Decks by Seiji Takahashi
See All by Seiji Takahashi
Go Backends for frontends with GraphQL and gRPC
timakin
6
3.7k
Design Pattern for Image and Text Composition in Go
timakin
5
6.5k
Golang API Testing the HARD way
timakin
13
6.4k
Head First Golang Image Package
timakin
2
9.9k
React Native Beyond Prototype
timakin
2
1.6k
Performance Optimization on Google AppEngine
timakin
5
6.1k
testcache.pdf
timakin
1
120
How Go cache
timakin
1
63
How Go cache tests
timakin
1
2.9k
Other Decks in Programming
See All in Programming
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
220
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
Jakarta EE meets AI
ivargrimstad
0
660
CSC509 Lecture 13
javiergs
PRO
0
110
Jakarta EE meets AI
ivargrimstad
0
210
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
cmp.Or に感動した
otakakot
3
200
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
What's new in Ruby 2.0
geeforr
343
31k
Git: the NoSQL Database
bkeepers
PRO
427
64k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
How to Ace a Technical Interview
jacobian
276
23k
Visualization
eitanlees
145
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
900
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Faster Mobile Websites
deanohume
305
30k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Transcript
Advanced API Calling in Go @__timakin__ / Go 1.9 Release
Party in Tokyo
ࣗݾհ • Github: timakin / Twitter: @__timakin__ • ŧŔŕŪ Go
Ŭ Contribute ŢŦ • Gunosy ৽نࣄۀ։ൃࣨ ॴଐ • Go / Swift • Recently Contributed / Created • giginet/xcprofiler • mercari/gaurun • timakin/gopli • timakin/ssm2env • timakin/md2mid • timakin/gonvert
Copyright© Gunosy Inc. All Rights Reserved 3 Go / Python
ΤϯδχΞืूத ▶https://gunosy.co.jp/recruit/ Gunosyɺ౦ژେֶʹ௨͏3ਓͷֶੜͷ ʮใΛੈքதͷਓʹ࠷దʹಧ͚͍ͨʯͱ͍͏͍͔Β࢝·Γ·ͨ͠ɻ ౦ূϚβʔζ্ɺຊώϧζͷΦϑΟεҠసΛܦͯɺ େ͖͍ͯ͘͠ΔձࣾͰ׆༂͍ͨ͠ϝϯόʔΛืू͍ͯ͠·͢ɻ
ࠓͷςʔϚ
GoͰAPI Client࡞Δͱ͖ͷ ΧελϚΠζྫ
ࡢࠓͷAPI Client։ൃ • APIΛୟ͍ͯදࣔ͢ΔCLIπʔϧΛ࡞Δ
ࡢࠓͷAPI Client։ൃ • APIΛୟ͍ͯදࣔ͢ΔCLIπʔϧΛ࡞Δ • ։ൃͯ͠ΔAPI͔ΒͲ͔ͬͷAPIΛݺͼ͍ͨ
ࡢࠓͷAPI Client։ൃ • APIΛୟ͍ͯදࣔ͢ΔCLIπʔϧΛ࡞Δ • ։ൃͯ͠ΔAPI͔ΒͲ͔ͬͷAPIΛݺͼ͍ͨ • SDKܦ༝ͰσʔλΛऔ͖͍ͬͯͨ
ࡢࠓͷAPI Client։ൃ • APIΛୟ͍ͯදࣔ͢ΔCLIπʔϧΛ࡞Δ • ։ൃͯ͠ΔAPI͔ΒͲ͔ͬͷAPIΛݺͼ͍ͨ • SDKܦ༝ͰσʔλΛऔ͖͍ͬͯͨ • Proxy࡞Γ͍ͨ
ࡢࠓͷAPI Client։ൃ • APIΛୟ͍ͯදࣔ͢ΔCLIπʔϧΛ࡞Δ • ։ൃͯ͠ΔAPI͔ΒͲ͔ͬͷAPIΛݺͼ͍ͨ • SDKܦ༝ͰσʔλΛऔ͖͍ͬͯͨ • Proxy࡞Γ͍ͨ
௨ৗͷAPIϦΫΤετ
௨ৗͷϦΫΤετ
ಠࣗAPI Client
ಠࣗͷAPI ClientΛఆٛ͢Δ
ಠࣗͷAPI ClientΛఆٛ͢Δ API ClientͷߏମΛఆٛ͢Δ
ಠࣗͷAPI ClientΛఆٛ͢Δ ίϯετϥΫλΛॻ͘
ಠࣗͷAPI ClientΛఆٛ͢Δ ڞ௨ϝιουΛఆٛ͢Δ
ಠࣗͷAPI ClientΛఆٛ͢Δ Ϧιʔε͝ͱͷAPI ίʔϧΛఆٛ͢Δɻ ͜ͷล௨ৗͷrepositoryͰͬͯΔ͜ͱͱ ͦ͜·ͰมΘΒͳ͍ɻ
goroutine
goroutineͰͷฒߦcall • Goॻ͍ͯΔΜ͔ͩΒɺෳճAPIίʔϧ ͢Δ߹ͪΖΜgoroutine͍͍ͨ
goroutineͰͷฒߦcall • Goॻ͍ͯΔΜ͔ͩΒɺෳճAPIίʔϧ ͢Δ߹ͪΖΜgoroutine͍͍ͨ • ྫʣtimakin/ssm2env
goroutineͰͷฒߦcall • Goॻ͍ͯΔΜ͔ͩΒɺෳճAPIίʔϧ ͢Δ߹ͪΖΜgoroutine͍͍ͨ • ྫʣtimakin/ssm2env • AWS (EC2 Parameter
Store)ʹอଘ͞Ε͍ͯΔ ಛఆͷKeyʹͻͮ͘ValueΛऔ͖ͬͯͯɺ ݁ՌΛmapʹೖΕ͍ͨ
goroutineͰͷฒߦcall • Goॻ͍ͯΔΜ͔ͩΒɺෳճAPIίʔϧ ͢Δ߹ͪΖΜgoroutine͍͍ͨ • ྫʣtimakin/ssm2env • AWS (EC2 Parameter
Store)ʹอଘ͞Ε͍ͯΔ ಛఆͷKeyʹͻͮ͘ValueΛऔ͖ͬͯͯɺ ݁ՌΛmapʹೖΕ͍ͨ • Ͱ10ݸ͔ͣͭ͠औಘͰ͖ͳ͍
goroutineͰͷฒߦcall AWS ClientΛ࣋ͬͨߏମΛఆٛ͢Δ
goroutineͰͷฒߦcall AWS SSMʢύϥϝʔλετΞʣ͔Β औͬͯདྷͨValueΛmapʹͯ͠ฦ͢ϝιουΛఆٛɻ
goroutineͰͷฒߦcall x/sync/errgroupͰ ɾContextͷϋϯυϦϯά ɾϦΫΤετࣗମͷΤϥʔॲཧ ɾgoroutineʹΑΔฒߦϦΫΤετ Λཧ͢Δɻ
goroutineͰͷฒߦcall ɾ֤APIίʔϧͷ݁ՌΛͪड͚Δchannelʢࠓճ”c”ʣΛ༻ҙ͢Δ
goroutineͰͷฒߦcall ɾ֤APIίʔϧͷ݁ՌΛͪड͚Δchannelʢࠓճ”c”ʣΛ༻ҙ͢Δ ɾ10ݸͣͭʹׂͨ͠ϦΫΤετ༻ͷkeyҰཡΛ༻ҙ͢Δ ɹɾ͜͏͍͏ׂchannelܦ༝Ͱ݁ՌΛड͚औΔͱ ɹɹͦͷ··rangeʹͤͯศར
goroutineͰͷฒߦcall ɾ֤APIίʔϧͷ݁ՌΛͪड͚Δchannelʢࠓճ”c”ʣΛ༻ҙ͢Δ ɾ10ݸͣͭʹׂͨ͠ϦΫΤετ༻ͷkeyҰཡΛ༻ҙ͢Δ ɹɾ͜͏͍͏ׂchannelܦ༝Ͱ݁ՌΛड͚औΔͱ ɹɹͦͷ··rangeʹͤͯศར ɾeg.Go෦ͰฒߦॲཧΛߦ͏
goroutineͰͷฒߦcall ɾޭϨεϙϯεͳΒ݁ՌΛchannelʹҾ͖͢
goroutineͰͷฒߦcall ɾޭϨεϙϯεͳΒ݁ՌΛchannelʹҾ͖͢ ɾcontextͷλΠϜΞτ௨Λड͚औͬͨΒͦͷ࣌Ͱऴྃ
goroutineͰͷฒߦcall ɾeg.Go͔ΒΤϥʔ͕ฦͬͯདྷͨΒclose͢Δ
goroutineͰͷฒߦcall ɾeg.Go͔ΒΤϥʔ͕ฦͬͯདྷͨΒclose͢Δ ɾchannelܦ༝Ͱؼͬͯདྷͨ݁ՌΛॱ࣍mapʹ٧ΊΔ
goroutineͰͷฒߦcall x/sync/errgroupͰ ɾContextͷϋϯυϦϯά ɾϦΫΤετࣗମͷΤϥʔॲཧ ɾgoroutineʹΑΔฒߦϦΫΤετ Λཧ͢Δɻ
Configuration
ϦΫΤετ༻ͷৄࡉͳConfig • http.ClientʹΧελϜͳઃఆΛ͍ͨͤͨ
ϦΫΤετ༻ͷৄࡉͳConfig • http.ClientʹΧελϜͳઃఆΛ͍ͨͤͨ • MaxIdleConnɺTimeoutͳͲ
ϦΫΤετ༻ͷৄࡉͳConfig • http.ClientʹΧελϜͳઃఆΛ͍ͨͤͨ • MaxIdleConnɺTimeoutͳͲ • ྫʣmercari/gaurun
ϦΫΤετ༻ͷৄࡉͳConfig ɾhttp.Transport(௨৴ॲཧΛ࣮ࡍʹߦ͏࣮Λ࣋ͬͨߏମ)ʹɺ ɹTimeoutConnͷઃఆΛ͢
ϦΫΤετ༻ͷৄࡉͳConfig ɾhttp.Transport(௨৴ॲཧΛ࣮ࡍʹߦ͏࣮Λ࣋ͬͨߏମ)ʹɺ ɹTimeoutConnͷઃఆΛ͢ ɾhttp.ClientͷϑΟʔϧυʹTransportΛͯ͠ΫϥΠΞϯτੜ
Proxy
Proxy • API ϦΫΤετͷࡍʹυϝΠϯॻ͖͍͑ͨ
Proxy • API ϦΫΤετͷࡍʹυϝΠϯॻ͖͍͑ͨ • ௨৴ΛΩϟογϡ͓͖͍ͯͨ͠
Proxy • API ϦΫΤετͷࡍʹυϝΠϯॻ͖͍͑ͨ • ௨৴ΛΩϟογϡ͓͖͍ͯͨ͠ • SSLɺHTTP2ͳͲΛΑ͠ͳʹΓସ͍͑ͨ
Proxy • API ϦΫΤετͷࡍʹυϝΠϯॻ͖͍͑ͨ • ௨৴ΛΩϟογϡ͓͖͍ͯͨ͠ • SSLɺHTTP2ͳͲΛΑ͠ͳʹΓସ͍͑ͨ • ྫʣkazeburo/chocon
Proxy
Proxy • GoͷHTTPϦΫΤετͷߏཁૉ • http.RoundTripper • HTTPͷτϥϯβΫγϣϯΛ࣮ߦ͠ɺreq/resΛॲཧ͢Δ ͨΊͷΠϯλʔϑΣʔε
Proxy • GoͷHTTPϦΫΤετͷߏཁૉ • http.RoundTripper • HTTPͷτϥϯβΫγϣϯΛ࣮ߦ͠ɺreq/resΛॲཧ͢Δ ͨΊͷΠϯλʔϑΣʔε • http.Transport
• http.RoundTripper Λ࣮ͨ͠ߏମͰɺ௨৴ʹඞཁͳ ઃఆͱɺRoundTripϝιουͷ࣮Λ࣋ͭ
Proxy • GoͷHTTPϦΫΤετͷߏཁૉ • http.RoundTripper • HTTPͷτϥϯβΫγϣϯΛ࣮ߦ͠ɺreq/resΛॲཧ͢Δ ͨΊͷΠϯλʔϑΣʔε • http.Transport
• http.RoundTripper Λ࣮ͨ͠ߏମͰɺ௨৴ʹඞཁͳ ઃఆͱɺRoundTripϝιουͷ࣮Λ࣋ͭ • http.Client • http.Transport Λ෦ʹ࣋ͬͨɺHTTPϓϩτίϧͰͷ௨ ৴Λ͓͜ͳ͏ͨΊͷૉͷΫϥΠΞϯτ࣮ • func (c *Client) Do(…) தͰRoundTripΛݺΜͰ͍Δ
Proxy • kazeburo/choconͰ…
Proxy ϦΫΤετ༰ͷॻ͖͑ɺRoundTripperΛ࣋ͬͨߏମ
Proxy ϦΫΤετ༰ͷॻ͖͑ʢHostͷஔɺBodyͷcopyʣ
Proxy ɾServeHTTPΛ࣮ ɾProxy͕ड͚औͬͨresponseΛɺAPIClient͚ʹcopyͯ͠ ɹฦ٫ͯ͋͛͠Δ
Retry
Retry • ϦΫΤετʹࣦഊͯ͠ɺࢦఆճ·Ͱ ϦτϥΠ͍ͨ͠
Retry • ϦΫΤετʹࣦഊͯ͠ɺࢦఆճ·Ͱ ϦτϥΠ͍ͨ͠ • ྫʣaws-sdk-go
Retry • ϦΫΤετʹࣦഊͯ͠ɺࢦఆճ·Ͱ ϦτϥΠ͍ͨ͠ • ྫʣaws-sdk-go
AWS-SDK-Go • NinjaΈ͍ͨͳίʔυ͍ͬͺ͍͋Δ
AWS-SDK-Go ಠࣗRetryerΛఆٛ͢Δ
AWS-SDK-Go WithRetryerͰɺΧελϜϦτϥΠϠʔΛઃఆͯ͋͛͠Δɻ ʢcontextύοέʔδͷWithValueతͳʣ
AWS-SDK-Go Requestੜ࣌ʹRetryerΛҾ͖͢
AWS-SDK-Go ϦτϥΠՄೳ͔Ͳ͏͔ΛνΣοΫͯ͠ɺtrueͳΒΧϯτ૿Ճ
AWS-SDK-Go νΣοΫޙɺϦτϥΠՄೳͳΒϦΫΤετ༰Λίϐʔ
ସखஈ • ಠࣗRetryerΛఆٛͤͣʹɺcenkalti/backoffͳͲͷ exponential backoff࣮Λ͏ํ๏͋Γ
ྫʣgo-datadog-api
Paging
Paging • ϖʔδϯά༻ͷAPIΛ࡞ΔͷͰͳ͘ɺ ϖʔδϯάରԠͷAPIʹɺҙͷϖʔδ·Ͱ ϦΫΤετΛ͛ଓ͚͍ͨ • ྫʣEC2ύϥϝʔλʔετΞʹอଘ͞Εͨ ΩʔͷҰཡΛऔಘ͍ͨ͠ɻ͕ɺ 10ݸ͔ͣͭ͠ฦͬͯ͜ͳ͍…
AWS-SDK-Go • ࠶Ninja
Paging request.Paginationʂʂʂʂ ϦΫΤετͷstructʹ߹Θͤͯɺϖʔδϯάॲཧͷ࣮Λ࣋ͭ
Paging ϖʔδϯάՄೳͳΒɺ࣍ͷϦΫΤετΛੜ͢Δ
Paging ϖʔδϯάՄೳͳΒɺ࣍ͷϦΫΤετΛੜ͢Δ Ҿ͕͗͢ΔͷͰݟ͑ͳ͍͕ɺ ίʔϧόοΫ͕ઃఆͯ͋ͬͯ͠ɺ ݁Ռͱʮ࠷ޙͷϖʔδ͔ʯ ͷϑϥάΛฦ͢
·ͱΊ
·ͱΊ • ੈͷதʹ৭ʑͳAPI Client͕͍Δ ΈΜͳҧͬͯΈΜͳ͍͍ • ඞཁͳΒΧελϜClientɺͳ͍͠ϦΫΤετ ཧ༻ͷstructΛ࣮͢Δ • aws-sdk-go͕͢͞ʹΓ͗͢ͳͷͰɺ
ͪΐ͏Ͳ͍͍ϋϯυϥΛ࣮͠·͠ΐ͏
Thank you!