-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathrouting.go
462 lines (387 loc) · 12.6 KB
/
routing.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package routing
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/logging"
"github.com/zalando/skipper/metrics"
"github.com/zalando/skipper/predicates"
)
const (
// Deprecated, use predicates.PathName instead
PathName = predicates.PathName
// Deprecated, use predicates.PathSubtreeName instead
PathSubtreeName = predicates.PathSubtreeName
// Deprecated, use predicates.WeightName instead
WeightPredicateName = predicates.WeightName
routesTimestampName = "X-Timestamp"
RoutesCountName = "X-Count"
defaultRouteListingLimit = 1024
)
// MatchingOptions controls route matching.
type MatchingOptions uint
const (
// MatchingOptionsNone indicates that all options are default.
MatchingOptionsNone MatchingOptions = 0
// IgnoreTrailingSlash indicates that trailing slashes in paths are ignored.
IgnoreTrailingSlash MatchingOptions = 1 << iota
)
func (o MatchingOptions) ignoreTrailingSlash() bool {
return o&IgnoreTrailingSlash > 0
}
// DataClient instances provide data sources for
// route definitions.
type DataClient interface {
LoadAll() ([]*eskip.Route, error)
LoadUpdate() ([]*eskip.Route, []string, error)
}
// Predicate instances are used as custom user defined route
// matching predicates.
type Predicate interface {
// Returns true if the request matches the predicate.
Match(*http.Request) bool
}
// PredicateSpec instances are used to create custom predicates
// (of type Predicate) with concrete arguments during the
// construction of the routing tree.
type PredicateSpec interface {
// Name of the predicate as used in the route definitions.
Name() string
// Creates a predicate instance with concrete arguments.
Create([]interface{}) (Predicate, error)
}
type WeightedPredicateSpec interface {
PredicateSpec
// Extra Weight of the predicate
Weight() int
}
// Options for initialization for routing.
type Options struct {
// Registry containing the available filter
// specifications that are used during processing
// the filter chains in the route definitions.
FilterRegistry filters.Registry
// Matching options are flags that control the
// route matching.
MatchingOptions MatchingOptions
// The timeout between requests to the data
// clients for route definition updates.
PollTimeout time.Duration
// The set of different data clients where the
// route definitions are read from.
DataClients []DataClient
// Specifications of custom, user defined predicates.
Predicates []PredicateSpec
// Performance tuning option.
//
// When zero, the newly constructed routing
// tree will take effect on the next routing
// query after every update from the data
// clients. In case of higher values, the
// routing queries have priority over the
// update channel, but the next routing tree
// takes effect only a few requests later.
//
// (Currently disabled and used with hard wired
// 0, until the performance benefit is verified
// by benchmarks.)
UpdateBuffer int
// Set a custom logger if necessary.
Log logging.Logger
// SuppressLogs indicates whether to log only a summary of the route changes.
SuppressLogs bool
// Metrics is used to collect monitoring data about the routes health, including
// total number of routes applied and UNIX time of the last routes update.
Metrics metrics.Metrics
// PreProcessors contains custom eskip.Route pre-processors.
PreProcessors []PreProcessor
// PostProcessors contains custom route post-processors.
PostProcessors []PostProcessor
// SignalFirstLoad enables signaling on the first load
// of the routing configuration during the startup.
SignalFirstLoad bool
}
// RouteFilter contains extensions to generic filter
// interface, serving mainly logging/monitoring
// purpose.
type RouteFilter struct {
filters.Filter
Name string
// Deprecated: currently not used, and post-processors may not maintain a correct value
Index int
}
// LBEndpoint represents the scheme and the host of load balanced
// backends.
type LBEndpoint struct {
Scheme, Host string
Metrics Metrics
}
// LBAlgorithm implementations apply a load balancing algorithm
// over the possible endpoints of a load balanced route.
type LBAlgorithm interface {
Apply(*LBContext) LBEndpoint
}
// LBContext is used to pass data to the load balancer to decide based
// on that data which endpoint to call from the backends
type LBContext struct {
Request *http.Request
Route *Route
LBEndpoints []LBEndpoint
Params map[string]interface{}
}
// NewLBContext is used to create a new LBContext, to pass data to the
// load balancer algorithms.
// Deprecated: create LBContext instead
func NewLBContext(r *http.Request, rt *Route) *LBContext {
return &LBContext{
Request: r,
Route: rt,
}
}
// Route object with preprocessed filter instances.
type Route struct {
// Fields from the static route definition.
eskip.Route
// weight used internally, received from the Weight() predicates.
weight int
// path predicate matching a subtree
path string
// path predicate matching a subtree
pathSubtree string
// The backend scheme and host.
Scheme, Host string
// The preprocessed custom predicate instances.
Predicates []Predicate
// The preprocessed filter instances.
Filters []*RouteFilter
// LBEndpoints contain the possible endpoints of a load
// balanced route.
LBEndpoints []LBEndpoint
// LBAlgorithm is the selected load balancing algorithm
// of a load balanced route.
LBAlgorithm LBAlgorithm
// LBFadeInDuration defines the duration of the fade-in
// function to be applied to new LB endpoints associated
// with this route.
LBFadeInDuration time.Duration
// LBExponent defines a secondary exponent modifier of
// the fade-in function configured mainly by the LBFadeInDuration
// field, adjusting the shape of the fade-in. By default,
// its value is usually 1, meaning linear fade-in, and it's
// configured by the post-processor found in the filters/fadein
// package.
LBFadeInExponent float64
}
// PostProcessor is an interface for custom post-processors applying changes
// to the routes after they were created from their data representation and
// before they were passed to the proxy.
type PostProcessor interface {
Do([]*Route) []*Route
}
// PreProcessor is an interface for custom pre-processors applying changes
// to the routes before they were created from eskip.Route representation.
type PreProcessor interface {
Do([]*eskip.Route) []*eskip.Route
}
// Routing ('router') instance providing live
// updatable request matching.
type Routing struct {
routeTable atomic.Value // of struct routeTable
log logging.Logger
firstLoad chan struct{}
firstLoadSignaled bool
quit chan struct{}
metrics metrics.Metrics
}
// New initializes a routing instance, and starts listening for route
// definition updates.
func New(o Options) *Routing {
if o.Log == nil {
o.Log = &logging.DefaultLog{}
}
r := &Routing{log: o.Log, firstLoad: make(chan struct{}), quit: make(chan struct{})}
r.metrics = o.Metrics
if !o.SignalFirstLoad {
close(r.firstLoad)
r.firstLoadSignaled = true
}
initialMatcher, _ := newMatcher(nil, MatchingOptionsNone)
rt := &routeTable{
m: initialMatcher,
created: time.Now(),
}
r.routeTable.Store(rt)
r.startReceivingUpdates(o)
return r
}
// ServeHTTP renders the list of current routes.
func (r *Routing) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" && req.Method != "HEAD" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
rt := r.routeTable.Load().(*routeTable)
req.ParseForm()
createdUnix := strconv.FormatInt(rt.created.Unix(), 10)
ts := req.Form.Get("timestamp")
if ts != "" && createdUnix != ts {
http.Error(w, "invalid timestamp", http.StatusBadRequest)
return
}
if req.Method == "HEAD" {
w.Header().Set(routesTimestampName, createdUnix)
w.Header().Set(RoutesCountName, strconv.Itoa(len(rt.validRoutes)))
if strings.Contains(req.Header.Get("Accept"), "application/json") {
w.Header().Set("Content-Type", "application/json")
} else {
w.Header().Set("Content-Type", "text/plain")
}
return
}
offset, err := extractParam(req, "offset", 0)
if err != nil {
http.Error(w, "invalid offset", http.StatusBadRequest)
return
}
limit, err := extractParam(req, "limit", defaultRouteListingLimit)
if err != nil {
http.Error(w, "invalid limit", http.StatusBadRequest)
return
}
w.Header().Set(routesTimestampName, createdUnix)
w.Header().Set(RoutesCountName, strconv.Itoa(len(rt.validRoutes)))
routes := slice(rt.validRoutes, offset, limit)
if strings.Contains(req.Header.Get("Accept"), "application/json") {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(routes); err != nil {
http.Error(
w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
)
}
return
}
w.Header().Set("Content-Type", "text/plain")
eskip.Fprint(w, extractPretty(req), routes...)
}
func (r *Routing) startReceivingUpdates(o Options) {
dc := len(o.DataClients)
c := make(chan *routeTable)
go receiveRouteMatcher(o, c, r.quit)
go func() {
for {
select {
case rt := <-c:
r.routeTable.Store(rt)
if !r.firstLoadSignaled {
dc--
if dc == 0 {
close(r.firstLoad)
r.firstLoadSignaled = true
}
}
r.log.Infof("route settings applied, id: %d", rt.id)
if r.metrics != nil { // existing codebases might not supply metrics instance
r.metrics.UpdateGauge("routes.total", float64(len(rt.validRoutes)))
r.metrics.UpdateGauge("routes.updated_timestamp", float64(rt.created.Unix()))
r.metrics.MeasureSince("routes.update_latency", rt.created)
}
case <-r.quit:
var rt *routeTable
rt, ok := r.routeTable.Load().(*routeTable)
if ok {
rt.close()
}
return
}
}
}()
}
// Route matches a request in the current routing tree.
//
// If the request matches a route, returns the route and a map of
// parameters constructed from the wildcard parameters in the path
// condition if any. If there is no match, it returns nil.
func (r *Routing) Route(req *http.Request) (*Route, map[string]string) {
rt := r.routeTable.Load().(*routeTable)
return rt.m.match(req)
}
// FirstLoad, when enabled, blocks until the first routing configuration was received
// by the routing during the startup. When disabled, it doesn't block.
func (r *Routing) FirstLoad() <-chan struct{} {
return r.firstLoad
}
// RouteLookup captures a single generation of the lookup tree, allowing multiple
// lookups to the same version of the lookup tree.
//
// Experimental feature. Using this solution potentially can cause large memory
// consumption in extreme cases, typically when:
// the total number routes is large, the backend responses to a subset of these
// routes is slow, and there's a rapid burst of consecutive updates to the
// routing table. This situation is considered an edge case, but until a protection
// against is found, the feature is experimental and its exported interface may
// change.
type RouteLookup struct {
matcher *matcher
}
// Do executes the lookup against the captured routing table. Equivalent to
// Routing.Route().
func (rl *RouteLookup) Do(req *http.Request) (*Route, map[string]string) {
return rl.matcher.match(req)
}
// Get returns a captured generation of the lookup table. This feature is
// experimental. See the description of the RouteLookup type.
func (r *Routing) Get() *RouteLookup {
rt := r.routeTable.Load().(*routeTable)
return &RouteLookup{matcher: rt.m}
}
// Close closes routing, routeTable and stops statemachine for receiving routes.
func (r *Routing) Close() {
close(r.quit)
}
func slice(r []*eskip.Route, offset int, limit int) []*eskip.Route {
if offset > len(r) {
offset = len(r)
}
end := offset + limit
if end > len(r) {
end = len(r)
}
result := r[offset:end]
if result == nil {
return []*eskip.Route{}
}
return result
}
func extractParam(r *http.Request, key string, defaultValue int) (int, error) {
param := r.Form.Get(key)
if param == "" {
return defaultValue, nil
}
val, err := strconv.Atoi(param)
if err != nil {
return 0, err
}
if val < 0 {
return 0, fmt.Errorf("invalid value `%d` for `%s`", val, key)
}
return val, nil
}
func extractPretty(r *http.Request) eskip.PrettyPrintInfo {
vals, ok := r.Form["nopretty"]
if !ok || len(vals) == 0 {
return eskip.PrettyPrintInfo{Pretty: true, IndentStr: " "}
}
val := vals[0]
if val == "0" || val == "false" {
return eskip.PrettyPrintInfo{Pretty: true, IndentStr: " "}
}
return eskip.PrettyPrintInfo{Pretty: false, IndentStr: ""}
}