Fix route selection IDs (#1890)

This commit is contained in:
Viktor Liu 2024-04-29 18:43:14 +02:00 committed by GitHub
parent fd26e989e3
commit e435e39158
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 8 deletions

View File

@ -156,7 +156,11 @@ func (c *clientNetwork) getBestRouteFromStatuses(routePeerStatuses map[string]ro
if currScore != 0 && currScore < chosenScore+0.1 { if currScore != 0 && currScore < chosenScore+0.1 {
return currID return currID
} else { } else {
log.Infof("new chosen route is %s with peer %s with score %f for network %s", chosen, c.routes[chosen].Peer, chosenScore, c.network) var peer string
if route := c.routes[chosen]; route != nil {
peer = route.Peer
}
log.Infof("new chosen route is %s with peer %s with score %f for network %s", chosen, peer, chosenScore, c.network)
} }
} }

View File

@ -3,14 +3,20 @@ package server
import ( import (
"context" "context"
"fmt" "fmt"
"net/netip"
"sort" "sort"
"golang.org/x/exp/maps" "golang.org/x/exp/maps"
"github.com/netbirdio/netbird/client/proto" "github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/route"
) )
type selectRoute struct {
NetID string
Network netip.Prefix
Selected bool
}
// ListRoutes returns a list of all available routes. // ListRoutes returns a list of all available routes.
func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (*proto.ListRoutesResponse, error) { func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (*proto.ListRoutesResponse, error) {
s.mutex.Lock() s.mutex.Lock()
@ -23,13 +29,17 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
routesMap := s.engine.GetClientRoutesWithNetID() routesMap := s.engine.GetClientRoutesWithNetID()
routeSelector := s.engine.GetRouteManager().GetRouteSelector() routeSelector := s.engine.GetRouteManager().GetRouteSelector()
var routes []*route.Route var routes []*selectRoute
for id, rt := range routesMap { for id, rt := range routesMap {
if len(rt) == 0 { if len(rt) == 0 {
continue continue
} }
rt[0].ID = id route := &selectRoute{
routes = append(routes, rt[0]) NetID: id,
Network: rt[0].Network,
Selected: routeSelector.IsSelected(id),
}
routes = append(routes, route)
} }
sort.Slice(routes, func(i, j int) bool { sort.Slice(routes, func(i, j int) bool {
@ -40,7 +50,7 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
iAddr := routes[i].Network.Addr() iAddr := routes[i].Network.Addr()
jAddr := routes[j].Network.Addr() jAddr := routes[j].Network.Addr()
if iAddr == jAddr { if iAddr == jAddr {
return routes[i].ID < routes[j].ID return routes[i].NetID < routes[j].NetID
} }
return iAddr.String() < jAddr.String() return iAddr.String() < jAddr.String()
} }
@ -50,9 +60,9 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
var pbRoutes []*proto.Route var pbRoutes []*proto.Route
for _, route := range routes { for _, route := range routes {
pbRoutes = append(pbRoutes, &proto.Route{ pbRoutes = append(pbRoutes, &proto.Route{
ID: route.ID, ID: route.NetID,
Network: route.Network.String(), Network: route.Network.String(),
Selected: routeSelector.IsSelected(route.ID), Selected: route.Selected,
}) })
} }

View File

@ -107,6 +107,12 @@ func (r *Route) Copy() *Route {
// IsEqual compares one route with the other // IsEqual compares one route with the other
func (r *Route) IsEqual(other *Route) bool { func (r *Route) IsEqual(other *Route) bool {
if r == nil && other == nil {
return true
} else if r == nil || other == nil {
return false
}
return other.ID == r.ID && return other.ID == r.ID &&
other.Description == r.Description && other.Description == r.Description &&
other.NetID == r.NetID && other.NetID == r.NetID &&