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
http.Client in go
Search
Buzzvil
April 14, 2021
Programming
0
260
http.Client in go
By Raf
Buzzvil
April 14, 2021
Tweet
Share
More Decks by Buzzvil
See All by Buzzvil
220903_GFS
buzzvil
0
490
Git 해부하기 2 + 3
buzzvil
0
42
Metastable Failure
buzzvil
0
250
Git 해부하기
buzzvil
0
55
Introduction to Plate Solving
buzzvil
0
43
Airbnb Minerva
buzzvil
0
350
Shape up 방법론
buzzvil
0
970
Buzzvil Billing Data Pipeline
buzzvil
0
580
Journey of Dash's release-cycle
buzzvil
0
200
Other Decks in Programming
See All in Programming
iOS 17で追加されたSubscriptionStoreView を利用して5分でサブスク実装チャレンジ
natmark
0
640
Reduxモダナイズ 〜コードのモダン化を通して、将来のライブラリ移行に備える〜
pvcresin
2
690
2分台で1500examples完走!爆速CIを支える環境構築術 - Kaigi on Rails 2025
falcon8823
3
3.4k
Pull-Requestの内容を1クリックで動作確認可能にするワークフロー
natmark
2
480
開発者への寄付をアプリ内課金として実装する時の気の使いどころ
ski
0
360
ポスターセッション: 「まっすぐ行って、右!」って言ってラズパイカーを動かしたい 〜生成AI × Raspberry Pi Pico × Gradioの試作メモ〜
komofr
0
1k
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
1
490
なぜGoのジェネリクスはこの形なのか? Featherweight Goが明かす設計の核心
ryotaros
7
1k
大規模アプリのDIフレームワーク刷新戦略 ~過去最大規模の並行開発を止めずにアプリ全体に導入するまで~
mot_techtalk
0
400
あなたの知らない「動画広告」の世界 - iOSDC Japan 2025
ukitaka
0
420
明日から始めるリファクタリング
ryounasso
0
120
CSC305 Lecture 04
javiergs
PRO
0
260
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
32
2.2k
A Tale of Four Properties
chriscoyier
160
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Documentation Writing (for coders)
carmenintech
75
5k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
189
55k
Raft: Consensus for Rubyists
vanstee
139
7.1k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
KATA
mclloyd
32
15k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Transcript
http.Client in go 1.15.6
http.Client가 쓰이는 곳 - RESTful API - elastic search -
AWS endpoint http.Client 안씀 - RDBMS connection (postgresql, mysql) - gRPC - redis
http.Client, http.Transport http.Client: 껍데기 http.Transport: 실제 요청 처리
http.Transport 실제 http요청이 처리되는 구현체 connection pool을 들고 있어서 goroutine-safe
함
client.Do(req) client := &http.Client{} // init 시점에 한번만 생성 req
:= http.Request { … } res, err := client.Do(req) if err != nil { … } defer res.Body.Close() …
Transport shared variables
getConn()
tryPutIdleConn()
default values
http.ClientTrace trace := &httptrace.ClientTrace{ GetConn: func(hostPort string) { fmt.Println(“%s”, hostPort)
}, } req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) res, err := client.Do(req)
httptrace.ClientTrace Request 1. http.Client.Do(req) 2. RoundTrip() 3. GetConn() 4. idleConn
가져오기 실패 5. idleConnWait에 req 등록 6. go dialConnFor()
1. DNS Lookup 2. DNSStart() 3. DNSDone() 4. Connect 5.
ConnectStart() 6. ConnectDone() 7. TLSHandshake 8. TLSHandshakeStart() 9. TLSHandshakeDone() 10. go readLoop() 11. go writeLoop() httptrace.ClientTrace Request dialConnFor()
httptrace.ClientTrace Request dialConnFor() pc.readLoop() pc.writeLoop() 1. <- pc.reqch 12. tryDeliver(pc)
7. GotConn() 8. pc.roundTrip() 9. pc.writech <- WriteRequest 10. pc.reqch <- requestAndChan 11. <- WriteErrch, resc 1. <- pc.writech
httptrace.ClientTrace Request pc.readLoop() pc.writeLoop() 2. br.peek(1) 3. GotFirstResponseByte() 4. ReadResponse(br)
5. Got100Continue() 6. continueCh <- {} 2. WroteHeaderField() 3. WroteHeaders() 4. Wait100Continue() 5. <- continueCh 6. bw.write(req) 7. WroteRequest() <- pc.writech
httptrace.ClientTrace Request pc.readLoop() 7. ReadResponse(br) 8. requestAndChan.resc <- body 9.
<- waitForBodyRead 10. tryPutIdleConn() 11. PutIdleConn() <- pc.reqch 12. ended roundTrip() 13. ended http.Client.Do() 14. call res.body.Close()
request가 완료되었을때 trace는?? - 커넥션을 받고 난 뒤 실제 요청을
던지는 시점: GotConn() - request가 완료되었을때 trace는 RoundTripper interface를 활용해야함 - http.Client에 transport가 interface로 들어가므로, wrapper를 씌워서 transport.RoundTrip이 끝나는 시점에 trace를 남김
Datadog에 timespan을 보여주기 적절한 trace 방식 1. Transport Wrapper에서 RoundTrip
시작 전에 Trace 2. Transport.RoundTrip() 호출 3. Transport에서 GetConn() 호출 3. Transport에서 GotConn() 호출 4. Transport Wrapper에서 RoundTrip이 완료되었을때 Trace connection을 받는데 걸리는 시간 요청을 보내고, 응답을 받는데 걸리는 시간
요약 1. http.Transport는 한개만 쓸것 2. http.Client.Do()가 끝나고 err가 없으면
res.Body.Close()를 호출할것 3. httptrace.GetConn/GotConn을 활용하여 connect 의 timespan 분리 4. MaxIdleConnsPerHost를 잘 조정하자 (=MaxIdleConns / #hosts)
끝 컨플루언스 문서 • [Golang] http connection management • [Golang]
http.persistConn, httptrace.ClientTrace