/
timeout.go
52 lines (47 loc) · 979 Bytes
/
timeout.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
package toxics
import "time"
// The TimeoutToxic stops any data from flowing through, and will close the connection after a timeout.
// If the timeout is set to 0, then the connection will not be closed.
type TimeoutToxic struct {
// Times in milliseconds
Timeout int64 `json:"timeout"`
}
func (t *TimeoutToxic) Pipe(stub *ToxicStub) {
timeout := time.Duration(t.Timeout) * time.Millisecond
if timeout > 0 {
for {
select {
case <-time.After(timeout):
stub.Close()
return
case <-stub.Interrupt:
return
case c := <-stub.Input:
if c == nil {
stub.Close()
return
}
// Drop the data on the ground.
}
}
} else {
for {
select {
case <-stub.Interrupt:
return
case c := <-stub.Input:
if c == nil {
stub.Close()
return
}
// Drop the data on the ground.
}
}
}
}
func (t *TimeoutToxic) Cleanup(stub *ToxicStub) {
stub.Close()
}
func init() {
Register("timeout", new(TimeoutToxic))
}