-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
216 lines (178 loc) · 3.63 KB
/
regex.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
210
211
212
213
214
215
216
package main
import (
"context"
"fmt"
"regexp"
"strings"
"time"
stream "go.atomizer.io/stream"
)
func Match(
ctx context.Context,
logger Logger,
records ...*Record,
) (*Regex, error) {
requests := make(chan matcher)
patternChans := make([]chan<- matcher, 0, len(records))
for _, record := range records {
var exp *regexp.Regexp
var err error
if record.Type != REGEX && record.Type != WILDCARD {
continue
}
if record.Type == WILDCARD {
exp, err = Wildcard(record.Pattern)
if err != nil {
logger.Errorw(
"failed to compile wildcard",
"pattern", record.Pattern,
"error", err,
)
continue
}
} else {
exp, err = regexp.Compile(record.Pattern)
if err != nil {
logger.Errorw(
"failed to compile regex",
"pattern", record.Pattern,
"error", err,
)
continue
}
}
in := make(chan matcher)
// Append the pattern to the list of patterns
// for the fan-out
patternChans = append(patternChans, in)
// Setup the pattern so it can scale to handle load
s := stream.Scaler[matcher, struct{}]{
Wait: time.Nanosecond,
Life: time.Millisecond,
Fn: (&expr{record, exp}).match,
}
_, err = s.Exec(ctx, in)
if err != nil {
logger.Errorw(
"failed to setup regex",
"pattern", record.Pattern,
"error", err,
)
}
}
if len(patternChans) == 0 {
return nil, fmt.Errorf("no patterns provided")
}
go stream.FanOut(ctx, requests, patternChans...)
return &Regex{len(patternChans), requests}, nil
}
type Regex struct {
patterns int
requests chan<- matcher
}
func (r *Regex) Match(
ctx context.Context,
data string,
timeout time.Duration,
) <-chan *Record {
out := make(chan *Record, 1)
go func() {
defer close(out)
// Collapse request immediately when there are no patterns
if r.patterns == 0 {
return
}
ctx, cancel := context.WithTimeout(ctx, timeout)
detection := make(chan *Record)
// Push the match request
select {
case <-ctx.Done():
return
case r.requests <- &matchReq{
ctx: ctx,
cancel: cancel,
data: data,
match: detection,
}:
}
for i := 0; i < r.patterns; i++ {
// Wait for the match
select {
case <-ctx.Done():
return
case record, ok := <-detection:
if !ok {
return
}
if record == nil {
continue
}
out <- record
}
}
}()
return out
}
type matchReq struct {
ctx context.Context
cancel context.CancelFunc
data string
match chan *Record
}
func (m *matchReq) Data() (context.Context, string) {
return m.ctx, m.data
}
func (m *matchReq) Matched(ctx context.Context, record *Record) {
select {
case <-ctx.Done():
return
case <-m.ctx.Done():
return
case m.match <- record:
}
}
type matcher interface {
Data() (context.Context, string)
Matched(ctx context.Context, record *Record)
}
type expr struct {
record *Record
pattern *regexp.Regexp
}
func (e *expr) match(
ctx context.Context,
req matcher,
) (struct{}, bool) {
rctx, data := req.Data()
if data == "" {
return struct{}{}, false
}
select {
case <-ctx.Done():
return struct{}{}, false
case <-rctx.Done():
return struct{}{}, false
default:
var matched *Record
if e.pattern.MatchString(data) {
matched = e.record
}
req.Matched(ctx, matched)
}
return struct{}{}, false
}
var (
ErrWildcard = fmt.Errorf("invalid wildcard")
ErrDomain = fmt.Errorf("invalid domain")
)
func Wildcard(entry string) (*regexp.Regexp, error) {
wild := strings.LastIndex(entry, "*")
if wild != 0 {
return nil, ErrWildcard
}
entry = regexp.QuoteMeta(entry[wild+1:])
if len(entry) == 0 {
return nil, ErrDomain
}
return regexp.Compile(fmt.Sprintf("%s$", entry))
}