-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
conn.go
138 lines (117 loc) · 3.66 KB
/
conn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sync/atomic"
"github.com/gravitational/trace"
)
// NewCloserConn returns new connection wrapper that
// when closed will also close passed closers
func NewCloserConn(conn net.Conn, closers ...io.Closer) *CloserConn {
return &CloserConn{
Conn: conn,
closers: closers,
}
}
// CloserConn wraps connection and attaches additional closers to it
type CloserConn struct {
net.Conn
closers []io.Closer
}
// AddCloser adds any closer in ctx that will be called
// whenever server closes session channel
func (c *CloserConn) AddCloser(closer io.Closer) {
c.closers = append(c.closers, closer)
}
func (c *CloserConn) Close() error {
var errors []error
for _, closer := range c.closers {
errors = append(errors, closer.Close())
}
errors = append(errors, c.Conn.Close())
return trace.NewAggregate(errors...)
}
// Roundtrip is a single connection simplistic HTTP client
// that allows us to bypass a connection pool to test load balancing
// used in tests, as it only supports GET request on /
func Roundtrip(addr string) (string, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return "", err
}
defer conn.Close()
return RoundtripWithConn(conn)
}
// RoundtripWithConn uses HTTP GET on the existing connection,
// used in tests as it only performs GET request on /
func RoundtripWithConn(conn net.Conn) (string, error) {
_, err := fmt.Fprintf(conn, "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
if err != nil {
return "", err
}
re, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return "", err
}
defer re.Body.Close()
out, err := ioutil.ReadAll(re.Body)
if err != nil {
return "", err
}
return string(out), nil
}
// Stater is extension interface of the net.Conn for implementations that
// track connection statistics.
type Stater interface {
// Stat returns TX, RX data.
Stat() (uint64, uint64)
}
// TrackingConn is a net.Conn that keeps track of how much data was transmitted
// (TX) and received (RX) over the net.Conn. A maximum of about 18446
// petabytes can be kept track of for TX and RX before it rolls over.
// See https://golang.org/ref/spec#Numeric_types for more details.
type TrackingConn struct {
// net.Conn is the underlying net.Conn.
net.Conn
// txBytes keeps track of how many bytes were transmitted.
txBytes uint64
// rxBytes keeps track of how many bytes were received.
rxBytes uint64
}
// NewTrackingConn returns a net.Conn that can keep track of how much data was
// transmitted over it.
func NewTrackingConn(conn net.Conn) *TrackingConn {
return &TrackingConn{
Conn: conn,
}
}
// Stat returns the transmitted (TX) and received (RX) bytes over the net.Conn.
func (s *TrackingConn) Stat() (uint64, uint64) {
return atomic.LoadUint64(&s.txBytes), atomic.LoadUint64(&s.rxBytes)
}
func (s *TrackingConn) Read(b []byte) (n int, err error) {
n, err = s.Conn.Read(b)
atomic.AddUint64(&s.rxBytes, uint64(n))
return n, trace.Wrap(err)
}
func (s *TrackingConn) Write(b []byte) (n int, err error) {
n, err = s.Conn.Write(b)
atomic.AddUint64(&s.txBytes, uint64(n))
return n, trace.Wrap(err)
}