-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (60 loc) · 1.44 KB
/
main.go
File metadata and controls
66 lines (60 loc) · 1.44 KB
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
62
63
64
65
66
package main
import (
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptrace"
"os"
"time"
)
func createHTTPClientWithTimeout(d time.Duration) *http.Client {
client := http.Client{Timeout: d}
return &client
}
func createHTTPGetRequestWithTrace(ctx context.Context, url string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
trace := &httptrace.ClientTrace{
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf("DNS Info: %+v\n", dnsInfo)
},
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %+v\n", connInfo)
},
TLSHandshakeStart: func() {
fmt.Printf("TLS HandShake Start\n")
},
TLSHandshakeDone: func(connState tls.ConnectionState, err error) {
fmt.Printf("TLS HandShake Done\n")
},
PutIdleConn: func(err error) {
fmt.Printf("Put Idle Conn Error: %+v\n", err)
},
}
ctxTrace := httptrace.WithClientTrace(req.Context(), trace)
req = req.WithContext(ctxTrace)
return req, err
}
func main() {
d := 5 * time.Second
ctx := context.Background()
client := createHTTPClientWithTimeout(d)
req, err := createHTTPGetRequestWithTrace(ctx, os.Args[1])
if err != nil {
log.Fatal(err)
}
for {
resp, _ := client.Do(req)
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
fmt.Printf("Resp protocol: %#v\n", resp.Proto)
time.Sleep(1 * time.Second)
fmt.Println("--------")
}
}