Skip to content

Commit

Permalink
Fix route selection IDs (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Apr 29, 2024
1 parent fd26e98 commit e435e39
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 5 additions & 1 deletion client/internal/routemanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ func (c *clientNetwork) getBestRouteFromStatuses(routePeerStatuses map[string]ro
if currScore != 0 && currScore < chosenScore+0.1 {
return currID
} 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)
}
}

Expand Down
24 changes: 17 additions & 7 deletions client/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package server
import (
"context"
"fmt"
"net/netip"
"sort"

"golang.org/x/exp/maps"

"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.
func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (*proto.ListRoutesResponse, error) {
s.mutex.Lock()
Expand All @@ -23,13 +29,17 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
routesMap := s.engine.GetClientRoutesWithNetID()
routeSelector := s.engine.GetRouteManager().GetRouteSelector()

var routes []*route.Route
var routes []*selectRoute
for id, rt := range routesMap {
if len(rt) == 0 {
continue
}
rt[0].ID = id
routes = append(routes, rt[0])
route := &selectRoute{
NetID: id,
Network: rt[0].Network,
Selected: routeSelector.IsSelected(id),
}
routes = append(routes, route)
}

sort.Slice(routes, func(i, j int) bool {
Expand All @@ -40,7 +50,7 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
iAddr := routes[i].Network.Addr()
jAddr := routes[j].Network.Addr()
if iAddr == jAddr {
return routes[i].ID < routes[j].ID
return routes[i].NetID < routes[j].NetID
}
return iAddr.String() < jAddr.String()
}
Expand All @@ -50,9 +60,9 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (
var pbRoutes []*proto.Route
for _, route := range routes {
pbRoutes = append(pbRoutes, &proto.Route{
ID: route.ID,
ID: route.NetID,
Network: route.Network.String(),
Selected: routeSelector.IsSelected(route.ID),
Selected: route.Selected,
})
}

Expand Down
6 changes: 6 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ func (r *Route) Copy() *Route {

// IsEqual compares one route with the other
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 &&
other.Description == r.Description &&
other.NetID == r.NetID &&
Expand Down

0 comments on commit e435e39

Please sign in to comment.