forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limit_data.go
60 lines (48 loc) · 1.06 KB
/
limit_data.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
package toxics
import "github.com/Shopify/toxiproxy/stream"
// LimitDataToxic has limit in bytes
type LimitDataToxic struct {
Bytes int64 `json:"bytes"`
}
type LimitDataToxicState struct {
bytesTransmitted int64
}
func (t *LimitDataToxic) Pipe(stub *ToxicStub) {
state := stub.State.(*LimitDataToxicState)
var bytesRemaining = t.Bytes - state.bytesTransmitted
for {
select {
case <-stub.Interrupt:
return
case c := <-stub.Input:
if c == nil {
stub.Close()
return
}
if bytesRemaining < 0 {
bytesRemaining = 0
}
if bytesRemaining < int64(len(c.Data)) {
c = &stream.StreamChunk{
Timestamp: c.Timestamp,
Data: c.Data[0:bytesRemaining],
}
}
if len(c.Data) > 0 {
stub.Output <- c
state.bytesTransmitted += int64(len(c.Data))
}
bytesRemaining = t.Bytes - state.bytesTransmitted
if bytesRemaining <= 0 {
stub.Close()
return
}
}
}
}
func (t *LimitDataToxic) NewState() interface{} {
return new(LimitDataToxicState)
}
func init() {
Register("limit_data", new(LimitDataToxic))
}