-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_matcher.go
209 lines (187 loc) · 4.64 KB
/
request_matcher.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
package main
import (
"log"
"math"
"time"
)
// A Matcher returns a pointer to the Request from rec,
// that matches r the best.
// If no match is found, nil is returned.
// i is the index of r in its recording.
type Matcher interface {
Match(r *Request, recording, replayingFrom Recording) *Request
Seen(r *Request) bool
MarkSeen(r *Request)
}
type countingMatcher struct {
seen map[*Request]struct{}
}
func (m *countingMatcher) Seen(r *Request) bool {
_, ok := m.seen[r]
return ok
}
func (m *countingMatcher) MarkSeen(r *Request) {
m.seen[r] = struct{}{}
}
func (m *countingMatcher) Match(r *Request, recording, replayingFrom Recording) *Request {
i := len(recording.getStream(r.StreamIdentifier))
rec := replayingFrom.getStream(r.StreamIdentifier)
if i < len(rec) && !m.Seen(rec[i]) && !rec[i].FromOutside {
m.MarkSeen(rec[i])
return rec[i]
}
return nil
}
type heuristicMatcher struct {
seen map[*Request]struct{}
}
func (m *heuristicMatcher) Seen(r *Request) bool {
_, ok := m.seen[r]
return ok
}
func (m *heuristicMatcher) MarkSeen(r *Request) {
m.seen[r] = struct{}{}
}
func (m *heuristicMatcher) Match(r *Request, recording, replayingFrom Recording) *Request {
i := len(recording.getStream(r.StreamIdentifier))
rec := replayingFrom.getStream(r.StreamIdentifier)
highScore := -math.MaxFloat64
var bestMatch *Request
faktor := float64(len(rec))
for j, req := range rec {
if m.Seen(req) || req.FromOutside || r.Method != req.Method {
continue
}
score := 0.0
score -= math.Abs(float64(j - i))
if req.To == r.To {
score += 1 * faktor
}
score -= math.Abs(float64(len(req.To)-len(r.To))) * faktor / 10
if string(req.Body) == string(r.Body) {
score += 1 * faktor
}
score -= math.Abs(float64(len(req.Body)-len(r.Body))) * faktor / 10
if score > highScore {
highScore = score
bestMatch = req
}
}
return bestMatch
}
type exactMatcher struct {
seen map[*Request]struct{}
}
func (m *exactMatcher) Seen(r *Request) bool {
_, ok := m.seen[r]
return ok
}
func (m *exactMatcher) MarkSeen(r *Request) {
m.seen[r] = struct{}{}
}
func (m *exactMatcher) Match(r *Request, recording, replayingFrom Recording) *Request {
i := len(recording.getStream(r.StreamIdentifier))
rec := replayingFrom.getStream(r.StreamIdentifier)
highScore := -math.MaxFloat64
var bestMatch *Request
for j, req := range rec {
if m.Seen(req) || req.FromOutside || r.Method != req.Method {
continue
}
score := 0.0
if i == j {
score += 1
}
if req.To == r.To {
score += 1
}
if len(req.To) == len(r.To) {
score += 1
}
if string(req.Body) == string(r.Body) {
score += 1
}
if len(req.Body) == len(r.Body) {
score += 1
}
if score > highScore {
highScore = score
bestMatch = req
}
}
return bestMatch
}
type mixMatcher struct {
seen map[*Request]struct{}
}
func (m *mixMatcher) Seen(r *Request) bool {
_, ok := m.seen[r]
return ok
}
func (m *mixMatcher) MarkSeen(r *Request) {
m.seen[r] = struct{}{}
}
func (m *mixMatcher) Match(r *Request, recording, replayingFrom Recording) *Request {
i := len(recording.getStream(r.StreamIdentifier))
rec := replayingFrom.getStream(r.StreamIdentifier)
highScore := -math.MaxFloat64
var bestMatch *Request
faktor := float64(len(rec))
for j, req := range rec {
if m.Seen(req) || req.FromOutside || r.Method != req.Method {
continue
}
score := 0.0
score -= math.Abs(float64(j - i))
if req.To == r.To {
score += 1 * faktor
}
if len(req.To) == len(r.To) {
score += 1 * faktor
}
if string(req.Body) == string(r.Body) {
score += 1 * faktor
}
if len(req.Body) == len(r.Body) {
score += 1 * faktor
}
if score > highScore {
highScore = score
bestMatch = req
}
}
return bestMatch
}
type timingMatcher struct {
seen map[*Request]struct{}
}
func (m *timingMatcher) Seen(r *Request) bool {
_, ok := m.seen[r]
return ok
}
func (m *timingMatcher) MarkSeen(r *Request) {
m.seen[r] = struct{}{}
}
func (m *timingMatcher) Match(r *Request, recording, replayingFrom Recording) *Request {
timePassed := r.Timestamp.Sub(recording.StartTime)
for _, req := range replayingFrom.Requests {
if req.Timestamp.Sub(replayingFrom.StartTime) <= timePassed+20*time.Millisecond {
if !m.Seen(req) && !req.FromOutside {
m.MarkSeen(req)
}
} else {
break
}
}
var blockConf BlockConfig
for _, conf := range replayingFrom.BlockConfigs {
if conf.Timestamp.Sub(replayingFrom.StartTime) <= timePassed {
blockConf = conf
} else {
break
}
}
log.Println(blockConf.Partitions)
blockRequest, blockResponse := blockConf.Block(r, func(r *Request) (bool, bool) { return false, false })
return &Request{Blocked: blockRequest, BlockedResponse: blockResponse}
}