forked from gojek/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing_strategy.go
49 lines (42 loc) · 1.03 KB
/
routing_strategy.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
package fiber
import "context"
// RoutingStrategy picks up primary route and zero or more fallbacks
// from the map of router routes
type RoutingStrategy interface {
Type
// req - Incoming request (so the route can be selected based on the request)
// routes - map of all possible routes
SelectRoute(ctx context.Context,
req Request,
routes map[string]Component,
) (route Component, fallbacks []Component, err error)
}
type baseRoutingStrategy struct {
RoutingStrategy
BaseFiberType
}
func (s *baseRoutingStrategy) getRoutesOrder(
ctx context.Context,
req Request,
routes map[string]Component,
) (<-chan []Component, <-chan error) {
out := make(chan []Component)
errCh := make(chan error, 1)
go func() {
route, fallbacks, err := s.SelectRoute(ctx, req, routes)
if err != nil {
errCh <- err
} else {
// Append routes
routes := fallbacks
if route != nil {
routes = append([]Component{route}, routes...)
}
out <- routes
}
// Close both channels
close(out)
close(errCh)
}()
return out, errCh
}