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
|
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 {
select {
case <-time.After(timeout):
stub.Close()
return
case <-stub.Interrupt:
return
}
} else {
<-stub.Interrupt
return
}
}
func init() {
Register("timeout", new(TimeoutToxic))
}
|