slightly faster switch logic, should be easier to have a useful tie-breaker for peers that are equally close to the destination via the tree metric

This commit is contained in:
Arceliar 2019-02-08 19:46:11 -06:00
parent 16d754bbbc
commit 74ac535d55
2 changed files with 26 additions and 23 deletions

View File

@ -6,13 +6,15 @@ import "os"
import "strings" import "strings"
import "strconv" import "strconv"
import "time" import "time"
import "log"
import "runtime" import "runtime"
import "runtime/pprof" import "runtime/pprof"
import "flag" import "flag"
import "github.com/gologme/log"
import . "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" import . "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
import . "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -569,23 +569,23 @@ func (t *switchTable) start() error {
return nil return nil
} }
// Check if a packet should go to the self node // Return a map of ports onto distance, keeping only ports closer to the destination than this node
// This means there's no node closer to the destination than us // If the map is empty (or nil), then no peer is closer
// This is mainly used to identify packets addressed to us, or that hit a blackhole func (t *switchTable) getCloser(dest []byte) map[switchPort]int {
func (t *switchTable) selfIsClosest(dest []byte) bool {
table := t.getTable() table := t.getTable()
myDist := table.self.dist(dest) myDist := table.self.dist(dest)
if myDist == 0 { if myDist == 0 {
// Skip the iteration step if it's impossible to be closer // Skip the iteration step if it's impossible to be closer
return true return nil
} }
closer := make(map[switchPort]int, len(table.elems))
for _, info := range table.elems { for _, info := range table.elems {
dist := info.locator.dist(dest) dist := info.locator.dist(dest)
if dist < myDist { if dist < myDist {
return false closer[info.port] = dist
} }
} }
return true return closer
} }
// Returns true if the peer is closer to the destination than ourself // Returns true if the peer is closer to the destination than ourself
@ -639,25 +639,26 @@ func (t *switchTable) bestPortForCoords(coords []byte) switchPort {
func (t *switchTable) handleIn(packet []byte, idle map[switchPort]struct{}) bool { func (t *switchTable) handleIn(packet []byte, idle map[switchPort]struct{}) bool {
coords := switch_getPacketCoords(packet) coords := switch_getPacketCoords(packet)
ports := t.core.peers.getPorts() ports := t.core.peers.getPorts()
if t.selfIsClosest(coords) { closer := t.getCloser(coords)
if len(closer) == 0 {
// TODO? call the router directly, and remove the whole concept of a self peer? // TODO? call the router directly, and remove the whole concept of a self peer?
ports[0].sendPacket(packet) ports[0].sendPacket(packet)
return true return true
} }
table := t.getTable()
myDist := table.self.dist(coords)
var best *peer var best *peer
bestDist := myDist var bestDist int
for port := range idle { for port, dist := range closer {
if to := ports[port]; to != nil { to := ports[port]
if info, isIn := table.elems[to.port]; isIn { _, isIdle := idle[port]
dist := info.locator.dist(coords) switch {
if !(dist < bestDist) { case to == nil: // skip
continue case !isIdle: // skip
} case best == nil: // keep
fallthrough
case dist < bestDist: // keep
best = to best = to
bestDist = dist bestDist = dist
} default: // skip
} }
} }
if best != nil { if best != nil {
@ -696,7 +697,7 @@ func (b *switch_buffers) cleanup(t *switchTable) {
// Remove queues for which we have no next hop // Remove queues for which we have no next hop
packet := buf.packets[0] packet := buf.packets[0]
coords := switch_getPacketCoords(packet.bytes) coords := switch_getPacketCoords(packet.bytes)
if t.selfIsClosest(coords) { if len(t.getCloser(coords)) == 0 {
for _, packet := range buf.packets { for _, packet := range buf.packets {
util.PutBytes(packet.bytes) util.PutBytes(packet.bytes)
} }