forked from mefellows/muxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_delay.go
81 lines (70 loc) · 2.13 KB
/
http_delay.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
package symptom
import (
"github.com/YangYongZhi/muxy/log"
"github.com/YangYongZhi/muxy/muxy"
"github.com/mefellows/plugo/plugo"
"time"
)
// HTTPDelaySymptom adds specified delays to requests Symptom
// Update docs: these values should be in ms
type HTTPDelaySymptom struct {
RequestDelay int `required:"false" mapstructure:"request_delay"`
ResponseDelay int `required:"false" mapstructure:"response_delay"`
Delay int `required:"false" mapstructure:"delay"`
MatchingRules []MatchingRule `required:"false" mapstructure:"matching_rules"`
}
var oneSecondInMillis = 1000
func init() {
plugo.PluginFactories.Register(func() (interface{}, error) {
return &HTTPDelaySymptom{}, nil
}, "http_delay")
plugo.PluginFactories.Register(func() (interface{}, error) {
return &HTTPDelaySymptom{}, nil
}, "delay")
}
// Setup sets up the delay plugin
func (m *HTTPDelaySymptom) Setup() {
log.Debug("Start to sets up the delay plugin")
// Add default (catch all) matching rule
// Only applicable if none supplied
if len(m.MatchingRules) == 0 {
m.MatchingRules = []MatchingRule{
defaultMatchingRule,
}
}
}
// Teardown shuts down the plugin
func (m *HTTPDelaySymptom) Teardown() {
log.Debug("Tear down the delay plugin")
}
// HandleEvent takes a proxy event for the proxy to intercept and modify
func (m *HTTPDelaySymptom) HandleEvent(e muxy.ProxyEvent, ctx *muxy.Context) {
if MatchSymptoms(m.MatchingRules, *ctx) {
log.Trace("HTTP Delay Tamperer Hit")
switch e {
case muxy.EventPreDispatch:
if m.RequestDelay != 0 {
m.Muck(ctx, m.RequestDelay)
}
case muxy.EventPostDispatch:
if m.ResponseDelay != 0 {
m.Muck(ctx, m.ResponseDelay)
} else if m.Delay != 0 { // legacy behaviour
m.Muck(ctx, m.Delay*oneSecondInMillis) // convert to ms
}
}
} else {
log.Trace("HTTP Delay Tamperer Miss")
}
}
// Muck injects chaos into the system
func (m *HTTPDelaySymptom) Muck(ctx *muxy.Context, wait int) {
delay := time.Duration(wait) * time.Millisecond
log.Debug("Delay Symptom - Muck(), delaying for %v seconds", delay.Seconds())
for {
select {
case <-time.After(delay):
return
}
}
}