consistently prioritize which peer to forward to instead of letting it be partly random

This commit is contained in:
Arceliar 2019-02-09 17:44:25 -06:00
parent 74ac535d55
commit 21cecf4630

View File

@ -645,20 +645,39 @@ func (t *switchTable) handleIn(packet []byte, idle map[switchPort]struct{}) bool
ports[0].sendPacket(packet) ports[0].sendPacket(packet)
return true return true
} }
table := t.getTable()
var best *peer var best *peer
var bestDist int var bestDist int
var bestCoordLen int
for port, dist := range closer { for port, dist := range closer {
to := ports[port] to := ports[port]
_, isIdle := idle[port] _, isIdle := idle[port]
coordLen := len(table.elems[port].locator.coords)
var update bool
switch { switch {
case to == nil: // skip case to == nil:
case !isIdle: // skip //nothing
case best == nil: // keep case !isIdle:
fallthrough //nothing
case dist < bestDist: // keep case best == nil:
update = true
case dist < bestDist:
update = true
case dist > bestDist:
//nothing
case coordLen < bestCoordLen:
update = true
case coordLen > bestCoordLen:
//nothing
case port < best.port:
update = true
default:
//nothing
}
if update {
best = to best = to
bestDist = dist bestDist = dist
default: // skip bestCoordLen = coordLen
} }
} }
if best != nil { if best != nil {