/
httptrace.go
61 lines (57 loc) · 1.51 KB
/
httptrace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptrace"
"os"
)
func main() {
traceCtx := httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
GetConn: func(hostPort string) {
fmt.Printf("Prepare to get a connection for %s.\n", hostPort)
},
GotConn: func(info httptrace.GotConnInfo) {
fmt.Printf("Got a connection: reused: %v, from the idle pool: %v.\n",
info.Reused, info.WasIdle)
},
PutIdleConn: func(err error) {
if err == nil {
fmt.Println("Put a connection to the idle pool: ok.")
} else {
fmt.Println("Put a connection to the idle pool:", err.Error())
}
},
ConnectStart: func(network, addr string) {
fmt.Printf("Dialing... (%s:%s).\n", network, addr)
},
ConnectDone: func(network, addr string, err error) {
if err == nil {
fmt.Printf("Dial is done. (%s:%s)\n", network, addr)
} else {
fmt.Printf("Dial is done with error: %s. (%s:%s)\n", err, network, addr)
}
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
if info.Err == nil {
fmt.Println("Wrote a request: ok.")
} else {
fmt.Println("Wrote a request:", info.Err.Error())
}
},
GotFirstResponseByte: func() {
fmt.Println("Got the first response byte.")
},
})
req, err := http.NewRequest("GET", "http://www.baidu.com/", nil)
if err != nil {
log.Fatal("Fatal error:", err)
}
req = req.WithContext(traceCtx)
_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Request error: %v\n", err)
os.Exit(1)
}
}