5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-09-20 03:42:32 +00:00

deduplicate dht responses when adding them to the search, limit the search toVisit size

This commit is contained in:
Arceliar 2018-06-02 00:29:36 -05:00
parent 10a72444e3
commit ed6c9c2a54

View File

@ -70,17 +70,30 @@ func (s *searches) handleDHTRes(res *dhtRes) {
}
func (s *searches) addToSearch(sinfo *searchInfo, res *dhtRes) {
// TODO
// Add responses to toVisit if closer to dest than the res node
from := dhtInfo{key: res.key, coords: res.coords}
for _, info := range res.infos {
if dht_firstCloserThanThird(info.getNodeID(), &res.dest, from.getNodeID()) {
sinfo.toVisit = append(sinfo.toVisit, info)
}
}
// Deduplicate
vMap := make(map[NodeID]*dhtInfo)
for _, info := range sinfo.toVisit {
vMap[*info.getNodeID()] = info
}
sinfo.toVisit = sinfo.toVisit[:0]
for _, info := range vMap {
sinfo.toVisit = append(sinfo.toVisit, info)
}
// Sort
sort.SliceStable(sinfo.toVisit, func(i, j int) bool {
return dht_firstCloserThanThird(sinfo.toVisit[i].getNodeID(), &res.dest, sinfo.toVisit[j].getNodeID())
})
// Truncate to some maximum size
if len(sinfo.toVisit) > 16 {
sinfo.toVisit = sinfo.toVisit[:16]
}
}
func (s *searches) doSearchStep(sinfo *searchInfo) {