mirror of
https://github.com/tailscale/tailscale.git
synced 2025-07-29 23:33:45 +00:00
ipn/ipnlocal: modernize nm.Peers with AppendMatchingPeers
Thanks to @nickkhyl for pointing out that NetMap.Peers doesn’t get incremental updates since the last full NetMap update. Instead, he recommends using ipn/ipnlocal.nodeBackend.AppendMatchingPeers. Updates #cleanup Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
parent
b63f8a457d
commit
bfb344905f
@ -7902,33 +7902,32 @@ func suggestExitNodeUsingTrafficSteering(nb *nodeBackend, prev tailcfg.StableNod
|
|||||||
panic("missing traffic-steering capability")
|
panic("missing traffic-steering capability")
|
||||||
}
|
}
|
||||||
|
|
||||||
peers := nm.Peers
|
var force tailcfg.NodeView
|
||||||
nodes := make([]tailcfg.NodeView, 0, len(peers))
|
nodes := nb.AppendMatchingPeers(nil, func(p tailcfg.NodeView) bool {
|
||||||
|
|
||||||
for _, p := range peers {
|
|
||||||
if !p.Valid() {
|
if !p.Valid() {
|
||||||
continue
|
return false
|
||||||
}
|
}
|
||||||
if allowed != nil && !allowed.Contains(p.StableID()) {
|
if allowed != nil && !allowed.Contains(p.StableID()) {
|
||||||
continue
|
return false
|
||||||
}
|
}
|
||||||
if !p.CapMap().Contains(tailcfg.NodeAttrSuggestExitNode) {
|
if !p.CapMap().Contains(tailcfg.NodeAttrSuggestExitNode) {
|
||||||
continue
|
return false
|
||||||
}
|
}
|
||||||
if !tsaddr.ContainsExitRoutes(p.AllowedIPs()) {
|
if !tsaddr.ContainsExitRoutes(p.AllowedIPs()) {
|
||||||
continue
|
return false
|
||||||
}
|
}
|
||||||
if p.StableID() == prev {
|
if p.StableID() == prev {
|
||||||
// Prevent flapping: since prev is a valid suggestion,
|
// Prevent flapping: since prev is a valid suggestion,
|
||||||
// force prev to be the only valid pick.
|
// force prev to be the only valid pick.
|
||||||
nodes = []tailcfg.NodeView{p}
|
force = p
|
||||||
break
|
return false
|
||||||
}
|
}
|
||||||
nodes = append(nodes, p)
|
return true
|
||||||
|
})
|
||||||
|
if force.Valid() {
|
||||||
|
nodes = append(nodes[:0], force)
|
||||||
}
|
}
|
||||||
|
|
||||||
var pick tailcfg.NodeView
|
|
||||||
|
|
||||||
scores := make(map[tailcfg.NodeID]int, len(nodes))
|
scores := make(map[tailcfg.NodeID]int, len(nodes))
|
||||||
score := func(n tailcfg.NodeView) int {
|
score := func(n tailcfg.NodeView) int {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
@ -7945,7 +7944,11 @@ func suggestExitNodeUsingTrafficSteering(nb *nodeBackend, prev tailcfg.StableNod
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(nodes) > 0 {
|
var pick tailcfg.NodeView
|
||||||
|
if len(nodes) == 1 {
|
||||||
|
pick = nodes[0]
|
||||||
|
}
|
||||||
|
if len(nodes) > 1 {
|
||||||
// Find the highest scoring exit nodes.
|
// Find the highest scoring exit nodes.
|
||||||
slices.SortFunc(nodes, func(a, b tailcfg.NodeView) int {
|
slices.SortFunc(nodes, func(a, b tailcfg.NodeView) int {
|
||||||
return cmp.Compare(score(b), score(a)) // reverse sort
|
return cmp.Compare(score(b), score(a)) // reverse sort
|
||||||
|
Loading…
x
Reference in New Issue
Block a user