-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
dialer.go
180 lines (158 loc) · 4.48 KB
/
dialer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package netext
import (
"context"
"net"
"strings"
"sync/atomic"
"time"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
"github.com/viki-org/dnscache"
)
// Dialer wraps net.Dialer and provides k6 specific functionality -
// tracing, blacklists and DNS cache and aliases.
type Dialer struct {
net.Dialer
Resolver *dnscache.Resolver
Blacklist []*net.IPNet
Hosts map[string]net.IP
BytesRead int64
BytesWritten int64
}
// NewDialer constructs a new Dialer and initializes its cache.
func NewDialer(dialer net.Dialer) *Dialer {
return &Dialer{
Dialer: dialer,
Resolver: dnscache.New(0),
}
}
// DialContext wraps the net.Dialer.DialContext and handles the k6 specifics
func (d *Dialer) DialContext(ctx context.Context, proto, addr string) (net.Conn, error) {
delimiter := strings.LastIndex(addr, ":")
host := addr[:delimiter]
// lookup for domain defined in Hosts option before trying to resolve DNS.
ip, ok := d.Hosts[host]
if !ok {
var err error
ip, err = d.Resolver.FetchOne(host)
if err != nil {
return nil, err
}
}
for _, net := range d.Blacklist {
if net.Contains(ip) {
return nil, errors.Errorf("IP (%s) is in a blacklisted range (%s)", ip, net)
}
}
ipStr := ip.String()
if strings.ContainsRune(ipStr, ':') {
ipStr = "[" + ipStr + "]"
}
conn, err := d.Dialer.DialContext(ctx, proto, ipStr+":"+addr[delimiter+1:])
if err != nil {
return nil, err
}
conn = &Conn{conn, &d.BytesRead, &d.BytesWritten}
return conn, err
}
// GetTrail creates a new NetTrail instance with the Dialer
// sent and received data metrics and the supplied times and tags.
func (d *Dialer) GetTrail(startTime, endTime time.Time, fullIteration bool, tags *stats.SampleTags) *NetTrail {
bytesWritten := atomic.SwapInt64(&d.BytesWritten, 0)
bytesRead := atomic.SwapInt64(&d.BytesRead, 0)
samples := []stats.Sample{
{
Time: endTime,
Metric: metrics.DataSent,
Value: float64(bytesWritten),
Tags: tags,
},
{
Time: endTime,
Metric: metrics.DataReceived,
Value: float64(bytesRead),
Tags: tags,
},
}
if fullIteration {
samples = append(samples, stats.Sample{
Time: endTime,
Metric: metrics.IterationDuration,
Value: stats.D(endTime.Sub(startTime)),
Tags: tags,
})
}
return &NetTrail{
BytesRead: bytesRead,
BytesWritten: bytesWritten,
FullIteration: fullIteration,
StartTime: startTime,
EndTime: endTime,
Tags: tags,
Samples: samples,
}
}
// NetTrail contains information about the exchanged data size and length of a
// series of connections from a particular netext.Dialer
type NetTrail struct {
BytesRead int64
BytesWritten int64
FullIteration bool
StartTime time.Time
EndTime time.Time
Tags *stats.SampleTags
Samples []stats.Sample
}
// Ensure that interfaces are implemented correctly
var _ stats.ConnectedSampleContainer = &NetTrail{}
// GetSamples implements the stats.SampleContainer interface.
func (ntr *NetTrail) GetSamples() []stats.Sample {
return ntr.Samples
}
// GetTags implements the stats.ConnectedSampleContainer interface.
func (ntr *NetTrail) GetTags() *stats.SampleTags {
return ntr.Tags
}
// GetTime implements the stats.ConnectedSampleContainer interface.
func (ntr *NetTrail) GetTime() time.Time {
return ntr.EndTime
}
// Conn wraps net.Conn and keeps track of sent and received data size
type Conn struct {
net.Conn
BytesRead, BytesWritten *int64
}
func (c *Conn) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
if n > 0 {
atomic.AddInt64(c.BytesRead, int64(n))
}
return n, err
}
func (c *Conn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
if n > 0 {
atomic.AddInt64(c.BytesWritten, int64(n))
}
return n, err
}