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
10k
React Native Beyond Prototype
timakin
2
1.6k
Performance Optimization on Google AppEngine
timakin
5
6.2k
testcache.pdf
timakin
1
130
How Go cache
timakin
1
66
How Go cache tests
timakin
1
2.9k
Other Decks in Programming
See All in Programming
php-conference-japan-2024
tasuku43
0
290
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
Refactor your code - refactor yourself
xosofox
1
260
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
선언형 UI에서의 상태관리
l2hyunwoo
0
160
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
760
Beyond ORM
77web
6
780
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
930
MCP with Cloudflare Workers
yusukebe
2
220
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
Recoilを剥がしている話
kirik
5
6.7k
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Making Projects Easy
brettharned
116
5.9k
Unsuck your backbone
ammeep
669
57k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
A Philosophy of Restraint
colly
203
16k
Designing Experiences People Love
moore
138
23k
The Invisible Side of Design
smashingmag
298
50k
Gamification - CAS2011
davidbonilla
80
5.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
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!