-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathexample_test.go
72 lines (67 loc) · 1.67 KB
/
example_test.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
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2017 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tcp_test
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"github.com/mikioh/tcp"
"github.com/mikioh/tcpinfo"
"github.com/mikioh/tcpopt"
)
// Monitoring a TCP connection
//
// For now only Darwin, FreeBSD, Linux and NetBSD kernels support the
// TCP information option. A custom net.Dial function that hooks up an
// underlying transport connection must be prepared before monitoring.
func ExampleConn() {
// When the underlying transport connection is established,
// your monitor goroutine can start monitoring the connection
// by using the Option method of Conn and tcpinfo package.
monitor := func(tc *tcp.Conn) {
tc.SetOption(tcpopt.KeepAlive(true))
tc.SetOption(tcpopt.KeepAliveProbeCount(3))
var o tcpinfo.Info
var b [256]byte
for {
i, err := tc.Option(o.Level(), o.Name(), b[:])
if err != nil {
log.Println(err)
return
}
txt, err := json.Marshal(i)
if err != nil {
log.Println(err)
return
}
fmt.Println(string(txt))
}
}
tr := &http.Transport{
Dial: func(network, address string) (net.Conn, error) {
d := net.Dialer{DualStack: true}
c, err := d.Dial(network, address)
if err != nil {
return nil, err
}
tc, err := tcp.NewConn(c)
if err != nil {
c.Close()
return nil, err
}
go monitor(tc)
return tc.Conn, nil
},
TLSClientConfig: &tls.Config{ServerName: "golang.org"},
}
client := http.Client{Transport: tr}
resp, err := client.Get("https://golang.org")
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
}