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
62
How Go cache tests
timakin
1
2.9k
Other Decks in Programming
See All in Programming
Going Staff - Keynote edition
pragtob
0
430
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
950
Kotlinの好きなところ
kobaken0029
0
220
僕がつくった48個のWebサービス達
yusukebe
17
16k
ピクシブ百科事典のWebフロントエンドパフォーマンス改善
higara
0
230
#pixiv小説画像メーカー はこうしてできた / The Making of the pixiv Novel Image Maker
roiban
0
220
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
290
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
160
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
350
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
3
1k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
220
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
73
9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
280
Happy Clients
brianwarren
97
6.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
670
Six Lessons from altMBA
skipperchong
26
3.4k
Automating Front-end Workflow
addyosmani
1365
200k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
Agile that works and the tools we love
rasmusluckow
327
21k
Building an army of robots
kneath
302
42k
Building Better People: How to give real-time feedback that sticks.
wjessup
363
19k
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!