forked from gojek/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_routing_strategy.go
37 lines (32 loc) · 1016 Bytes
/
random_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
package extras
import (
"context"
"math/rand"
"strconv"
"github.com/gojek/fiber"
)
// RandomRoutingStrategy is just a reference implementation of a RoutingStrategy.
// It randomly selects a primary route and all other routes are fallbacks (with no specific order)
type RandomRoutingStrategy struct {
fiber.BaseFiberType
}
// SelectRoute on the RandomRoutingStrategy selects one of the given routes as the primary
// route, at random, and sets the others as fallbacks
func (s *RandomRoutingStrategy) SelectRoute(
_ context.Context,
_ fiber.Request,
routes map[string]fiber.Component,
) (route fiber.Component, fallbacks []fiber.Component, labels fiber.Labels, err error) {
idx := rand.Intn(len(routes))
// Add idx to attribute map for logging / debugging upstream
labels = fiber.NewLabelsMap().WithLabel("idx", strconv.Itoa(idx))
for _, child := range routes {
if idx == 0 {
route = child
} else {
fallbacks = append(fallbacks, child)
}
idx--
}
return route, fallbacks, labels, nil
}