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
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
270
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
useSyncExternalStoreを使いまくる
ssssota
6
1k
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
ドメインイベント増えすぎ問題
h0r15h0
1
220
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
220
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
CSC509 Lecture 14
javiergs
PRO
0
140
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Faster Mobile Websites
deanohume
305
30k
YesSQL, Process and Tooling at Scale
rocio
169
14k
A designer walks into a library…
pauljervisheath
204
24k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
A better future with KSS
kneath
238
17k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Code Review Best Practice
trishagee
65
17k
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!