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
280
0
Share
http.Client in go
By Raf
Buzzvil
April 14, 2021
More Decks by Buzzvil
See All by Buzzvil
220903_GFS
buzzvil
0
610
Git 해부하기 2 + 3
buzzvil
0
63
Metastable Failure
buzzvil
0
340
Git 해부하기
buzzvil
0
77
Introduction to Plate Solving
buzzvil
0
70
Airbnb Minerva
buzzvil
0
490
Shape up 방법론
buzzvil
0
1.1k
Buzzvil Billing Data Pipeline
buzzvil
0
700
Journey of Dash's release-cycle
buzzvil
0
260
Other Decks in Programming
See All in Programming
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
1.6k
Moments When Things Go Wrong
aurimas
3
110
AIエージェントと協働するCLI開発 — BunとOpenClawで学んだこと
yoshikouki
1
210
Kubernetesを使わない環境にもCloud Nativeなデプロイを実現する / Enabling Cloud Native deployments without the complexity of Kubernetes
linyows
3
540
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
370
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
1
960
TSKaigi 2026 TypeScriptバックエンドのオブザーバビリティ戦略 — Datadog × NestJSの実践
taiseiyamamotoan
1
180
ふつうのFeature Flag実践入門
irof
1
160
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
360
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
290
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
180
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
940
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.5k
Music & Morning Musume
bryan
47
7.2k
Un-Boring Meetings
codingconduct
0
300
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
97
Code Review Best Practice
trishagee
74
20k
Color Theory Basics | Prateek | Gurzu
gurzu
0
310
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
390
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.7k
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