summaryrefslogtreecommitdiff
path: root/internal/routing/router.go
blob: 53e52617ce60b42d305198afe442c2652b44e1e2 (plain)
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
package routing

import (
	"errors"
	"sort"
	"strconv"
	"sync"
	"sync/atomic"

	"aigw/internal/catalog"
	"aigw/internal/domain"
)

var ErrNoRoute = errors.New("no compatible upstream route")

type Router struct {
	catalog  *catalog.Catalog
	counters sync.Map
}

func New(catalog *catalog.Catalog) *Router {
	return &Router{catalog: catalog}
}

func (r *Router) Plan(modelID string, protocol domain.Protocol) ([]domain.Route, error) {
	model, err := r.catalog.Model(modelID)
	if err != nil {
		return nil, err
	}
	routes := make([]domain.Route, 0, len(model.Routes))
	for _, route := range model.Routes {
		if route.Provider.Protocol == protocol {
			routes = append(routes, route)
		}
	}
	if len(routes) == 0 {
		return nil, ErrNoRoute
	}

	sort.SliceStable(routes, func(i, j int) bool { return routes[i].Priority < routes[j].Priority })
	result := make([]domain.Route, 0, len(routes))
	for start := 0; start < len(routes); {
		end := start + 1
		for end < len(routes) && routes[end].Priority == routes[start].Priority {
			end++
		}
		result = append(result, r.rotate(modelID, protocol, routes[start:end])...)
		start = end
	}
	return result, nil
}

func (r *Router) rotate(modelID string, protocol domain.Protocol, routes []domain.Route) []domain.Route {
	if len(routes) < 2 {
		return append([]domain.Route(nil), routes...)
	}
	key := modelID + "\x00" + string(protocol) + "\x00" + strconv.Itoa(routes[0].Priority)
	counterValue, _ := r.counters.LoadOrStore(key, &atomic.Uint64{})
	counter := counterValue.(*atomic.Uint64).Add(1) - 1

	totalWeight := 0
	for _, route := range routes {
		totalWeight += route.Weight
	}
	position := int(counter % uint64(totalWeight))
	selected := 0
	for i, route := range routes {
		if position < route.Weight {
			selected = i
			break
		}
		position -= route.Weight
	}

	result := make([]domain.Route, 0, len(routes))
	result = append(result, routes[selected])
	for offset := 1; offset < len(routes); offset++ {
		result = append(result, routes[(selected+offset)%len(routes)])
	}
	return result
}