search timing changes

This commit is contained in:
Arceliar 2020-02-06 18:37:58 -06:00
parent b8bab06f95
commit cd856426e5

View File

@ -27,6 +27,7 @@ const search_MAX_SEARCH_SIZE = 16
// This defines the time after which we time out a search (so it can restart).
const search_RETRY_TIME = 3 * time.Second
const search_STEP_TIME = 100 * time.Millisecond
// Information about an ongoing search.
// Includes the target NodeID, the bitmask to match it to an IP, and the list of nodes to visit / already visited.
@ -78,14 +79,20 @@ func (s *searches) createSearch(dest *crypto.NodeID, mask *crypto.NodeID, callba
// If there is, it adds the response info to the search and triggers a new search step.
// If there's no ongoing search, or we if the dhtRes finished the search (it was from the target node), then don't do anything more.
func (sinfo *searchInfo) handleDHTRes(res *dhtRes) {
sinfo.recv++
if res == nil || sinfo.checkDHTRes(res) {
// Either we don't recognize this search, or we just finished it
return
old := sinfo.visited
if res != nil {
sinfo.recv++
if sinfo.checkDHTRes(res) {
sinfo.searches.router.core.log.Debugln("Finished search:", sinfo.dest, sinfo.send, sinfo.recv)
return // Search finished successfully
}
// Add results to the search
sinfo.addToSearch(res)
}
if res == nil || sinfo.visited != old {
// Continue the search
sinfo.doSearchStep()
}
// Add to the search and continue
sinfo.addToSearch(res)
sinfo.doSearchStep()
}
// Adds the information from a dhtRes to an ongoing search.
@ -104,7 +111,7 @@ func (sinfo *searchInfo) addToSearch(res *dhtRes) {
// Sort
sort.SliceStable(sinfo.toVisit, func(i, j int) bool {
// Should return true if i is closer to the destination than j
return dht_ordered(&res.Dest, sinfo.toVisit[i].getNodeID(), sinfo.toVisit[j].getNodeID())
return dht_ordered(&sinfo.dest, sinfo.toVisit[i].getNodeID(), sinfo.toVisit[j].getNodeID())
})
}
}
@ -121,14 +128,14 @@ func (sinfo *searchInfo) doSearchStep() {
return
}
// Send to the next search target
for _, next := range sinfo.toVisit {
if len(sinfo.toVisit) > 0 {
next := sinfo.toVisit[0]
sinfo.toVisit = sinfo.toVisit[1:]
rq := dhtReqKey{next.key, sinfo.dest}
sinfo.searches.router.dht.addCallback(&rq, sinfo.handleDHTRes)
sinfo.searches.router.dht.ping(next, &sinfo.dest)
sinfo.time = time.Now()
sinfo.send++
}
sinfo.toVisit = sinfo.toVisit[:0]
}
// If we've recently sent a ping for this search, do nothing.
@ -138,7 +145,7 @@ func (sinfo *searchInfo) continueSearch() {
// In case the search dies, try to spawn another thread later
// Note that this will spawn multiple parallel searches as time passes
// Any that die aren't restarted, but a new one will start later
time.AfterFunc(search_RETRY_TIME, func() {
time.AfterFunc(search_STEP_TIME, func() {
sinfo.searches.router.Act(nil, func() {
// FIXME this keeps the search alive forever if not for the searches map, fix that
newSearchInfo := sinfo.searches.searches[sinfo.dest]
@ -171,6 +178,7 @@ func (sinfo *searchInfo) checkDHTRes(res *dhtRes) bool {
// Closer to the destination, so update visited
sinfo.searches.router.core.log.Debugln("Updating search:", sinfo.dest, *from.getNodeID(), sinfo.send, sinfo.recv)
sinfo.visited = *from.getNodeID()
sinfo.time = time.Now()
}
them := from.getNodeID()
var destMasked crypto.NodeID