forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
283 lines (250 loc) · 7.66 KB
/
link.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package toxiproxy
import (
"context"
"fmt"
"io"
"net"
"time"
"github.com/rs/zerolog"
"github.com/Shopify/toxiproxy/v2/stream"
"github.com/Shopify/toxiproxy/v2/toxics"
)
// ToxicLinks are single direction pipelines that connects an input and output via
// a chain of toxics. The chain always starts with a NoopToxic, and toxics are added
// and removed as they are enabled/disabled. New toxics are always added to the end
// of the chain.
//
// | NoopToxic LatencyToxic
// | v v
// | Input > ToxicStub > ToxicStub > Output.
type ToxicLink struct {
stubs []*toxics.ToxicStub
proxy *Proxy
toxics *ToxicCollection
input *stream.ChanWriter
output *stream.ChanReader
direction stream.Direction
Logger *zerolog.Logger
}
func NewToxicLink(
proxy *Proxy,
collection *ToxicCollection,
direction stream.Direction,
logger zerolog.Logger,
) *ToxicLink {
link := &ToxicLink{
stubs: make(
[]*toxics.ToxicStub,
len(collection.chain[direction]),
cap(collection.chain[direction]),
),
proxy: proxy,
toxics: collection,
direction: direction,
Logger: &logger,
}
// Initialize the link with ToxicStubs
last := make(chan *stream.StreamChunk) // The first toxic is always a noop
link.input = stream.NewChanWriter(last)
for i := 0; i < len(link.stubs); i++ {
var next chan *stream.StreamChunk
if i+1 < len(link.stubs) {
next = make(chan *stream.StreamChunk, link.toxics.chain[direction][i+1].BufferSize)
} else {
next = make(chan *stream.StreamChunk)
}
link.stubs[i] = toxics.NewToxicStub(last, next)
last = next
}
link.output = stream.NewChanReader(last)
return link
}
// Start the link with the specified toxics.
func (link *ToxicLink) Start(
server *ApiServer,
name string,
source io.Reader,
dest io.WriteCloser,
) {
logger := link.Logger
logger.
Debug().
Str("direction", link.Direction()).
Msg("Setup connection")
labels := []string{
link.Direction(),
link.proxy.Name,
link.proxy.Listen,
link.proxy.Upstream}
go link.read(labels, server, source)
for i, toxic := range link.toxics.chain[link.direction] {
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {
link.stubs[i].State = stateful.NewState()
}
if _, ok := toxic.Toxic.(*toxics.ResetToxic); ok {
if err := source.(*net.TCPConn).SetLinger(0); err != nil {
logger.Err(err).
Str("toxic", toxic.Type).
Msg("source: Unable to setLinger(ms)")
}
if err := dest.(*net.TCPConn).SetLinger(0); err != nil {
logger.Err(err).
Str("toxic", toxic.Type).
Msg("dest: Unable to setLinger(ms)")
}
}
go link.stubs[i].Run(toxic)
}
go link.write(labels, name, server, dest)
}
// read copies bytes from a source to the link's input channel.
func (link *ToxicLink) read(
metricLabels []string,
server *ApiServer,
source io.Reader,
) {
logger := link.Logger
bytes, err := io.Copy(link.input, source)
if err != nil {
logger.Warn().
Int64("bytes", bytes).
Err(err).
Msg("Source terminated")
}
if server.Metrics.proxyMetricsEnabled() {
server.Metrics.ProxyMetrics.ReceivedBytesTotal.
WithLabelValues(metricLabels...).Add(float64(bytes))
}
link.input.Close()
}
// write copies bytes from the link's output channel to a destination.
func (link *ToxicLink) write(
metricLabels []string,
name string,
server *ApiServer, // TODO: Replace with AppConfig for Metrics and Logger
dest io.WriteCloser,
) {
logger := link.Logger.
With().
Str("component", "ToxicLink").
Str("method", "write").
Str("link", name).
Str("proxy", link.proxy.Name).
Str("link_addr", fmt.Sprintf("%p", link)).
Logger()
bytes, err := io.Copy(dest, link.output)
if err != nil {
logger.Warn().
Int64("bytes", bytes).
Err(err).
Msg("Could not write to destination")
} else if server.Metrics.proxyMetricsEnabled() {
server.Metrics.ProxyMetrics.SentBytesTotal.
WithLabelValues(metricLabels...).Add(float64(bytes))
}
dest.Close()
logger.Trace().Msgf("Remove link %s from ToxicCollection", name)
link.toxics.RemoveLink(name)
logger.Trace().Msgf("RemoveConnection %s from Proxy %s", name, link.proxy.Name)
link.proxy.RemoveConnection(name)
}
// Add a toxic to the end of the chain.
func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) {
i := len(link.stubs)
newin := make(chan *stream.StreamChunk, toxic.BufferSize)
link.stubs = append(link.stubs, toxics.NewToxicStub(newin, link.stubs[i-1].Output))
// Interrupt the last toxic so that we don't have a race when moving channels
if link.stubs[i-1].InterruptToxic() {
link.stubs[i-1].Output = newin
if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {
link.stubs[i].State = stateful.NewState()
}
go link.stubs[i].Run(toxic)
go link.stubs[i-1].Run(link.toxics.chain[link.direction][i-1])
} else {
// This link is already closed, make sure the new toxic matches
link.stubs[i].Output = newin // The real output is already closed, close this instead
link.stubs[i].Close()
}
}
// Update an existing toxic in the chain.
func (link *ToxicLink) UpdateToxic(toxic *toxics.ToxicWrapper) {
if link.stubs[toxic.Index].InterruptToxic() {
go link.stubs[toxic.Index].Run(toxic)
}
}
// Remove an existing toxic from the chain.
func (link *ToxicLink) RemoveToxic(ctx context.Context, toxic *toxics.ToxicWrapper) {
toxic_index := toxic.Index
log := zerolog.Ctx(ctx).
With().
Str("component", "ToxicLink").
Str("method", "RemoveToxic").
Str("toxic", toxic.Name).
Str("toxic_type", toxic.Type).
Int("toxic_index", toxic.Index).
Str("link_addr", fmt.Sprintf("%p", link)).
Str("toxic_stub_addr", fmt.Sprintf("%p", link.stubs[toxic_index])).
Str("prev_toxic_stub_addr", fmt.Sprintf("%p", link.stubs[toxic_index-1])).
Logger()
if link.stubs[toxic_index].InterruptToxic() {
cleanup, ok := toxic.Toxic.(toxics.CleanupToxic)
if ok {
cleanup.Cleanup(link.stubs[toxic_index])
// Cleanup could have closed the stub.
if link.stubs[toxic_index].Closed() {
log.Trace().Msg("Cleanup closed toxic and removed toxic")
// TODO: Check if cleanup happen would link.stubs recalculated?
return
}
}
log.Trace().Msg("Interrupting the previous toxic to update its output")
stop := make(chan bool)
go func(stub *toxics.ToxicStub, stop chan bool) {
stop <- stub.InterruptToxic()
}(link.stubs[toxic_index-1], stop)
// Unblock the previous toxic if it is trying to flush
// If the previous toxic is closed, continue flusing until we reach the end.
interrupted := false
stopped := false
for !interrupted {
select {
case interrupted = <-stop:
stopped = true
case tmp := <-link.stubs[toxic_index].Input:
if tmp == nil {
link.stubs[toxic_index].Close()
if !stopped {
<-stop
}
return // TODO: There are some steps after this to clean buffer
}
err := link.stubs[toxic_index].WriteOutput(tmp, 5*time.Second)
if err != nil {
log.Err(err).
Msg("Could not write last packets after interrupt to Output")
}
}
}
// Empty the toxic's buffer if necessary
for len(link.stubs[toxic_index].Input) > 0 {
tmp := <-link.stubs[toxic_index].Input
if tmp == nil {
link.stubs[toxic_index].Close()
return
}
err := link.stubs[toxic_index].WriteOutput(tmp, 5*time.Second)
if err != nil {
log.Err(err).
Msg("Could not write last packets after interrupt to Output")
}
}
link.stubs[toxic_index-1].Output = link.stubs[toxic_index].Output
link.stubs = append(link.stubs[:toxic_index], link.stubs[toxic_index+1:]...)
go link.stubs[toxic_index-1].Run(link.toxics.chain[link.direction][toxic_index-1])
}
}
// Direction returns the direction of the link (upstream or downstream).
func (link *ToxicLink) Direction() string {
return link.direction.String()
}